Elasticsearch 根据搜索查询更新整个“_source”字段

Abh*_*h S 2 elasticsearch elasticsearch-java-api

"_source": {
         "id": "5b1676493d21784208c36041",
         "label": "name",
         "properties": {
           "name": "patrick"
         },
         "updatedAt": 1528259039542
       }
Run Code Online (Sandbox Code Playgroud)

我想根据id(而不是_id)使用新文档更新此文档。

像这样的东西:

    "_source": {
             "dataSource": "ELASTIC",
             "entity": "vertices",
             "label": "vertices",
             "id": "5b1676493d21784208c36041",
             "properties": {
                     "name": "patrick"
                  },
             "updatedAt": 1528259039542
           }
Run Code Online (Sandbox Code Playgroud)

elasticsearch版本:6.2,ES Java api:6.2

Val*_*Val 6

您可以使用以下方法实现您想要的你可以使用update by query API,基本上是这样的:

POST index/_update_by_query
{
  "query": {
    "match": {
      "id": "5b1676493d21784208c36041"
    }
  },
  "script": {
    "source": "ctx._source = params",
    "params": {
       "dataSource": "ELASTIC",
       "entity": "vertices",
       "label": "vertices"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

更新:使用 Java API

Map<String, String> params = new HashMap<>();
params.put("dataSource", "ELASTIC");
params.put("entity", "vertices");
params.put("label", "vertices");

UpdateByQueryRequestBuilder updateByQuery = UpdateByQueryAction.INSTANCE.newRequestBuilder(client);
updateByQuery.source("index")
    .filter(QueryBuilders.matchQuery("id", "5b1676493d21784208c36041"))
    .size(1000)
    .script(new Script(ScriptType.INLINE, "painless", "ctx._source.putAll(params)", params));
BulkByScrollResponse response = updateByQuery.get();
Run Code Online (Sandbox Code Playgroud)

有关使用UpdateByQuery Java APIJava 高级 Rest 客户端的更多详细信息