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
elasticsearch 資料 新增,更新,刪除,查詢
上篇文章說了,elasticsearch mapping欄位的增,刪,更新。如果把mapping的修改理解成對資料結構的修改,那這篇文章就可以理解成對資料的修改。
1,新增資料
- $ curl -XPOST "http://127.0.0.1:9200/ik_v2/chinese/3?pretty" -H "Content-Type: application/json" -d '
- {
- "id" : 3,
- "username" : "測試測試",
- "description" : "測試測試"
- }'
2,更新資料
2.1,部分資料更新
- $ curl -XPOST 'localhost:9200/ik_v2/chinese/3/_update?pretty' -H "Content-Type: application/json" -d '{
- "doc" : {
- "username" : "testtest"
- }
- }
- }'
- $ curl -XGET "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"
- {
- "_index" : "ik_v2",
- "_type" : "chinese",
- "_id" : "3",
- "_version" : 1,
- "found" : true,
- "_source" : {
- "id" : 3,
- "username" : "testtest", //部分更新了
- "description" : "測試測試"
- }
- }
2.2,全部更新
- curl -XPOST "http://127.0.0.1:9200/ik_v2/chinese/3?pretty" -H "Content-Type: application/json" -d '
- {
- "id" : 4,
- "username" : "111111111",
- "description" : "222222222"
- }'
- //id為3的資料全部更新了
- $ curl -XGET "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"
- {
- "_index" : "ik_v2",
- "_type" : "chinese",
- "_id" : "3",
- "_version" : 1,
- "found" : true,
- "_source" : {
- "id" : 4,
- "username" : "111111111",
- "description" : "222222222"
- }
- }
2.3,拼接更新
- $ curl -XPOST 'localhost:9200/ik_v2/chinese/3/_update?pretty' -H "Content-Type: application/json" -d '{
- "script" : {
- "inline" : "ctx._source.description += params.content", //因為description是string型,所以是拼
- "params" : {
- "content" : 333
- }
- }
- }'
- $ curl -XPOST 'localhost:9200/ik_v2/chinese/3/_update?pretty' -H "Content-Type: application/json" -d '{
- "script" : {
- "inline" : "ctx._source.id += params.num", //因為id是int型,所以是加
- "params" : {
- "num" : 2
- }
- }
- }'
- $ curl -XGET "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"
- {
- "_index" : "ik_v2",
- "_type" : "chinese",
- "_id" : "3",
- "_version" : 3,
- "found" : true,
- "_source" : {
- "id" : 6, //加了2
- "username" : "111111111",
- "description" : "222222222333" //拼了333
- }
- }
2.4,新增欄位,並更新資料
- //新增一個欄位為sex值為1
- $ curl -XPOST 'localhost:9200/ik_v2/chinese/3/_update?pretty' -H "Content-Type: application/json" -d '{
- "script" : "ctx._source.sex = 1"
- }'
- //刪除sex這個欄位
- $ curl -XPOST 'localhost:9200/ik_v2/chinese/3/_update?pretty' -H "Content-Type: application/json" -d '{
- "script" : "ctx._source.remove(\"sex\")"
- }'
在這裡要注意,用這個方法,mapping結構會改變。
注:以上的更新操作都是單條資料更新
2.5,多條資料更新
- $ curl -XPOST 'localhost:9200/ik_v2/test/_update_by_query?pretty' -H "Content-Type: application/json" -d '{
- > "query": {
- > "bool": {
- > "should": {
- > "match": {
- > "username": "高鐵"
- > }
- > }
- > }
- > },
- > "script" : {
- > "inline" : "ctx._source.username = \"666666\""
- > }
- > }'
- {
- "took" : 6,
- "timed_out" : false,
- "total" : 3,
- "updated" : 2, //更新了二條
- "deleted" : 0,
- "batches" : 1,
- "version_conflicts" : 0,
- "noops" : 0,
- "retries" : {
- "bulk" : 0,
- "search" : 0
- },
- "throttled_millis" : 0,
- "requests_per_second" : -1.0,
- "throttled_until_millis" : 0,
- "failures" : [ ]
- }
注意,這個條件欄位,最好不要用分詞欄位,因為不可控。上面我只是為了測試用。
3,刪除資料
3.1,單條刪除
- $ curl -XDELETE "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"
3.2,多條資料刪除
- $ curl -XPOST 'http://127.0.0.1:9200/ik_v2/_delete_by_query?pretty' -H "Content-Type: application/json" -d '{
- > "query": {
- > "term": {
- > "username": "666666"
- > }
- > }
- > }'
- {
- "took" : 6,
- "timed_out" : false,
- "total" : 3,
- "deleted" : 2, //刪除了二條
- "batches" : 1,
- "version_conflicts" : 0,
- "noops" : 0,
- "retries" : {
- "bulk" : 0,
- "search" : 0
- },
- "throttled_millis" : 0,
- "requests_per_second" : -1.0,
- "throttled_until_millis" : 0,
- "failures" : [ ]
- }
注意,這個條件欄位,最好不要用分詞欄位,因為不可控。
4,查詢
- $ curl -XPOST "http://127.0.0.1:9200/ik,ik_v2/chinese/_search?pretty" -H "Content-Type: application/json" -d '
- {
- "query": {
- "multi_match": {
- "query": "中國高鐵",
- "fields": [ "username", "description"]
- }
- }
- }
- '
查詢的操作,非常多,後面會單獨的詳細說。查詢總的來說,支援多索引多欄位查詢。新版es不支援一個索引多個mapping,老版還支援多mapping查詢。