Reputation: 1
i have restaurants collection in my redis DB, the restaurant object stored in as bellow:
{
uid: "T8957Ixo1kRjtw3Nof3Ttase",
name: "Restaurant name",
address:
"751 S 10th St, Philadelphia, Pennsylvania 19140, United States",
location: "-78.15096902,38.94039013",
}
i wonder if it possible to return results sorted by distance
i tried to do FT.SEARCH index "@name:%%burger%% @location:[-75.153270 39.949655]" SORTBY location ASC
but it doesn't work at all, How can I achieve that by redisearch?
Upvotes: 0
Views: 575
Reputation: 702
Currently, this is only possible with FT.AGGREGATE
. You will have to use APPLY
(documentation here), and then sort by it.
Try something like:
FT.AGGREGATE index "@name:%%burger%%" APPLY "geodistance(@location, -75.153270, 39.949655)" AS distance SORTBY distance ASC
There are some other overloading for geodistance
in the documentation, you can use the one that suits your environment best.
In addition, you should take another look at the GEO filter syntax. Your query is invalid:
Geo filters
As of v0.21, it is possible to add geo radius queries directly into the query language with the syntax @field:[{lon} {lat} {radius} {m|km|mi|ft}]. This filters the result to a given radius from a lon,lat point, defined in meters, kilometers, miles or feet. See Redis' own GEORADIUS command for more details as it is used internally for that).
Radius filters can be added into the query just like numeric filters. For example, in a database of businesses, looking for Chinese restaurants near San Francisco (within a 5km radius) would be expressed as: chinese restaurant @location:[-122.41 37.77 5 km].
The query I suggested will work, but if you can give a relevant radius to look in, you might get better performance.
for example:
FT.AGGREGATE index "@name:%%burger%% @location:[-75.153270 39.949655 5 km]" APPLY "geodistance(@location, -75.153270, 39.949655)" AS distance SORTBY distance ASC
will limit the search to a 5km radius.
Hope that helps!
Upvotes: 1