我已经提到了以下问题。 (Bulk update
查询和per document
更新查询)
核心逻辑在于script
,这在两个查询中都是相同的。
我建议您仔细检查逻辑,因为它是可以自我解释的。基本上,脚本会迭代嵌套文档,并根据您所说的条件,它会更新count
因此。
批量更新 - 使用按查询更新
POST <your_index_name>/_update_by_query
{
"query": {
"match_all": {}
},
"script": {
"lang": "painless",
"source": """
if(ctx._source.ref_counter.contains(params.cur_data)){
for(int i=0; i<ctx._source.ref_counter.size(); i++)
{
HashMap myKV = ctx._source.ref_counter.get(i);
if(myKV.get(params.key_ref)==params.key_ref_value){
myKV.put(params.key_count, params.key_count_value_new);
}
}
//ctx._source.ref_counter.add(params.new_data);
}""",
"params": {
"cur_data": { "ref_name": "test2", "count": 2},
"new_data": { "ref_name": "test2", "count": 22},
"key_ref": "ref_name",
"key_ref_value": "test2",
"key_count": "count",
"key_count_value_new": 80
}}
}
每个文档更新 - 使用文档编号
POST <your_index_name>/<your_mapping>/1/_update
{
"script": {
"lang": "painless",
"source": """
if(ctx._source.ref_counter.contains(params.cur_data)){
for(int i=0; i<ctx._source.ref_counter.size(); i++)
{
HashMap myKV = ctx._source.ref_counter.get(i);
if(myKV.get(params.key_ref)==params.key_ref_value){
myKV.put(params.key_count, params.key_count_value_new);
}
}
//ctx._source.ref_counter.add(params.new_data);
}""",
"params": {
"cur_data": { "ref_name": "test2", "count": 2},
"new_data": { "ref_name": "test2", "count": 22},
"key_ref": "ref_name",
"key_ref_value": "test2",
"key_count": "count",
"key_count_value_new": 80
}
}
}
我希望这对您有所帮助,如果您有任何疑问,请告诉我!