From 5db609a6d5ada7d3866b17fac697ec904b31ef1f Mon Sep 17 00:00:00 2001 From: Zerroi Date: Mon, 15 Apr 2024 21:29:16 +0800 Subject: [PATCH] =?UTF-8?q?fix(dev):=20bug=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hotel/service/impl/HotelService.java | 34 ++++++++++++++----- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/main/java/cn/itcast/hotel/service/impl/HotelService.java b/src/main/java/cn/itcast/hotel/service/impl/HotelService.java index b035d8a..e509844 100644 --- a/src/main/java/cn/itcast/hotel/service/impl/HotelService.java +++ b/src/main/java/cn/itcast/hotel/service/impl/HotelService.java @@ -16,6 +16,8 @@ import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; +import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder; +import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortOrder; @@ -33,18 +35,15 @@ public class HotelService extends ServiceImpl implements IHo @Resource private RestHighLevelClient client; - @Override public PageResult search(RequestParams params) { SearchRequest request = new SearchRequest("hotel"); - BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); - - paramCheck(params, boolQuery); + buildQuery(params, request); Integer page = params.getPage(); Integer size = params.getSize(); request.source().from((page - 1) * size).size(size); - if (StringUtils.isEmpty(params.getLocation())) { + if (!StringUtils.isEmpty(params.getLocation())) { request.source().sort(SortBuilders .geoDistanceSort("location", new GeoPoint(params.getLocation())) .order(SortOrder.ASC) @@ -59,24 +58,43 @@ public class HotelService extends ServiceImpl implements IHo } } - private void paramCheck(RequestParams params, BoolQueryBuilder boolQuery) { + private void buildQuery(RequestParams params, SearchRequest request) { + BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); + if (StringUtils.isEmpty(params.getKey())) { boolQuery.must(QueryBuilders.matchAllQuery()); } else { boolQuery.must(QueryBuilders.matchQuery("all", params.getKey())); } - if (StringUtils.isEmpty(params.getCity())) { + if (!StringUtils.isEmpty(params.getBrand())) { + boolQuery.filter(QueryBuilders.termQuery("brand", params.getBrand())); + } + if (!StringUtils.isEmpty(params.getStarName())) { + boolQuery.filter(QueryBuilders.termQuery("starName", params.getStarName())); + } + + if (!StringUtils.isEmpty(params.getCity())) { boolQuery.filter(QueryBuilders.termQuery("city", params.getCity())); } - if (StringUtils.isEmpty(params.getStarName())) { + if (!StringUtils.isEmpty(params.getStarName())) { boolQuery.filter(QueryBuilders.termQuery("city", params.getSize())); } if (params.getMinPrice() != null && params.getMaxPrice() != null) { boolQuery.filter(QueryBuilders.rangeQuery("price").gte(params.getMinPrice()).lte(params.getMaxPrice())); } + + FunctionScoreQueryBuilder functionScoreQuery = + QueryBuilders.functionScoreQuery(boolQuery, + new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{ + new FunctionScoreQueryBuilder.FilterFunctionBuilder( + QueryBuilders.termQuery("isAd", true), + ScoreFunctionBuilders.weightFactorFunction(10) + ) + }); + request.source().query(functionScoreQuery); } private PageResult handleResponse(SearchResponse response) throws IOException {