1. 程式人生 > >ElasticSearch根據匹配某個條件,區域性更新文件

ElasticSearch根據匹配某個條件,區域性更新文件

首先宣告版本為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