ElasticSearch _update_by_query

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

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

    }
}



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

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-01-12 15:58:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-12 15:58:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-12 15:58:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-12 15:58:03       18 阅读

热门阅读

  1. LNMP平台对接redis服务

    2024-01-12 15:58:03       25 阅读
  2. 力扣(leetcode)第541题反转字符串II(Python)

    2024-01-12 15:58:03       35 阅读
  3. [BUG] docker: unknown server OS: .

    2024-01-12 15:58:03       31 阅读
  4. 力扣labuladong一刷day59天动态规划

    2024-01-12 15:58:03       25 阅读
  5. SpringSecurity入门demo(四)授权

    2024-01-12 15:58:03       37 阅读
  6. AttributeError: ‘str‘ object has no attribute ‘spilt‘

    2024-01-12 15:58:03       26 阅读
  7. 实名核验、企业工商、生活常用等API分享

    2024-01-12 15:58:03       31 阅读