我已经提到了以下查询。(
Bulk update查询和
per document更新查询)
核心逻辑在中
script,这在两个查询中都相同。
我建议您按照逻辑进行自我解释。基本上,脚本会遍历嵌套文档,并根据您指定的条件对其进行
count相应的更新。
批量更新-使用 __update_by_query_
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 }}}每个文档更新-使用 文档ID
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 }}}希望对您有所帮助,如有任何疑问,请与我们联系!



