RediSearch 在使用地理过滤器时按数字字段然后按距离排序

RediSearch sort by numeric field then by distance when using geofilter

在 RediSearch 中使用 GEOFILTER 时,如何对结果进行排序,首先按数字字段(例如按价格)然后按距离?

FT.SEARCH 命令不支持 SORTBY 的多个字段。

但是您可以使用 FT.AGGREGATE command and the geodistance 函数。

这里是一个使用 REDIS-CLI 的例子:

HSET doc1 price 9.99 location "-122.41,37.77"
(integer) 0
HSET doc2 price 19.99 location "-122.40,37.78"
(integer) 0
HSET doc3 price 19.99 location "-122.42,37.79"
(integer) 0
FT.CREATE idx SCHEMA price NUMERIC SORTABLE location GEO SORTABLE
OK
FT.AGGREGATE idx "@price:[0 100]" APPLY 'geodistance(@location, "-122.39,37.78")' AS dist SORTBY 4 @price DESC @dist ASC
1) (integer) 3
2) 1) "location"
   2) "-122.40,37.78"
   3) "dist"
   4) "879.1"
   5) "price"
   6) "19.99"
3) 1) "location"
   2) "-122.42,37.79"
   3) "dist"
   4) "2862.08"
   5) "price"
   6) "19.99"
4) 1) "location"
   2) "-122.41,37.77"
   3) "dist"
   4) "2080.58"
   5) "price"
   6) "9.99"