0

I would like to dynamically calculate some values and retrieve the calculated values along with the original fields instead of only calculated. Say, there are fields "userId", "value". Additionally, I would like to calculate "additionalParameter". In order to do that, the following simple script has been added:

"query": {
    "match": {
            "userId": "1161851"
        }
  },
  "script_fields": {
    "additionalParameter": {
      "script": {
        "lang": "painless",
        "source": "doc['value'] * factor",
        "params": {
        "factor": 2.0
    }
      }
    }
  }

When the above query is executed, however, I get only additionalParameter:

{
        "_index" : "test_index",
        "_type" : "doc",
        "_id" : "dd57ba66-a31a-45f1-9948-18ff4fb27dc1",
        "_score" : 3.0488422,
        "fields" : {
          "additionalParameter" : [
            10.0
          ]
        }
      },
...

Does anyone know what is the right way to append calculated values to original fields and retrieve all?

1 Answer 1

1

Add a _source in the query

{
  "stored_fields": [
    "_source"
  ],
  "query": {
    "match": {
            "userId": "1161851"
        }
  },
  "script_fields": {
    "additionalParameter": {
      "script": {
        "lang": "painless",
        "source": "doc['value'] * factor",
        "params": {
        "factor": 2.0
    }
      }
    }
  }
}
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.