0

I have tried to find the answer of this question but couldn't find. The scenario is: I have to get one extra field which is the calculation of two fileds in the elastic search query result. For this purpose I am using script_fields.

"script_fields": {
      "result": {
        "script": "doc['feild1'].value / doc['field2'].value"
      }
    }

The query is working fine, and I am getting the correct result. But now I have to implement the query in Java, I couldn't find any way to implement script_fields in Java.

Can somebody please help ?

1 Answer 1

1

Very simply using the addScriptField() method like this:

SearchResponse response = client().prepareSearch()
    .setQuery(matchAllQuery())
    .addScriptField("result", new Script(ScriptType.INLINE, "groovy", "doc['field1'].value / doc['field2'].value", Collections.emptyMap()))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your response, but I am using SearchResult API,
What do you mean by "SearchResult API"? Please update your question with the current code you have

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.