ElasticSearch _update_by_query

根据查询条件进行数据更新

UPDATE job_call SET admin_id = 0 WHERE admin_id = 283;

ElasticSearch _update_by_query_第1张图片

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;"

    }
}



作者:青山淼淼
链接:https://www.jianshu.com/p/c57404a58626
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

你可能感兴趣的:(elasticsearch,大数据)