【问题标题】:Spring Data Elasticsearch 2.1.16: how to get the sorting distance?Spring Data Elasticsearch 2.1.16:如何获取排序距离?
【发布时间】:2021-04-21 06:19:00
【问题描述】:

当你尝试在 ES 中按距离排序时,结果通常包含匹配项与你的搜索位置之间的距离:

  "hits": {
"total": 3,
"max_score": null,
"hits": [
  {
    "_index": "circle",
    "_type": "doc",
    "_id": "7",
    "_score": null,
    "_source": {
      "id": 7,
      "coordinates": {
        "lon": 112.548443,
        "lat": 37.780269
      }
    },
    "sort": [
      116.39283058047849
    ]
  },
  {
    "_index": "circle",
    "_type": "doc",
    "_id": "5",
    "_score": null,
    "_source": {
      "id": 5,
      "coordinates": {
        "lon": 112.55061,
        "lat": 37.779145
      }
    },
    "sort": [
      231.9203763747634
    ]
  }
]

} 排序距离包含在sort 字段中。 如何使用 elasticsearchTemplate 在 Spring Date Elasticsearch 2.1.16 中获取排序距离?

这是我的查询代码: Page<CircleES> resultPage = elasticsearchTemplate.queryForPage(searchQuery, CircleES.class);

【问题讨论】:

    标签: elasticsearch spring-data-elasticsearch elasticsearch-template


    【解决方案1】:

    要在 2.1.16 版本中获取此信息,您需要使用方法

    queryForPage(SearchQuery query, Class<T> clazz, SearchResultMapper mapper)
    

    并提供一个自定义的SearchResultMapper,它会从返回的数据中解析所需的信息。

    顺便说一句,2.1.16 已于 2018 年 10 月 15 日发布,现已不受支持。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-08
      • 1970-01-01
      • 2017-07-29
      • 2020-01-18
      • 2014-03-09
      • 2023-03-23
      • 2014-04-27
      • 2022-06-15
      相关资源
      最近更新 更多