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