elasticsearch查詢20千米之內的數據並按距離升序排序data es寫法

NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
BoolQueryBuilder boolQueryBuilder=new BoolQueryBuilder();//這暫wei加入業務查詢條件
queryBuilder.withQuery(boolQueryBuilder);
//距離限制
GeoDistanceQueryBuilder distanceQueryBuilder=new GeoDistanceQueryBuilder("location");
distanceQueryBuilder.point(latitude,longitude);
distanceQueryBuilder.distance("20", DistanceUnit.KILOMETERS);//設置二十千米之內的學校
boolQueryBuilder.filter(distanceQueryBuilder);
System.out.println(boolQueryBuilder.toString());
//建立距離排序
GeoDistanceSortBuilder distanceSortBuilder =new GeoDistanceSortBuilder("location", latitude, longitude);
distanceSortBuilder.unit(DistanceUnit.KILOMETERS);
distanceSortBuilder.order(SortOrder.ASC);
//按距離降序,評分降序
queryBuilder.withSort(distanceSortBuilder);
queryBuilder.withSort(SortBuilders.fieldSort("score").order(SortOrder.DESC));
queryBuilder.withPageable(PageRequest.of(PageUtil.getPage(param.getPage())-1,PageUtil.getPageSize(param.getPage_size())));
SearchQuery searchQuery=queryBuilder.build();
Page<CourseIndex> page= courseEsRepository.search(searchQuery);
相關文章
相關標籤/搜索