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

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

Incrementing a string field count in Kibana

问题

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

  1. POST /posts/_update/<id>
  2. {
  3. "script": {
  4. "source": "ctx._source.likeCount++"
  5. }
  6. }
英文:

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.

  1. POST /posts/_update/&lt;id&gt;
  2. {
  3. &quot;script&quot; : {
  4. &quot;source&quot;: &quot;ctx._source.likeCount++&quot;
  5. }
  6. }

答案1

得分: 0

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

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

You can parse the number via integer.ParseInt:

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

答案2

得分: 0

Integer.toString worked. Making it dynamic:

  1. POST /posts/_update/<id>
  2. {
  3. "script": {
  4. "source": "ctx._source.likeCount = Integer.toString(Integer.parseInt(ctx._source.likeCount)+params.newValue);",
  5. "lang": "painless",
  6. "params" : {
  7. "newValue" : 1
  8. }
  9. }
  10. }
英文:

It is working with below code Integer.toString worked.

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

Making it dynamic

  1. POST /posts/_update/&lt;id&gt;
  2. {
  3. &quot;script&quot;: {
  4. &quot;source&quot;: &quot;ctx._source.likeCount = Integer.toString(Integer.parseInt(ctx._source.likeCount)+params.newValue);&quot;,
  5. &quot;lang&quot;: &quot;painless&quot;,
  6. &quot;params&quot; : {
  7. &quot;newValue&quot; : 1
  8. }
  9. }
  10. }

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:

确定