Reputation: 24659
I would like to use the java client API in order to return a computed field in the response of documents.
Specifically, I want a user to pass in a geopoint and use that geopoint in order to compute the distance between that geopoint (passed in the query) and the documents' geopoints (stored in the ES index).
I understand it is easily feasible using a script field as follows:
curl -XGET 'http://127.0.0.1:9200/geonames/_search?pretty=1' -d '
{
"script_fields" : {
"distance" : {
"params" : {
"lat" : 2.27,
"lon" : 50.3
},
"script" : "doc[\u0027location\u0027].distanceInKm(lat,lon)"
}
}
}
The above is quoted from the following SO post: https://stackoverflow.com/a/9309674/536299
Now how do you go about doing this with the java API or Spring Data ES?
Upvotes: 0
Views: 849
Reputation: 16335
You can use the below to convert it to java api
import org.elasticsearch.action.search.SearchRequestBuilder;
String script = "doc[\u0027location\u0027].distanceInKm(lat,lon)";
Map<String, Object> params = ...
SearchRequestBuilder esRequestBuilder = new SearchRequestBuilder(<ES_CLIENT>);
esRequestBuilder.addScriptField("distance", script, params);
Upvotes: 1