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

相关推荐
无足鸟ICT7 分钟前
【RHCA+】介绍proc
linux·运维·服务器
Decay__4 小时前
PostgreSQL 用户权限与安全 —— 角色体系、GRANT 授权、pg_hba.conf 认证、行级安全与审计
运维·数据库·sql·postgresql
fengkai45454 小时前
Python运维实战
运维·开发语言·python·ssh·sftp·paramiko
那明眸似水6 小时前
LVS高可用负载均衡集群部署实训
运维·负载均衡·lvs
MSTcheng.6 小时前
【LinuxIO篇】Linux学习第二十三弹——fopen和open有什么区别?从 FILE* 到 fd,讲透 Linux 文件 IO
linux·运维·学习·io·系统调用·fd
charliejohn6 小时前
计算机考研 408 网络 DNS 概念
运维·服务器·网络
BullSmall6 小时前
Jenkins Git插件报错?6步修复+预防指南
运维·jenkins
分布式存储与RustFS6 小时前
向量库快照只落在本地盘:换节点恢复时把 RustFS 桶当副本源
运维·云原生·开源·对象存储·分布式存储·s3·性能基准
分布式存储与RustFS7 小时前
远程 Agent 连不上 stdio 的 MCP 工具:给 RustFS 加一层网关,六个操作变成 HTTP 端点
运维·云原生·开源·对象存储·分布式存储·s3·性能基准
飞奔的瓜牛7 小时前
运维工程师 Redis 日常巡检清单与实战注意事项
运维·数据库·redis