Reputation: 1377
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 ?
Upvotes: 0
Views: 1979
Reputation: 217554
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()))
Upvotes: 1