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

相关推荐
逸模8 小时前
告别熬夜手工整理台账,逸模智能归集实现项目数据自动化存档
大数据·运维·人工智能·笔记·其他·信息可视化·自动化
sbjdhjd8 小时前
Redis 主从复制、哨兵高可用与 Cluster 集群部署实验手册
运维·前端·redis·云原生·开源·bootstrap·html
AOwhisky9 小时前
MySQL 学习笔记(第四期):SQL 语言之多表查询
linux·运维·网络·数据库·笔记·学习·mysql
Phantom Void9 小时前
服务器处理客户端请求的设计方法
linux·运维·网络
倔强的石头1069 小时前
Fooocus开源神器+cpolarAI让绘画告别服务器依赖
运维·服务器·cpolar
wei_shuo9 小时前
服务器挂了等用户投诉才发现?我用Beszel搭了轻量监控系统,宕机第一时间通知我
运维·服务器
王码码20359 小时前
多台服务器怎么统一看状态?Beszel 轻量监控,搭起来不费事
运维·服务器·后端·安全·阿里云·接口·web
APItesterCris12 小时前
实战教程:借助 Open Claw + 淘宝商品 API,低成本实现电商自动化监控与智能选品
大数据·运维·自动化
风曦Kisaki14 小时前
# 自动化运维Day03:Ansible模块进阶(setup,debug),四种常用变量,进阶语法;Ansible Roles(角色)
运维·自动化·ansible
赵民勇14 小时前
Linux strings命令详解
linux·运维