fix(dev): bug修复

This commit is contained in:
Zerroi 2024-04-15 21:29:16 +08:00
parent 58669de46a
commit 5db609a6d5
1 changed files with 26 additions and 8 deletions

View File

@ -16,6 +16,8 @@ import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders; 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.SearchHit;
import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder; import org.elasticsearch.search.sort.SortOrder;
@ -33,18 +35,15 @@ public class HotelService extends ServiceImpl<HotelMapper, Hotel> implements IHo
@Resource @Resource
private RestHighLevelClient client; private RestHighLevelClient client;
@Override @Override
public PageResult search(RequestParams params) { public PageResult search(RequestParams params) {
SearchRequest request = new SearchRequest("hotel"); SearchRequest request = new SearchRequest("hotel");
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery(); buildQuery(params, request);
paramCheck(params, boolQuery);
Integer page = params.getPage(); Integer page = params.getPage();
Integer size = params.getSize(); Integer size = params.getSize();
request.source().from((page - 1) * size).size(size); request.source().from((page - 1) * size).size(size);
if (StringUtils.isEmpty(params.getLocation())) { if (!StringUtils.isEmpty(params.getLocation())) {
request.source().sort(SortBuilders request.source().sort(SortBuilders
.geoDistanceSort("location", new GeoPoint(params.getLocation())) .geoDistanceSort("location", new GeoPoint(params.getLocation()))
.order(SortOrder.ASC) .order(SortOrder.ASC)
@ -59,24 +58,43 @@ public class HotelService extends ServiceImpl<HotelMapper, Hotel> 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())) { if (StringUtils.isEmpty(params.getKey())) {
boolQuery.must(QueryBuilders.matchAllQuery()); boolQuery.must(QueryBuilders.matchAllQuery());
} else { } else {
boolQuery.must(QueryBuilders.matchQuery("all", params.getKey())); 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())); boolQuery.filter(QueryBuilders.termQuery("city", params.getCity()));
} }
if (StringUtils.isEmpty(params.getStarName())) { if (!StringUtils.isEmpty(params.getStarName())) {
boolQuery.filter(QueryBuilders.termQuery("city", params.getSize())); boolQuery.filter(QueryBuilders.termQuery("city", params.getSize()));
} }
if (params.getMinPrice() != null && params.getMaxPrice() != null) { if (params.getMinPrice() != null && params.getMaxPrice() != null) {
boolQuery.filter(QueryBuilders.rangeQuery("price").gte(params.getMinPrice()).lte(params.getMaxPrice())); 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 { private PageResult handleResponse(SearchResponse response) throws IOException {