根据查询条件进行数据更新
UPDATE job_call SET admin_id = 0 WHERE admin_id = 283;
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;"
}
}
网友评论