Spring data elasticsearch:将排序值设置为实体字段

时间:2015-12-10 15:23:59

标签: elasticsearch spring-data-elasticsearch elasticsearch-java-api

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

问题

StoreES已填充 orgCode storeCode ,因为它们位于_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
}

提前致谢。

1 个答案:

答案 0 :(得分:2)

解决方法

@Override
public void onPause() {
    super.onPause();

    mGoogleApiClient.stopAutoManage(getActivity());
    mGoogleApiClient.disconnect();
}