I need to return the distance for multiple geo points per document in an Elasticsearch array. As of now, my results only return one distance calculated for the array.
I started with the code from the following StackOverflow question: Return distance in elasticsearch results?
My elasticsearch query body contains this:
{
"stored_fields" : [ "_source" ],
"script_fields" : {
"distance" : {
"script" : {
"inline": "doc['locations.facility.address.coordinates'].arcDistance(params.lat,params.lon) * 0.001",
"lang": "painless",
"params": {
"lat": 2.27,
"lon": 50.3
}
}
}
}
}
And, my Elasticsearch source documents, when returned, resemble this. (Note that locations is an array.)
"locations": [
{
"facility": {
"address": {
"country_code": "US",
"city": "San Diego",
"coordinates": {
"lon": -117.165,
"lat": 32.8408
},
"country_name": "United States",
"state_province": "California",
"postal_code": "92123"
}
}
},
{
"facility": {
"address": {
"country_code": "US",
"city": "Tampa",
"coordinates": {
"lon": -82.505,
"lat": 28.0831
},
"country_name": "United States",
"state_province": "Florida",
"postal_code": "33613"
}
}
}
]
Currently, my results return something similar to this:
"fields": {
"distance": [
13952.518249603361
]
}
But in the distance array, I need to return a value for each entry in 'locations'.