EasyEs:搜索入门

依赖

<!-- es -->
<dependency>
    <groupId>cn.easy-es</groupId>
    <artifactId>easy-es-boot-starter</artifactId>
    <version>1.1.1</version>
</dependency>

实体类Dao

import cn.easyes.annotation.HighLight;
import cn.easyes.annotation.IndexField;
import cn.easyes.annotation.IndexId;
import cn.easyes.annotation.IndexName;
import cn.easyes.annotation.rely.Analyzer;
import cn.easyes.annotation.rely.FieldType;
import cn.easyes.annotation.rely.IdType;
import lombok.Data;

@Data
@IndexName("demo")
public class Demo {

    /**
     * id
     */
    @IndexId(type = IdType.CUSTOMIZE)
    private Integer id;

    /**
     * 类型
     */
    private String type;

    /**
     * 标题
     */
    @HighLight
    @IndexField(fieldType = FieldType.TEXT, analyzer = Analyzer.IK_SMART, searchAnalyzer = Analyzer.IK_MAX_WORD)
    private String title;

    /**
     * 价格
     */
    private BigDecimal price;


    /**
     * 特征
     */
    @IndexField(fieldType = FieldType.TEXT, analyzer = Analyzer.IK_SMART)
    private List<String> features;

    /**
     * 面积
     */
    private Double area;

    /**
     * 所在地
     * 经度,纬度
     */
    @IndexField(fieldType = FieldType.GEO_POINT)
    private String location;

}

Mapper

import cn.easyes.core.conditions.interfaces.BaseEsMapper;

public interface DemoMapper extends BaseEsMapper<Demo> {
}

Search实体

import lombok.Data;

@Data
public class DemoParam {

    /**
     * 搜索词
     */
    private String searchWord;

    /**
     * 地铁
     */
    @Data
    public static class Subway{

        /**
         * 经度
         */
        private Double lon;

        /**
         * 纬度
         */
        private Double lat;
    }

    /**
     * 地铁
     */
    private Subway subway;

    /**
     * 类型
     */
    private String type;

    /**
     * 价格下限
     */
    private BigDecimal priceLow;

    /**
     * 价格上限
     */
    private BigDecimal priceUp;

    /**
     * 特征
     */
    private List<String> features;

    /**
     * 是否为价格升序
     */
    private Boolean priceIsAsc;

    /**
     * 第几页
     */
    private Integer pageNum = 1;

    /**
     *
     */
    private Integer pageSize = 20;

}

Service

import cn.easyes.core.biz.EsPageInfo;
import cn.easyes.core.conditions.LambdaEsQueryWrapper;
import cn.gcfc.monitor.service.house.HouseSearchService;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.common.geo.GeoPoint;
import org.elasticsearch.common.unit.DistanceUnit;

@Service
@Slf4j
public class DemoServiceImpl implements DemoService {

    @Resource
    private DemoMapper demoMapper;

    @Override
    public List<DemoVO> houseVisitorModelSearch(DemoParam demoParam) {

        LambdaEsQueryWrapper<Demo> wrapper = new LambdaEsQueryWrapper<>();
        wrapper.match(StrUtil.isNotBlank(demoParam.getSearchWord()), Demo::getTitle, demoParam.getSearchWord())
                .ge(ObjectUtil.isNotEmpty(demoParam.getPriceLow()), Demo::getPrice, demoParam.getPriceLow())
                .le(ObjectUtil.isNotEmpty(demoParam.getPriceUp()), Demo::getPrice, demoParam.getPriceUp())
                .in(CollUtil.isNotEmpty(demoParam.getFeatures()), Demo::getFeatures, demoParam.getFeatures())
                .eq(StrUtil.isNotBlank(demoParam.getType()), Demo::getType, demoParam.getType());


        // 如果地铁不为空
        DemoParam.Subway subway = demoParam.getSubway();
        if (ObjectUtil.isNotEmpty(subway)) {
            Double lon = subway.getLon();
            Double lat = subway.getLat();

            wrapper.geoDistance(Demo::getLocation, 2.0, DistanceUnit.KILOMETERS, new GeoPoint(lat, lon));
        }

        // 价格排序
        Boolean priceIsAsc = demoParam.getPriceIsAsc();
        if (ObjectUtil.isNotEmpty(priceIsAsc)) {
            if (priceIsAsc) {
                wrapper.orderByAsc(Demo::getPrice);
            } else {
                wrapper.orderByDesc(Demo::getPrice);
            }
        }

        // 分页
        EsPageInfo<Demo> pageInfo = demoMapper.pageQuery(wrapper, demoParam.getPageNum(), demoParam.getPageSize());
        List<Demo> demoList = pageInfo.getList();
        return BeanUtil.copyToList(demoList, DemoVO.class);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员无羡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值