首页 > 解决方案 > 如何将距离值存储到 SortValues 或实体

问题描述

如何使用 SDE4.0 @Query 和 SearchHit 将距离值存储到 SortValues 或实体

"sort": [
  {
    "_geo_distance" : {
      "codenames.geoLocation" : [
        {
          "lat" : 32.846027,
          "lon" : -96.84987
        }
      ],
      "unit" : "mi",
      "order" : "asc",
    }
  }
]

标签: elasticsearchspring-dataelastic-stackspring-data-elasticsearch

解决方案


您必须在Sort存储库查询中添加一个参数,请参阅Spring Data Elasticsearch 4 的文档,其中对此进行了描述。

在您的情况下,您需要:

Sort sort = Sort.by(
    new GeoDistanceOrder("geoLocation", new GeoPoint(32.846027, -96.84987))
        .withUnit("mi")
        .with(Sort.Direction.ASC)
);

您可以省略排序方向,因为 ASC 是默认值


推荐阅读