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

"_source": {

"id": "5b1676493d21784208c36041",

"label": "name",

"properties": {

"name": "patrick"

},

"updatedAt": 1528259039542

}

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

像这样:

    "_source": {

"dataSource": "ELASTIC",

"entity": "vertices",

"label": "vertices",

"id": "5b1676493d21784208c36041",

"properties": {

"name": "patrick"

},

"updatedAt": 1528259039542

}

elasticsearch版本:6.2,ES Java API:6.2

回答:

您基本上可以使用查询API更新来实现所需的功能,基本上是这样的:

POST index/_update_by_query

{

"query": {

"match": {

"id": "5b1676493d21784208c36041"

}

},

"script": {

"source": "ctx._source = params",

"params": {

"dataSource": "ELASTIC",

"entity": "vertices",

"label": "vertices"

}

}

}

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和Java高级Rest

客户端的更多详细信息

以上是 Elasticsearch根据搜索查询更新整个_source字段 的全部内容, 来源链接: utcz.com/qa/422719.html

回到顶部