在Kibana中增加字符串字段计数

huangapple go评论49阅读模式
英文:

Incrementing a string field count in Kibana

问题

在下面的查询中,likeCount 是一个字符串字段,但我需要对它执行增量操作。使用以下查询,它执行的是连接操作而不是增量操作。

POST /posts/_update/<id>
{
  "script": {
    "source": "ctx._source.likeCount++"
  }
}
英文:

In the below query likeCount is a string field, but I need to do a increment operation on it, with the below query, it is doing a concatenation operation instead of increment.

POST /posts/_update/&lt;id&gt;
{
  &quot;script&quot; : {
    &quot;source&quot;: &quot;ctx._source.likeCount++&quot;
  }
}

答案1

得分: 0

你可以通过 integer.ParseInt 解析数字:

POST /posts/_update/&lt;id&gt;
{
  "script" : {
    "source": "ctx._source.likeCount = (Integer.parseInt(ctx._source.likeCount) + 1).toString()"
  }
}
英文:

You can parse the number via integer.ParseInt:

POST /posts/_update/&lt;id&gt;
{
  &quot;script&quot; : {
    &quot;source&quot;: &quot;ctx._source.likeCount = (Integer.parseInt(ctx._source.likeCount) + 1).toString()&quot;
  }
}

答案2

得分: 0

Integer.toString worked. Making it dynamic:

POST /posts/_update/<id>
{
  "script": {
    "source": "ctx._source.likeCount = Integer.toString(Integer.parseInt(ctx._source.likeCount)+params.newValue);",
    "lang": "painless",
    "params" : {
      "newValue" : 1
    }
  }
}
英文:

It is working with below code Integer.toString worked.

POST /posts/_update/&lt;id&gt;
{
  &quot;script&quot;: {
    &quot;source&quot;: &quot;ctx._source.likeCount = Integer.toString(Integer.parseInt(ctx._source.likeCount)+1);&quot;,
    &quot;lang&quot;: &quot;painless&quot;
  }
}

Making it dynamic

POST /posts/_update/&lt;id&gt;
{
  &quot;script&quot;: {
    &quot;source&quot;: &quot;ctx._source.likeCount = Integer.toString(Integer.parseInt(ctx._source.likeCount)+params.newValue);&quot;,
    &quot;lang&quot;: &quot;painless&quot;,
    &quot;params&quot; : {
      &quot;newValue&quot; : 1
    }
  }
}

huangapple
  • 本文由 发表于 2023年7月24日 16:06:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752497.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定