2

使用 spring-data-elastic search 使用地理距离排序从弹性搜索中查找最近的商店。

问题

StoreES 被orgCodestoreCode填充,因为它们在 _source 中。如何从搜索响应排序 [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

2

解决方法

public default List<StoreES> nearStores(Double latitude, Double longitude, String address, Integer page,
            Integer size) {
DistanceSortBuilder 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);

    List<StoreES> stores = storesPage.getContent();

        // ##############calculate distance##################
        for (StoreES store : stores) {
            if (store.getLocation() != null) {
                double distance = GeoDistance.DEFAULT.calculate(latitude, longitude, store.getLocation().getLat(),
                        store.getLocation().getLon(), DistanceUnit.KILOMETERS);
                store.setDistance(distance);
            }
            store.setDistanceUnit(DistanceUnit.KILOMETERS.toString());
        }
        return stores;
    }
于 2016-06-18T17:56:59.993 回答