ElasticSearch 条件更新 删除

ElasticSearch根据匹配某个条件,局部更新文档

首先声明版本为ES 6.0。

index中有很多文档,要更新这些文档中符合某个条件的所有documents,可以使用ES的_update_by_query的及脚本方式完成:POST请求:http://localhost:9200/indexName/typeName/_update_by_query

  1. {

  2. "script": {"source":"ctx._source['user_name']='LsiuLi';ctx._source['assignedto_id']='123';"},

  3. "query": {"term": {"user_id": 60}}

  4. }

执行上面的query,意思是把当前index/type下的所有符合user_id为 60的document,把这些document的user_name字段全部修改成LsiuLi,把assignedto_id 改成123。

如果增加数组元素:

http://localhost:9200/1909_user/user/15670260/_update

  1. {

  2. "script": {

  3. "lang": "painless",

  4. "source":"ctx._source['field_mult_value_7917'].add(params.hobby)",

  5. "params" : {

  6. "hobby" : "c"

  7. }

  8. }

  9.  
  10. }

script 删除index中的field:

http://localhost:9200/1542_case/case/_update_by_query?wait_for_completion=false&conflicts=proceed

  1. {

  2. "script": {"source":"ctx._source.remove('user_field_email_5613')"}

  3. }

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,添加数据

  1. $ curl -XPOST "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"  -H "Content-Type: application/json" -d ' 
  2.     "id" : 3, 
  3.     "username" :  "测试测试", 
  4.     "description" :  "测试测试" 
  5. }'  

2,更新数据

2.1,部分数据更新

  1. $ curl -XPOST 'localhost:9200/ik_v2/chinese/3/_update?pretty' -H "Content-Type: application/json" -d '{ 
  2.     "doc" : { 
  3.             "username" : "testtest" 
  4.         } 
  5.     } 
  6. }'  
  7.   
  8. $ curl -XGET "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"  
  9. {  
  10.   "_index" : "ik_v2",  
  11.   "_type" : "chinese",  
  12.   "_id" : "3",  
  13.   "_version" : 1,  
  14.   "found" : true,  
  15.   "_source" : {  
  16.     "id" : 3,  
  17.     "username" : "testtest",  //部分更新了  
  18.     "description" : "测试测试"  
  19.   }  
  20. }  

2.2,全部更新

  1. curl -XPOST "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"  -H "Content-Type: application/json" -d ' 
  2.     "id" : 4, 
  3.     "username" :  "111111111", 
  4.     "description" :  "222222222" 
  5. }'  
  6.   
  7. //id为3的数据全部更新了  
  8. $ curl -XGET "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"  
  9. {  
  10.   "_index" : "ik_v2",  
  11.   "_type" : "chinese",  
  12.   "_id" : "3",  
  13.   "_version" : 1,  
  14.   "found" : true,  
  15.   "_source" : {  
  16.     "id" : 4,  
  17.     "username" : "111111111",  
  18.     "description" : "222222222"  
  19.   }  
  20. }  

2.3,拼接更新

  1. $ curl -XPOST 'localhost:9200/ik_v2/chinese/3/_update?pretty' -H "Content-Type: application/json" -d '{ 
  2.     "script" : { 
  3.         "inline" : "ctx._source.description += params.content", //因为description是string型,所以是拼 
  4.         "params" : { 
  5.             "content" : 333 
  6.         } 
  7.     } 
  8. }'  
  9.   
  10. $ curl -XPOST 'localhost:9200/ik_v2/chinese/3/_update?pretty' -H "Content-Type: application/json" -d '{ 
  11.     "script" : { 
  12.         "inline" : "ctx._source.id += params.num",   //因为id是int型,所以是加 
  13.         "params" : { 
  14.             "num" : 2 
  15.         } 
  16.     } 
  17. }'  
  18.   
  19. $ curl -XGET "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"  
  20. {  
  21.   "_index" : "ik_v2",  
  22.   "_type" : "chinese",  
  23.   "_id" : "3",  
  24.   "_version" : 3,  
  25.   "found" : true,  
  26.   "_source" : {  
  27.     "id" : 6,   //加了2  
  28.     "username" : "111111111",  
  29.     "description" : "222222222333"   //拼了333  
  30.   }  
  31. }  

2.4,添加字段,并更新数据

  1. //添加一个字段为sex值为1  
  2. $ curl -XPOST 'localhost:9200/ik_v2/chinese/3/_update?pretty' -H "Content-Type: application/json" -d '{ 
  3.  "script" : "ctx._source.sex = 1" 
  4. }'  
  5.   
  6. //删除sex这个字段  
  7. $ curl -XPOST 'localhost:9200/ik_v2/chinese/3/_update?pretty' -H "Content-Type: application/json" -d '{ 
  8.  "script" : "ctx._source.remove(\"sex\")" 
  9. }'  

在这里要注意,用这个方法,mapping结构会改变。

注:以上的更新操作都是单条数据更新

2.5,多条数据更新

  1. $ curl -XPOST 'localhost:9200/ik_v2/test/_update_by_query?pretty' -H "Content-Type: application/json" -d '{ 
  2. >  "query": { 
  3. >         "bool": { 
  4. >             "should": { 
  5. >                 "match": { 
  6. >                     "username": "高铁" 
  7. >                 } 
  8. >             } 
  9. >         } 
  10. >     }, 
  11. >     "script" : { 
  12. >         "inline" : "ctx._source.username = \"666666\"" 
  13. >     } 
  14. > }'  
  15. {  
  16.   "took" : 6,  
  17.   "timed_out" : false,  
  18.   "total" : 3,  
  19.   "updated" : 2,  //更新了二条  
  20.   "deleted" : 0,  
  21.   "batches" : 1,  
  22.   "version_conflicts" : 0,  
  23.   "noops" : 0,  
  24.   "retries" : {  
  25.     "bulk" : 0,  
  26.     "search" : 0  
  27.   },  
  28.   "throttled_millis" : 0,  
  29.   "requests_per_second" : -1.0,  
  30.   "throttled_until_millis" : 0,  
  31.   "failures" : [ ]  
  32. }  

注意,这个条件字段,最好不要用分词字段,因为不可控。上面我只是为了测试用。

3,删除数据

3.1,单条删除

  1. $ curl -XDELETE "http://127.0.0.1:9200/ik_v2/chinese/3?pretty"  

3.2,多条数据删除

  1. $ curl -XPOST 'http://127.0.0.1:9200/ik_v2/_delete_by_query?pretty' -H "Content-Type: application/json" -d '{ 
  2. > "query": { 
  3. >         "term": { 
  4. >             "username": "666666" 
  5. >         } 
  6. >     } 
  7. > }'  
  8. {  
  9.   "took" : 6,  
  10.   "timed_out" : false,  
  11.   "total" : 3,  
  12.   "deleted" : 2,  //删除了二条  
  13.   "batches" : 1,  
  14.   "version_conflicts" : 0,  
  15.   "noops" : 0,  
  16.   "retries" : {  
  17.     "bulk" : 0,  
  18.     "search" : 0  
  19.   },  
  20.   "throttled_millis" : 0,  
  21.   "requests_per_second" : -1.0,  
  22.   "throttled_until_millis" : 0,  
  23.   "failures" : [ ]  
  24. }  

注意,这个条件字段,最好不要用分词字段,因为不可控。

4,查询

  1. $ curl -XPOST "http://127.0.0.1:9200/ik,ik_v2/chinese/_search?pretty"  -H "Content-Type: application/json"  -d ' 
  2.     "query": { 
  3.         "multi_match": { 
  4.             "query": "中国高铁", 
  5.             "fields": [ "username", "description"] 
  6.         } 
  7.     } 
  8. '  

查询的操作,非常多,后面会单独的详细说。查询总的来说,支持多索引多字段查询。新版es不支持一个索引多个mapping,老版还支持多mapping查询。

猜你喜欢

转载自blog.csdn.net/jacke121/article/details/81113443