Hibernate:从空间搜索中检索距离值
Hibernate: Retrieve distance value from spatial search
我正在尝试使用 Hibernate Spatial 检索用户当前位置半径范围内的商店列表。我想在不计算两次距离的情况下将距离连同每个结果一起提供给用户。
Hibernate 的 documentation 建议为 FullTextQuery 设置一个投影,以便在结果中包含距离。但是,该示例没有提供如何检索所述值的指示。该示例最终只是检索半径范围内的商店列表,而不是到中心的距离。
这就是我的代码目前的样子:
FullTextSession session = (FullTextSession)Search.getFullTextEntityManager(entityManager);
QueryBuilder builder = session.getSearchFactory()
.buildQueryBuilder().forEntity(Shop.class).get();
Query query = builder.spatial()
.onField("location")
.within(radius, Unit.KM)
.ofLatitude(latitude)
.andLongitude(longitude)
.createQuery();
FullTextQuery hibernate = session.createFullTextQuery(query)
.setProjection(FullTextQuery.SPATIAL_DISTANCE, FullTextQuery.THIS)
.setSpatialParameters(latitude, longitude, "location");
// TODO: build map of shops and their distance
距离包含在对象数组中的查询命中中,就像任何其他投影一样。
FullTextSession session = (FullTextSession)Search.getFullTextEntityManager(entityManager);
QueryBuilder builder = session.getSearchFactory()
.buildQueryBuilder().forEntity(Shop.class).get();
Query query = builder.spatial()
.onField("location")
.within(radius, Unit.KM)
.ofLatitude(latitude)
.andLongitude(longitude)
.createQuery();
FullTextQuery ftQuery = session.createFullTextQuery(query, Shop.class)
.setProjection(FullTextQuery.SPATIAL_DISTANCE, FullTextQuery.THIS)
.setSpatialParameters(latitude, longitude, "location");
List<Object[]> results = ftQuery.list();
Object[] firstResult = results.get(0);
Double distance = (Double) firstResult[0];
Shop shop = (Shop) firstResult[1];
见https://docs.jboss.org/hibernate/search/5.11/reference/en-US/html_single/#projections
我正在尝试使用 Hibernate Spatial 检索用户当前位置半径范围内的商店列表。我想在不计算两次距离的情况下将距离连同每个结果一起提供给用户。
Hibernate 的 documentation 建议为 FullTextQuery 设置一个投影,以便在结果中包含距离。但是,该示例没有提供如何检索所述值的指示。该示例最终只是检索半径范围内的商店列表,而不是到中心的距离。
这就是我的代码目前的样子:
FullTextSession session = (FullTextSession)Search.getFullTextEntityManager(entityManager);
QueryBuilder builder = session.getSearchFactory()
.buildQueryBuilder().forEntity(Shop.class).get();
Query query = builder.spatial()
.onField("location")
.within(radius, Unit.KM)
.ofLatitude(latitude)
.andLongitude(longitude)
.createQuery();
FullTextQuery hibernate = session.createFullTextQuery(query)
.setProjection(FullTextQuery.SPATIAL_DISTANCE, FullTextQuery.THIS)
.setSpatialParameters(latitude, longitude, "location");
// TODO: build map of shops and their distance
距离包含在对象数组中的查询命中中,就像任何其他投影一样。
FullTextSession session = (FullTextSession)Search.getFullTextEntityManager(entityManager);
QueryBuilder builder = session.getSearchFactory()
.buildQueryBuilder().forEntity(Shop.class).get();
Query query = builder.spatial()
.onField("location")
.within(radius, Unit.KM)
.ofLatitude(latitude)
.andLongitude(longitude)
.createQuery();
FullTextQuery ftQuery = session.createFullTextQuery(query, Shop.class)
.setProjection(FullTextQuery.SPATIAL_DISTANCE, FullTextQuery.THIS)
.setSpatialParameters(latitude, longitude, "location");
List<Object[]> results = ftQuery.list();
Object[] firstResult = results.get(0);
Double distance = (Double) firstResult[0];
Shop shop = (Shop) firstResult[1];
见https://docs.jboss.org/hibernate/search/5.11/reference/en-US/html_single/#projections