0

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++"
  }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105

2 Answers2

0

You can parse the number via integer.ParseInt:

POST /posts/_update/<id>
{
  "script" : {
    "source": "ctx._source.likeCount = (Integer.parseInt(ctx._source.likeCount) + 1).toString()"
  }
}
Milan Gatyás
  • 2,509
  • 1
  • 17
  • 23
0

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
    }
  }
}
Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105