5

Elastic Search Script を介して Java API を使用して、インデックスの複数の値を更新しようとしています。しかし、フィールドを更新できません。

サンプルコード:-

1:

UpdateResponse response = request.setScript("ctx._source").setScriptParams(scriptParams).execute().actionGet();

2:

UpdateResponse response = request.setScript("ctx._source.").setScriptParams(scriptParams).execute().actionGet();

("ctx._source.") で .(dot) に言及した場合は、 illegalArgument Exception が発生し、ドットを使用しない場合、例外は発生しませんが、インデックスで値が更新されません。これを解決するための解決策を教えてください。

4

4 に答える 4

13

まず第一ctx._sourceに、コメント投稿者の 1 人が既に指摘したように、スクリプト () は何もしません。たとえば、フィールド「a」を更新する場合は、次のようなスクリプトが必要になります。

ctx._source.a = "foobar"

これにより、文字列「foobar」がフィールド「a」に割り当てられます。ただし、単純な割り当て以上のことを行うことができます。詳細と例については、ドキュメントをご覧ください。

http://www.elasticsearch.org/guide/reference/api/update/

1 つのスクリプトで複数のフィールドを更新することも可能です。セミコロンを使用して、異なる MVEL 命令を区切ることができます。例えば:

ctx._source.a = "foo"; ctx._source.b = "bar"
于 2013-08-15T11:50:35.207 に答える
5

エラスティック検索には、Update Java API があります。次のコードを見てください

client.prepareUpdate("index","typw","1153")
            .addScriptParam("assignee", assign)
             .addScriptParam("newobject", responsearray)
            .setScript("ctx._source.assignee=assignee;ctx._source.responsearray=newobject ").execute().actionGet();

ここで、割り当て変数にはオブジェクト値が含まれ、応答配列変数にはデータのリストが含まれます。

于 2014-11-13T06:43:32.873 に答える
0
 XContentType contentType = 
 org.elasticsearch.client.Requests.INDEX_CONTENT_TYPE;
 public XContentBuilder getBuilder(User assign){
 try {
      XContentBuilder builder = XContentFactory.contentBuilder(contentType);
        builder.startObject();
      Map<String,?> assignMap=objectMap.convertValue(assign, Map.class);
                 builder.field("assignee",assignMap);
      return builder;
  } catch (IOException e) {
                log.error("custom field index",e);
}
      IndexRequest indexRequest = new IndexRequest();
        indexRequest.source(getBuilder(assign));
        UpdateQuery updateQuery = new UpdateQueryBuilder()
                                        .withType(<IndexType>)
                                        .withIndexName(<IndexName>)
                                        .withId(String.valueOf(id))
                                        .withClass(<IndexClass>)
                                        .withIndexRequest(indexRequest)
                                        .build();
于 2019-03-02T07:08:32.377 に答える