EelasticSearch使用!!!

1. Easy-ES介绍

Easy-Es傻瓜级ElasticSearch搜索引擎ORM框架https://www.easy-es.cn/

2. 导入依赖包

XML 复制代码
<dependencies>
        <!--easy-es-boot-starter -->
        <dependency>
            <groupId>cn.easy-es</groupId>
            <artifactId>easy-es-boot-starter</artifactId>
            <version>2.0.0-beta1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--排除springboot中自带的es依赖-->
            <exclusions>
                <exclusion>
                    <groupId>org.elasticsearch.client</groupId>
                    <artifactId>elasticsearch-rest-high-level-client</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.elasticsearch.client</groupId>
                    <artifactId>elasticsearch-rest-client</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.elasticsearch</groupId>
                    <artifactId>elasticsearch</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--es-->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>7.14.0</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.14.0</version>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--hutool-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

3. 在对像属性上添加注解

注解 | Easy-Es傻瓜级ElasticSearch搜索引擎ORM框架https://www.easy-es.cn/pages/v1.x/4eb7db/#esmapperscan

java 复制代码
@IndexName( aliasName = "es_product")
public class EsProduct {

    @IndexId(type = IdType.CUSTOMIZE)
    private Integer id;
    @IndexField( fieldType= FieldType.TEXT, analyzer = Analyzer.IK_MAX_WORD,searchAnalyzer = Analyzer.IK_MAX_WORD)
    private String name;
    @IndexField( fieldType= FieldType.INTEGER)
    private Integer categoryId;
    @IndexField( fieldType= FieldType.DOUBLE) // 12.56
    private BigDecimal price;
    @IndexField( fieldType= FieldType.TEXT, analyzer = Analyzer.IK_MAX_WORD)
    private String brief;
    @IndexField( fieldType= FieldType.KEYWORD)
    private String img;
    @IndexField( fieldType= FieldType.TEXT, analyzer = Analyzer.IK_MAX_WORD)
    private List<String> tags;
    @IndexField( fieldType= FieldType.INTEGER) //198
    private Integer highOpinion;
    @IndexField( fieldType= FieldType.INTEGER)
    private Integer salesVolume;
    @IndexField( fieldType= FieldType.DATE)
    private LocalDateTime productionDate;
}

4.新建Mapper类,类似Mybatis的dao

java 复制代码
import cn.easyes.core.core.BaseEsMapper;

public interface ProductMapper  extends BaseEsMapper<Product> {
}

5.配置ES

bash 复制代码
# Easy-Es配置部分
easy-es:
  # 启用Easy-Es功能
  enable: true
  # 设置Elasticsearch服务器地址和端口
  address: 192.168.23.27:9200
  # 全局配置项,设置是否打印执行的DSL语句(便于调试)
  global-config:
    print-dsl: true

5. 创建、删除、查询索引

java 复制代码
 @Test
    void insert() {
        EsProduct esProduct = EsProduct.builder()
                .id(1)
                .name("小米")
                .img("图片地址")
                .brief("小米(MI)Redmi Note12 5G 120Hz OLED屏幕 骁龙4移动平台 5000mAh长续航 8GB+128GB子夜黑 小米红米")
                .price(new BigDecimal(18))
                .categoryId(1)
                .highOpinion(20)
                .productionDate(LocalDateTime.now())
                .salesVolume(99)
                .tags(CollUtil.newArrayList("120高刷","舒适护眼"))
                .build();
        esMapper.insert(esProduct);
    }
    @Test
    void insert2() {
        delete();
        List<EsProduct> esProducts = CollUtil.newArrayList();
        for (int i = 1; i <= 10; i++) {
            EsProduct esProduct = EsProduct.builder()
                    .id(i)
                    .name("小米"+i)
                    .img("图片地址"+i)
                    .brief("小米(MI)Redmi Note"+i+" 5G 120Hz OLED屏幕 骁龙4移动平台 5000mAh长续航 8GB+128GB子夜黑 小米红米")
                    .price(new BigDecimal(500.36+i))
                    .categoryId(1)
                    .highOpinion(100+i)
                    .productionDate(LocalDateTime.now())
                    .salesVolume(200+i)
                    .tags(CollUtil.newArrayList("12"+i+"高刷","舒适护眼"))
                    .build();
            esProducts.add(esProduct);
                }

        esMapper.insertBatch(esProducts);
    }

    @Test
    void update() {
        EsProduct esProduct = EsProduct.builder()
                .id(1)
                .name("su7")
                .img("图片地址")
                .brief("小米汽车")
                .price(new BigDecimal(18))
                .categoryId(9)
                .highOpinion(20)
                .productionDate(LocalDateTime.now())
                .salesVolume(99)
                .tags(CollUtil.newArrayList("120高刷","舒适护眼"))
                .build();
        Integer integer = esMapper.updateById(esProduct);
    }
    @Test
    void delete() {
        Integer batchIds = esMapper.deleteBatchIds(CollUtil.newArrayList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10));
    }

    @Test
    void select() {
        LambdaEsQueryWrapper  queryWrapper = new LambdaEsQueryWrapper<EsProduct>();
        queryWrapper.eq("id",1);
        List list = esMapper.selectList(queryWrapper);
    }
    @Test
    void select2() {
        LambdaEsQueryWrapper  queryWrapper = new LambdaEsQueryWrapper<EsProduct>();
        queryWrapper.queryStringQuery("汽车之家"); //所有字段都去匹配
        List list = esMapper.selectList(queryWrapper);
    }
    @Test
    void select3() {
        LambdaEsQueryWrapper  queryWrapper = new LambdaEsQueryWrapper<EsProduct>();
        queryWrapper.eq("categoryId",1);
        List list = esMapper.selectList(queryWrapper);
    }
    @Test
    void select4() {
        LambdaEsQueryWrapper<EsProduct>  queryWrapper = new LambdaEsQueryWrapper<>();
        queryWrapper.in(EsProduct::getCategoryId,1,9);
      
        List list = esMapper.selectList(queryWrapper);
    }

    @Test
    void select5() {
        LambdaEsQueryWrapper<EsProduct>  queryWrapper = new LambdaEsQueryWrapper<>();
        //queryWrapper.match(EsProduct::getBrief,"汽车",1.0F);
        //queryWrapper.match(EsProduct::getName,"汽车",1.0F);
        //queryWrapper.multiMatchQuery("汽车", Operator.OR, EsProduct::getName,EsProduct::getBrief);
        queryWrapper.in("categoryId",1,9); //where categroyId in (1,9) and ( name like '%汽车%' or brief like '%汽车%')
        queryWrapper.and(
                w->w.match(EsProduct::getBrief,"汽车",1.0F)
                        .or().match(EsProduct::getName,"汽车",2.0F));
        List list = esMapper.selectList(queryWrapper);
        String dsl = esMapper.getSource(queryWrapper);
        System.out.println(dsl);
    }
    @Test
    void select6() {
        LambdaEsQueryWrapper<EsProduct>  queryWrapper = new LambdaEsQueryWrapper<>();

        queryWrapper.in("categoryId",1,9); //where categroyId in (1,9) and ( name like '%汽车%' or brief like '%汽车%')
        queryWrapper.and(
                w->w.match(EsProduct::getBrief,"高刷",1.0F)
                        .or().match(EsProduct::getName,"高刷",2.0F)
                        .or().match(EsProduct::getTags,"高刷",1.0F));
        queryWrapper.orderByDesc(EsProduct::getSalesVolume);
        EsPageInfo<EsProduct> esProductEsPageInfo = esMapper.pageQuery(queryWrapper, 2, 3);

        String dsl = esMapper.getSource(queryWrapper);
        System.out.println(dsl);
    }

    @Test
    void select7() {
        LambdaEsQueryWrapper<EsProduct>  queryWrapper = new LambdaEsQueryWrapper<>();


        queryWrapper.match(EsProduct::getName,"水汽车门");
        List<EsProduct> esProducts = esMapper.selectList(queryWrapper);


    }

6. 原生Api调用

6.1查看索引mapping关系

GET /es_product/_mapping

6.2查看某个文档,具体字段的分词

GET /product/_doc/2/_termvectors?fields=brief

相关推荐
RobinDevNotes1 分钟前
自动化构建镜像:Packer
运维·packer
fareast_mzh35 分钟前
Setting Up a Simple Live Streaming Server on Debian 11
运维·debian·直播
BillKu2 小时前
Linux设置Nginx开机启动
linux·运维·nginx
东方佑2 小时前
FastHTML快速入门:服务器渲染超媒体应用的利器
运维·服务器
baidu_375528812 小时前
光感传感器 芯片stk3171 linux驱动程序
linux·运维·服务器
飞腾开发者2 小时前
飞腾平台Arm NN软件栈安装使用指南
linux·运维·人工智能·机器学习·计算机视觉
鸠摩智首席音效师2 小时前
.NET Core 应用程序如何在 Linux 中创建 Systemd 服务 ?
linux·运维·.netcore
叫我龙翔4 小时前
【计网】实现reactor反应堆模型 --- 多线程方案优化 ,OTOL方案
linux·运维·网络
mit6.8244 小时前
[Docker#9] 存储卷 | Volume、Bind、Tmpfs | -v/mount | MySQL 灾难恢复 | 问题
linux·运维·docker·容器·架构
Z1eaf_complete4 小时前
Docker的基础使用
运维·docker·容器·云计算