I am working on a project where we need to show distance the listings are from a user location. To show the distance, when lat/lon is given in the input, we calculate distance using a script field called "distance"
"script_fields" : {
"distance" : {
"script" : {
"lang": "painless",
"source": "Math.round((doc['location'].planeDistanceWithDefault(params.lat, params.lon, 0) * 0.001) * 100.0) / 100.0",
"params" : {
"lat" : -33.8152,
"lon" : 151.0012
}
}
}
}
However, for relevancy search, we want to rank the nearest listing higher and we are calculating the distance again inside the function score query. This is inefficient. I have searched the internet to find a solution but no luck.
Is there any way to reuse a script field in the other parts of the query or filter or sorting?
Full Query:
GET /listings/_doc/_search
{
"_source" : true,
"query": {
"function_score": {
"score_mode": "sum",
"query": { "match": {
"source_id": 1
} },
"functions": [
{
"script_score": {
"script": {
"params": {
"dealer": {"a": 5, "b": 6},
"photo": {"a": 4, "b": 5},
"location": {"lat": -33.8152, "lon": 151.0012}
},
"source": "(doc['location'].planeDistanceWithDefault(params.location.lat, params.location.lon, 1000) * 0.001 < 25 ? 200000 : 0) + (doc['is_dealer'].value == 1 ? Math.pow(params.dealer.a, params.dealer.b) : 0) + (doc['hasPhoto'].value == 1 ? Math.pow(params.photo.a, params.photo.b) : 0)"
}
}
}
]
}
},
"script_fields" : {
"distance" : {
"script" : {
"lang": "painless",
"source": "Math.round((doc['location'].planeDistanceWithDefault(params.lat, params.lon, 0) * 0.001) * 100.0) / 100.0",
"params" : {
"lat" : -33.8152,
"lon" : 151.0012
}
}
}
}
}