Elasticsearch根据搜索查询更新整个_source字段 [英] Elasticsearch update the whole `_source` field based on search query

查看:1041
本文介绍了Elasticsearch根据搜索查询更新整个_source字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

"_source": {
         "id": "5b1676493d21784208c36041",
         "label": "name",
         "properties": {
           "name": "patrick"
         },
         "updatedAt": 1528259039542
       }

我想基于id(不是_id)用新文档更新此文档.

I want to update this document based on id (not _id) with a new document.

类似这样的东西:

    "_source": {
             "dataSource": "ELASTIC",
             "entity": "vertices",
             "label": "vertices",
             "id": "5b1676493d21784208c36041",
             "properties": {
                     "name": "patrick"
                  },
             "updatedAt": 1528259039542
           }

elasticsearch版本:6.2,ES Java API:6.2

elasticsearch version: 6.2, ES Java api: 6.2

推荐答案

您可以使用

You can achieve what you want using the update by query API, basically like this:

POST index/_update_by_query
{
  "query": {
    "match": {
      "id": "5b1676493d21784208c36041"
    }
  },
  "script": {
    "source": "ctx._source = params",
    "params": {
       "dataSource": "ELASTIC",
       "entity": "vertices",
       "label": "vertices"
    }
  }
}

更新:使用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();

有关使用 UpdateByQuery Java API 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆