django-elasticsearch-dsl-drf
django-elasticsearch-dsl-drf copied to clipboard
Return distance in elasticsearch results?
Is there any API I can use to implement this in django-elasticsearch-dsl-drf?
https://stackoverflow.com/questions/9295118/return-distance-in-elasticsearch-results
@playma:
Look, you work with backends. So, you should make a custom backend for it and included in the list of backends. Generic solution is also possible and might be handy, so I leave this one open as an improvement.
If you need this urgently and somehow don't come up with a working solution, you can speed it up by sponsoring this feature. Contact me by email for the details.
Hi,
I want to return distance in my search results. Normally, when the result is order by geopoint, elasticsearch return the distance but it seems that this is not the case with django-elasticsearch-dsl.
So I tried to use script_fields:
search = search.query({
"term" : {
"is_visible": 'true'
}
})
search = search.filter({
"geo_distance" : {
"location" : {
"lat": query_latitude, "lon": query_longitude
},
"unit" : "km",
"distance" : distance
}
})
search = search.sort({
"_geo_distance" : {
"location" : {
"lat": query_latitude, "lon": query_longitude
},
"order" : "asc",
"unit" : "km",
"distance_type" : "arc"
}
})
search = search.source(True).script_fields(distance={
'script':{
'inline':"doc['location'].arcDistance(params.lat, params.lon) * 0.001",
'params': {
'lat':item.latitude,
'lon':item.longitude
}
}
})
response = search[start:end].execute()
paginator = DSEPaginator(response, 6)
try:
posts = paginator.page(page)
except PageNotAnInteger:
posts = paginator.page(1)
except EmptyPage:
posts = paginator.page(paginator.num_pages)
return posts
The query is accepted by elasticsearch but there is not posts.distance. Do you have a solution ?
Thank you.
It should be solved with script fields. We don't yet have a script backend. I'm considering implementing it. Implementation speed can be boosted if sponsored.