如何使用Spring Data Redis存储库指定geo radius命令附加参数?

问题描述 投票:1回答:1

我正在使用Spring Data Redis保存一些地址,每个都包含类型为locationPoint属性,该属性保存特定地址的地理坐标。同样,该属性用@GeoIndexed注释。就像这里描述的一样:Geospatial Index

我的Address模型看起来像这样:

@RedisHash("addresses")
public class Address {    
    @Id
    private String id;    

    @GeoIndexed
    private Point location;    
}

通过此存储库查询,我能够将所有附近的地址获取到给定的点和距离:

public interface AddressRepository extends CrudRepository<Address, String> {
    List<Address> findByLocationNear(Point location, Distance distance);
}

我的问题是上述查询返回的地址未排序,但我需要将它们从最近到最远排序(此处描述的ASC选项:GEORADIUS - Redis Command)。

因此,通常,我需要一种方法来将其他参数传递给该查询,例如对结果进行排序或限制(GEORADIUS - Redis Command的任何选项。

有人可以帮忙吗?

spring redis spring-data spring-data-redis
1个回答
0
投票
通过这种方式,您可以使用RedisGeoCommands.GeoRadiusCommandArgs.limit(n),并且可以将结果数量限制为前n个匹配项。

在这里查看Spring Data Redis的官方文档:

https://docs.spring.io/spring-data/redis/docs/current/api/org/springframework/data/redis/connection/RedisGeoCommands.GeoRadiusCommandArgs.html

您也可以直接在Spring Data Redis测试中找到一些示例。

© www.soinside.com 2019 - 2024. All rights reserved.