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查询。