mathkibanaamazon-opensearch

Incrementing a string field count in Kibana


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++"
  }
}

Solution

  • 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
        }
      }
    }