es添加字段并赋值


添加字段即

1
2
3
4
PUT /index/_mapping
{
...
}
1
给es添加字段

这里主要通过script脚本.
并且在elasticsearch1.3后这个默认配置是被关掉的.

即在elasticsearch.yml中添加
script.inline=true
script.indexed=true
一开始我在true后面加了空格,配置不被识别,所以这里要注意

开启了script.line后通过update_by_query进行操作,这里注意以下写法已经过时

1
2
3
4
5
6
7
8
9

POST /index/_update_by_query?conflicts=proceed
{
"script":{
"lang":"painless",
"inline": "ctx._source.name='trhee'"
}
}

即inline这种写法已经过时. 现在主要用的是source

1
2
3
4
5
6
7
POST /index/_update_by_query?conflicts=proceed
{
"script":{
"lang":"painless",
"source": "ctx._source.name='trhee'"
}
}

还能加入条件判断

1
2
3
4
5
6
7
POST /index/_update_by_query?conflicts=proceed
{
"script":{
"lang":"painless",
"source": "if(ctx._source.name==''){ctx._source.name='trhee'}"
}
}

至此即可以实现对_source.name字段的数据进行默认赋值


Author: Kuiq Wang
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source Kuiq Wang !
  TOC