Reputation: 12910
I have a GET
request, that is matching query string. It is searching within address strings and now returns solid and relevant results from an index.
Now I'd like to prioritise results by distance, so first, relevant strings are returned and ordered by closest geo_point parameter.
Putting the sort
into the same level, right after the query
parameter actually does not return hits sorted by distance. It returns weird results and it is definitely not what I want.
This is the mapping I use:
{
"location": {
"properties": {
"address": {
"type": "string"
},
"gps": {
"type": "geo_point"
}
}
}
}
The request I am doing now is:
GET /locations/location/_search
{
"query": {
"match" : {
"address" : {
"query": "Churchill Av"
}
}
},
"sort": [
{
"_geo_distance": {
"gps": {
"lat": 51.358599,
"lon": 0.531964
},
"order": "asc",
"unit": "km",
"distance_type": "plane"
}
}
]
}
I know that the best way would be gettting the results by match first and then sorting those few results by distance, so the geo-distance calculation is not too expensive.
I tried this question, but it didn't help.
EDIT: I need to mention, that I store geo_point data in my index like this:
"_source": {
"address" : "Abcdef, ghijk, lmnoprst"
"gps": [
51.50,
1.25
]
}
QUESTION: How to set the the geo distance sorting / filter, so results are sorted after the match query, ordered by closest geo_point parameter?
Upvotes: 1
Views: 967
Reputation: 12910
EDIT:
I realised that as the geo_point data is stored as an indexed array and that means the values are as [Lon, Lat]
, and not [Lat, Lon]
as I expected, it is unable to search within _geo_distance pattern of associative array:
{
"lat" : Lat,
"lon" : Long
}
From elasticsearch.co Docs:
Please note that string geo-points are ordered as lat,lon, while array geo-points are ordered as the reverse: lon,lat.
So the correct sort notation in this manner is
"_geo_distance": {
"gps": [51.358599,0.531964],
}
OR
"_geo_distance": {
"gps": {
"lat": 0.531964,
"lon": 51.358599
}
}
... because I store my geo_point data as [LON, LAT] and not [LAT, LON], as I had thought.
Now it work as expected. My problem is now, that I should reindex data with reverse order of latitude/longitude within the geo_point array.
I hope this remark could help someone else.
Upvotes: 3