篇首语:本文由小编为大家整理,主要介绍了ES: update by query相关的知识,希望对你有一定的参考价值。
文章目录
- _update_by_query 的应用场景
- 造数据
- 1、修改一个字段的值
- 2、 给es里某个字段增加一个子类型,要求之前的数据也能被查询到
es 版本为7.9.3
_update_by_query 的应用场景
- 1、修改一个字段的值
- 给es里某个字段增加一个子类型,要求之前的数据也能被查询到
造数据
POST test "mappings" : "properties" : "name" : "type" : "text", "fields" : "keyword" : "type" : "keyword", "ignore_above" : 256 POST test/_doc/1 "name": "chb", "age": "20"POST test/_doc/2 "name": "ling", "age": 18POST test/_doc/3 "name": "旺仔", "age": 1POST test/_doc/4 "name": "李四"
1、修改一个字段的值
# 修改李四的年龄为44POST test/_update_by_query "script": "source": "ctx._source.age = 44", "lang": "painless" , "query": "bool": "must_not": [ "exists": "field": "age" ]
2、 给es里某个字段增加一个子类型,要求之前的数据也能被查询到
修改mapping,添加一个子字段
POST test/_mapping "properties": "name": "type": "text", "fields": "keyword": "type": "keyword", "ignore_above": 256 , "ik_smart": "type": "text", "analyzer": "ik_smart"
插入一条新的数据
PUT test/_doc/5 "name": "王五", "age": 35
查询 李四,王五,发现查不到李四
GET test/_search "query": "match": "name.ik_smart": "李四" GET test/_search "query": "match": "name.ik_smart": "王五"
因为李四是 更改mapping之前插入,新增字段没有在老数据上生效,导致查询不出
为了之前的数据也能被查询到,我们通过 _update_by_query
POST test/_update_by_query
结果可以查询
以上是关于ES: update by query的主要内容,如果未能解决你的问题,请参考以下文章