0

Using Elasticsearch 7.*, trying to execute SQL query on an index 'com-prod':

GET /com-prod/_search
{
  "script_fields": {
    "test1": {
      "script": {
        "lang": "painless",
        "source": "params._source.ElapsedTime"
      }
    }
  }
}

It gives the output and below as one of the hit successfully:

"hits" : [
      {
        "_index" : "com-prod",
        "_type" : "_doc",
        "_id" : "abcd",
        "_score" : 1.0,
        "fields" : {
          "test1" : [
            "29958"
          ]
        }
      }

Now, I am trying to increment the ElapsedTime by 2, as below:

GET /com-prod/_search
{
  "script_fields": {
    "test2": {
      "script": {
        "lang": "painless",
        "source": "params._source.ElapsedTime + 2"
      }
    }
  }
}

But its actually adding number 2 to the output, as below:

"hits" : [
      {
        "_index" : "com-prod",
        "_type" : "_doc",
        "_id" : "abcd",
        "_score" : 1.0,
        "fields" : {
          "test2" : [
            "299582"
          ]
        }
      }

Please guide what could be wrong here, and how to get the output as 29960.

1 Answer 1

2

You are getting 299582, instead of 29960, because the ElapsedTime field is of string type ("29958"), so when you are adding 2 in this using script, 2 gets appended at the end (similar to concat two strings).

So, in order to solve this issue, you can :

  1. Create a new index, with updated mapping of the ElaspsedTIme field of int type, then reindex the data. Then you can use the same search query as given in the question above.

  2. Convert the string to an int type value, using Integer.parseInt()

GET /com-prod/_search
{
  "script_fields": {
    "test2": {
      "script": {
        "lang": "painless",
        "source": "Integer.parseInt(params._source.ElapsedTime) + 2"
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.