当前位置:   article > 正文

ES: update by query_es updatebyquery

es updatebyquery

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": 18
}

POST test/_doc/3
{
  "name": "旺仔",
  "age": 1
}

POST test/_doc/4
{
  "name": "李四"
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

1、修改一个字段的值

# 修改李四的年龄为44
POST test/_update_by_query
{
  "script": {
    "source": "ctx._source.age = 44",
    "lang": "painless"
  },
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field": "age"
          }
        }
      ]
    }
  }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

在这里插入图片描述

2、 给es里某个字段增加一个子类型,要求之前的数据也能被查询到

修改mapping,添加一个子字段

POST test/_mapping
{
  "properties": {
    "name": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        },
        "ik_smart": {
          "type": "text",
          "analyzer": "ik_smart"
        }
      }
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

插入一条新的数据

PUT test/_doc/5
{
  "name": "王五",
  "age": 35
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

查询 李四,王五,发现查不到李四



GET test/_search
{
  "query": {
    "match": {
      "name.ik_smart": "李四"
    }
  }
}

GET test/_search
{
  "query": {
    "match": {
      "name.ik_smart": "王五"
    }
  }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

因为李四是 更改mapping之前插入,新增字段没有在老数据上生效,导致查询不出

为了之前的数据也能被查询到,我们通过 _update_by_query

POST test/_update_by_query
  • 1

结果可以查询
在这里插入图片描述

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号