gpt4 book ai didi

elasticsearch - Spring Data Elasticsearch:将排序值设置为实体字段

转载 作者:行者123 更新时间:2023-12-02 22:24:27 26 4
gpt4 key购买 nike

通过使用 Spring 数据 flex 搜索的地理距离排序,从elasticsearch查找最近的商店。
问题
StoreES填充了 orgCode storeCode ,因为它们在_source中。
如何从搜索响应 sort [0] 值中填充距离值。

StoreElasticSearchRepository method

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();
}

Rest request equivalent for above query:

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

Result for query

    {
"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
]
}

Entity object

public class StoreES{

private String orgCode;

private String storeCode;

private Double distance;

// setter gettter methods
}
提前致谢。

最佳答案

解决

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;
}

关于elasticsearch - Spring Data Elasticsearch:将排序值设置为实体字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34205651/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com