2

spring-data-elastic search を使用した geo distance sort を使用して、elasticsearch から最寄りの店舗を検索します。

問題

_sourceにあるため、 StoreES にorgCodestoreCodeが取り込まれます。検索応答の並べ替え [0]値から距離値を入力するにはどうすればよいですか。

StoreElasticSearchRepository メソッド

public default List<StoreES> nearStores(Double latitude, Double longitude, Integer page, Integer size) {
    GeoDistanceSortBuilder distanceSortbuilder = SortBuilders.geoDistanceSort("location").point(latitude, longitude)
                        .order(SortOrder.ASC).unit(DistanceUnit.KILOMETERS);
                SearchQuery query = new NativeSearchQueryBuilder().withQuery(QueryBuilders.matchAllQuery())
                        .withSort(distanceSortbuilder).withPageable(new PageRequest(page, size)).build();
                FacetedPage<StoreES> storesPage = this.search(query);
    return storesPage.getContent();
}

上記のクエリに相当する残りのリクエスト:

{
  "sort": {
    "_geo_distance": {
      "location": {
        "lat": 12.9259,
        "lon": 77.6229
      },
      "order": "asc",
      "unit": "km"
    }
  }
}

クエリの結果

    {
  "took": 5,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1365,
    "max_score": null,
    "hits": [
      {
        "_index": "app",
        "_type": "store",
        "_id": "99991258",
        "_score": null,
        "_source": {
          "id": 1,
          "orgCode": "ABC",
          "storeCode": "12345",
       },
      "sort": [
          0.49933591591981075
        ]
}

エンティティ オブジェクト

public class StoreES{

    private String orgCode;

    private String storeCode;
  
    private Double distance;

// setter gettter methods
}

前もって感謝します。

4

1 に答える 1