elasticsearch:如何将整数字段的输入值和现有值相加



我在ES中有一个文档,如下所示:

{
"noteCount": 1,
....
}

还有另一个类似的传入文档:

{
"noteCount": 2,
....
}

在更新ES文档时,对于noteCount对象,我需要将其保存为3,即1+2。有什么办法让它发挥作用吗?

您可以在更新查询中使用无痛脚本。

PUT /test/_doc/1?pretty
{
"noteCount" : 1,
"tags" : ["red"]
}
POST /test/_update/1?pretty
{
"script" : {
"source": "ctx._source.noteCount += params.count",
"lang": "painless",
"params" : {
"count" : 1
}
}
}
GET /test/_doc/1

最新更新