英文:
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/<id>
{
  "script" : {
    "source": "ctx._source.likeCount++"
  }
}
答案1
得分: 0
你可以通过 integer.ParseInt 解析数字:
POST /posts/_update/<id>
{
  "script" : {
    "source": "ctx._source.likeCount = (Integer.parseInt(ctx._source.likeCount) + 1).toString()"
  }
}
英文:
You can parse the number via integer.ParseInt:
POST /posts/_update/<id>
{
  "script" : {
    "source": "ctx._source.likeCount = (Integer.parseInt(ctx._source.likeCount) + 1).toString()"
  }
}
答案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/<id>
{
  "script": {
    "source": "ctx._source.likeCount = Integer.toString(Integer.parseInt(ctx._source.likeCount)+1);",
    "lang": "painless"
  }
}
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
    }
  }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论