Elasticsearch给所有记录新增一个字段:(1)字段是固定值 (2)字段是其他字段计算值
1. 固定值: _update_by_query加固定值字段
新增一个speaker_bak字段,值是'HAMLET'
方法1:
POST hamlet/_update_by_query
{
"script": {
"source": "ctx._source.speaker_bak='HAMLET'",
"lang": "painless"
},
"query": {
"match_all": {}
}
}现在我们删掉这个字段, 用pipeline再试一次
POST hamlet/_update_by_query
{
"script": {
"source": "ctx._source.remove('speaker_bak')",
"lang": "painless"
}
}扩展 如果存在字段 talked, 则将它的值增加increment;否则赋值为1:
POST hamlet/_update_by_query
{
"script": {
"lang": "painless",
"source": """
if(ctx._source.talked != null){
ctx._source.talked += params.increment;
} else {
ctx._source.talked = 1;
}
""",
"params": {
"increment": 3
}
}
}方法2:通过 pipeline
PUT _ingest/pipeline/addSpeakerBak
{
"description": "增加字段的pipeline",
"processors": [
{
"set": {
"field": "speaker_bak",
"value": "HAMLET"
}
}
]
}
POST hamlet/_update_by_query?pipeline=addSpeakerBak2. 新加一个计算值字段(通过其他字段)
新增一个entry_len字段,值是text_entry字段的长度
POST hamlet/_update_by_query
{
"script": {
"source": "ctx._source.entry_len=ctx._source.text_entry.length()",
"lang": "painless"
},
"query": {
"match_all": {}
}
}3.已有字段改名:旧字段改名
貌似只能通过pipeline
PUT _ingest/pipeline/renameField
{
"description": "旧字段改名",
"processors": [
{
"rename": {
"field": "text_entry",
"target_field": "textEntry"
}
}
]
}
POST hamlet/_update_by_query?pipeline=renameField
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用。你还可以使用@来通知其他用户。