首先声明版本为ES 6.0。
index中有很多文档,要更新这些文档中符合某个条件的所有documents,可以使用ES的_update_by_query的及脚本方式完成:POST请求:http://localhost:9200/indexName/typeName/_update_by_query
{ "script": {"source":"ctx._source['user_name']='LsiuLi';ctx._source['assignedto_id']='123';"}, "query": {"term": {"user_id": 60}} }
执行上面的query,意思是把当前index/type下的所有符合user_id为 60的document,把这些document的user_name字段全部修改成LsiuLi,把assignedto_id 改成123。
如果增加数组元素:
http://localhost:9200/1909_user/user/15670260/_update
{ "script": { "lang": "painless", "source":"ctx._source['field_mult_value_7917'].add(params.hobby)", "params" : { "hobby" : "c" } } }
script 删除index中的field:
http://localhost:9200/1542_case/case/_update_by_query?wait_for_completion=false&conflicts=proceed
{ "script": {"source":"ctx._source.remove('user_field_email_5613')"} }wait_for_completion=false可以直接返回http请求,哪怕index中的field还没删完。
参考链接:
https://www.elastic.co/guide/en/elasticsearch/reference/6.0/modules-scripting-using.html
https://www.cnblogs.com/rodge-run/p/7760308.html