【问题标题】:How to update multiple fields using java api elasticsearch script如何使用 java api elasticsearch 脚本更新多个字段
【发布时间】:2019-03-02 15:08:32
【问题描述】:

我正在尝试通过弹性搜索脚本使用 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) 得到非法参数异常,并且如果我不使用 dot,则不会得到任何异常但索引中的值没有得到更新。 谁能告诉我解决这个问题的方法。

【问题讨论】:

  • 脚本参数是什么样的?您的脚本提到了 _source,但没有分配。你看过here的例子吗?

标签: elasticsearch


【解决方案1】:

首先,正如其中一位评论者已经指出的那样,您的脚本 (ctx._source) 没有做任何事情。如果你想更新,比如说,字段“a”,那么你需要一个像这样的脚本:

ctx._source.a = "foobar"

这会将字符串“foobar”分配给字段“a”。不过,您可以做的不仅仅是简单的分配。查看文档以获取更多详细信息和示例:

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

也可以使用一个脚本更新多个字段。您可以使用分号来分隔不同的 MVEL 指令。例如:

ctx._source.a = "foo"; ctx._source.b = "bar"

【讨论】:

    【解决方案2】:

    在 Elastic 搜索中有一个更新 Java API。看下面的代码

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

    这里,赋值变量包含对象值,响应数组变量包含数据列表。

    【讨论】:

      【解决方案3】:

      您可以使用以下代码使用 spring java 客户端执行相同的操作。我还列出了代码中使用的依赖项。

      import org.elasticsearch.action.update.UpdateRequest;
      
      import org.elasticsearch.index.query.QueryBuilder;
      
      import org.springframework.data.elasticsearch.core.query.UpdateQuery;
      
      import org.springframework.data.elasticsearch.core.query.UpdateQueryBuilder;
      
      private UpdateQuery updateExistingDocument(String Id) {
          // Add updatedDateTime, CreatedDateTime, CreateBy, UpdatedBy field in existing documents in Elastic Search Engine
          UpdateRequest updateRequest = new UpdateRequest().doc("UpdatedDateTime", new Date(), "CreatedDateTime", new Date(), "CreatedBy", "admin", "UpdatedBy", "admin");
      
          // Create updateQuery
          UpdateQuery updateQuery = new UpdateQueryBuilder().withId(Id).withClass(ElasticSearchDocument.class).build();
          updateQuery.setUpdateRequest(updateRequest);
      
          // Execute update
           elasticsearchTemplate.update(updateQuery);
      }
      

      【讨论】:

        【解决方案4】:
         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();
        

        【讨论】:

        • 欢迎来到stackoverflow。通过添加代码说明可以改进您的答案。
        猜你喜欢
        • 2021-05-16
        • 2020-08-10
        • 1970-01-01
        • 2018-10-13
        • 1970-01-01
        • 2016-08-11
        • 1970-01-01
        • 1970-01-01
        • 2013-03-02
        相关资源
        最近更新 更多