ElasticSearch搜索常用操作,条件过滤,排序,根据比分排位

package cn.itcast.hotel.service.impl;

import cn.itcast.hotel.constant.EsConstant;
import cn.itcast.hotel.mapper.HotelMapper;
import cn.itcast.hotel.pojo.Hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import cn.itcast.hotel.pojo.PageResult;
import cn.itcast.hotel.pojo.SearchParams;
import cn.itcast.hotel.service.IHotelService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;

@Service
public class HotelService extends ServiceImpl<HotelMapper, Hotel> implements IHotelService {
    @Autowired
    private RestHighLevelClient client;
    @Override
    public PageResult list(SearchParams searchParams) {
        try {
            //获取参数
            String key = searchParams.getKey();
            Integer page = searchParams.getPage();
            Integer size = searchParams.getSize();
            String sortBy = searchParams.getSortBy();

            SearchRequest searchRequest = new SearchRequest(EsConstant.INDEX_HOTEL);
            //1.过滤查询、创建boolean查询
            BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
            //1.1搜索框使用must
            if (StringUtils.isBlank(key)){
                //关键字为空match_all搜索
                boolQuery.must(QueryBuilders.matchAllQuery());
            }else {
                //关键字不为空match搜索
                boolQuery.must(QueryBuilders.matchQuery("all", key));
            }
            //1.2品牌过滤
            String brand = searchParams.getBrand();
            if (StringUtils.isNotBlank(brand)){
                boolQuery.filter(QueryBuilders.termQuery("brand", brand));
            }
            //1.3星级过滤
            String starName = searchParams.getStarName();
            if (StringUtils.isNotBlank(starName)){
                boolQuery.filter(QueryBuilders.termQuery("starName", starName));
            }
            //1.4价格过滤
            Integer maxPrice = searchParams.getMaxPrice();
            Integer minPrice = searchParams.getMinPrice();
            if (maxPrice!=null&&minPrice!=null){
                boolQuery.filter(QueryBuilders.rangeQuery("price")
                        .gte(minPrice)
                        .lte(maxPrice));
            }
            //1.5城市过滤
            String city = searchParams.getCity();
            if (StringUtils.isNotBlank(city)){
                boolQuery.filter(QueryBuilders.termQuery("city", city));
            }
            //1.6将boolQuery对象添加到query中
            searchRequest.source().query(boolQuery);
            //2.周边酒店(距离排序)
            // searchRequest.source().sort("_score");//根据分值排序默认降序
            String location = searchParams.getLocation();
            if (StringUtils.isNotBlank(location)){
                searchRequest.source().sort(
                        SortBuilders.geoDistanceSort("location", new GeoPoint(location))
                                .order(SortOrder.ASC)
                                .unit(DistanceUnit.KILOMETERS)
                );
            }
/*            searchRequest.source().query(QueryBuilders.functionScoreQuery(boolQuery,
                    new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{
                            new FunctionScoreQueryBuilder.FilterFunctionBuilder(
                                    QueryBuilders.termQuery("isAD", true),
                                    ScoreFunctionBuilders.weightFactorFunction(100.0F)
                            )
                    }).boostMode(CombineFunction.SUM));*/
            //3.广告排位
            // searchRequest.source().query(new FunctionScoreQueryBuilder(, ))
            searchRequest.source().query(
                    new FunctionScoreQueryBuilder(boolQuery,
                            new FunctionScoreQueryBuilder.FilterFunctionBuilder[]{
                                    new FunctionScoreQueryBuilder.FilterFunctionBuilder(
                                            QueryBuilders.termQuery("isAD", true),
                                            ScoreFunctionBuilders.weightFactorFunction(100.0F)
                                    )
                            }).boostMode(CombineFunction.SUM));
            //分页
            searchRequest.source().from((page-1)*size).size(size);
            SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            long total  = searchResponse.getHits().getTotalHits().value;
            //数据封装
            SearchHit[] hits = searchResponse.getHits().getHits();
            ArrayList<HotelDoc> list = new ArrayList<>();
            for (SearchHit hit : hits) {
                String json = hit.getSourceAsString();
                Object[] sortValues = hit.getSortValues();
                HotelDoc hotelDoc = JSONObject.parseObject(json, HotelDoc.class);
                if (sortValues.length>0){
                    hotelDoc.setDistance(sortValues[0]);
                }
                list.add(hotelDoc);
            }
            return new PageResult(total, list);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new PageResult();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值