美文网首页Elastic...
ElasticSearch _update_by_query

ElasticSearch _update_by_query

作者: 青山淼淼 | 来源:发表于2020-10-22 00:05 被阅读0次
    根据查询条件进行数据更新

    UPDATE job_call SET admin_id = 0 WHERE admin_id = 283;

    kibana.png

    1.其中红色框的位置为query的内容,对应为SQL语句中的WHERE admin_id = 283
    2.划红色线的位置为修改内容,对应SQL中的SET admin_id = 0。如果是更新多个字段source的值则为

    ctx._source['admin_id'] = 0;ctx._source['time'] = 1603282909;
    
    • 注:source也可以为传递参数的形式进行更新↓
    {
        "source": "ctx._source.admin_id = params.admin_id",
        "params": {
          "admin_id": 0
    }
    
    为已存在索引新增字段给默认值

    为不含有sort_time的数据赋值为这条数据update_time
    MySQL就类似于:UPDATE zhaogong_resume SET sort_time = update_time WHERE sort_time is null ;

    POST /zhaogong_resume/_update_by_query

    {
      "query": {
        "bool": {
          "must_not": [
            {
              "exists": {
                "field": "sort_time"
              }
            }
          ]
        }
      },
      "script": {
        "source": "ctx._source['sort_time'] = ctx._source['update_time']"
      }
    }
    
    计算查询更新(script

    这是一个简单的script查询

    {
      "query": {
        "bool": {
          "must": [
            {
              "script": {
                "script": {
                  "source": "doc['sort_time'].value - doc['time'].value == 19992561"
                }
              }
              //....这里可多个script同级
            }
          ]
        }
      }
    }
    

    假设MySQL语句为:update zhaogong_resume set username = 'liubo' where (sort_time - time = 19992561)

    POST /zhaogong_resume/_update_by_query

    {
      "query": {
        "bool": {
          "must": [
            {
              "script": {
                "script": {
                  "source": "doc['sort_time'].value- doc['time'].value == 19992561"
                }
              }
            }
          ]
        }
      },
      "script": {
        "source": "ctx._source.username = params.username",
        "params": {
          "username": "liubo"
        }
      }
    }
    
    ElasticSearch对 script结构体定义变量,对变量进行操作。_update_by_query
    {
        "query": {
            "bool": {
                "must": [{
                    "term": {
                        "id": 1273
                    }
                }]
            }
        },
        "script": {
            "source": "def province_array = [ctx._source['province']] ;ctx._source['province'] = province_array; def city_array = [ctx._source['city']] ;ctx._source['city_arr'] = city_array;"
    
        }
    }
    

    相关文章

      网友评论

        本文标题:ElasticSearch _update_by_query

        本文链接:https://www.haomeiwen.com/subject/jmgemktx.html