SpringBoot中easy-es入门实战(最完整-结合官方文档版)

06-01 1129阅读

前言:本文主要是参考官方文档进行编写,记录一下自己一些比较常使用easy-es使用方法和内容,其实他的使用和MybatisPlus差不多的,之前我还写了一些关于es的博客可以参考一下:

Springboot中使用Elasticsearch(部署+使用+讲解 最完整)_spring boot elasticsearch-CSDN博客文章浏览阅读5.3k次,点赞29次,收藏51次。最完整最详细的springboot中使用es,在前面有服务器部署es相关的东西,在后面有使用java的实战,对于实战的方法使用结合官网深度去研究和讲解。在这篇文章前面是实战,后面是具体讲解~~~如果只想实战就只看一和二,深入了解就继续看,在未来还会继续更新对这个实战,还有es技术的更新,几万字大长文。_spring boot elasticsearchhttps://blog.csdn.net/qq_73440769/article/details/141477177?spm=1001.2014.3001.5501

目录

一、官方文档

二、添加依赖

三、配置

四、Spring Boot 启动类

五、准备实体类

六、编写Mapper类

七、编写接口进行CRUD

1.创建索引

2.批量新增文档数据

3.批量删除

4.修改

5.查询

普通写法:

链式写法:

时间范围查询:

八、四大嵌套查询

ES四大嵌套查询

ES四大拼接查询

官方文档案例

九、链式调用

十、MySQL和EE语法对比


一、官方文档

简介 | Easy-Es傻瓜级ElasticSearch搜索引擎ORM框架SpringBoot中easy-es入门实战(最完整-结合官方文档版)

二、添加依赖

我的项目版本是

  • JDK:17
  • SpringBoot:3.2.4
  • elasticsearch:7.14.0
  • easy-es:2.0.0
  • mybatis:3.0.3
  • mybatis-plus:3.5.5
  • mysql-connector-j:8.2.0

    其实在这次演示中mysql相关是没有用到的

    SpringBoot中easy-es入门实战(最完整-结合官方文档版)

            
            
                org.springframework.boot
                spring-boot-starter-web
                
                    
                        org.elasticsearch.client
                        elasticsearch-rest-high-level-client
                    
                    
                        org.elasticsearch
                        elasticsearch
                    
                
            
            
                org.elasticsearch.client
                elasticsearch-rest-high-level-client
                7.14.0
            
            
                org.elasticsearch
                elasticsearch
                7.14.0
            
            
                org.dromara.easy-es
                easy-es-boot-starter
                
                2.0.0
            
            
            
                com.mysql
                mysql-connector-j
                8.2.0
            
            
            
                org.mybatis.spring.boot
                mybatis-spring-boot-starter
                3.0.3
            
            
            
                com.baomidou
                mybatis-plus-boot-starter
                3.5.5
            

    三、配置

    SpringBoot中easy-es入门实战(最完整-结合官方文档版)

    server:
      port: 8080
    spring:
      profiles:
        active: dev
      main:
        allow-circular-references: true
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        #连接数据库的用户名
        url: jdbc:mysql://localhost:3306/document?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true
        username: root
        password: 123456
    easy-es:
      enable: true #默认为true,若为false则认为不启用本框架
      address : localhost:9200 # es的连接地址,必须含端口 若为集群,则可以用逗号隔开 例如:127.0.0.1:9200,127.0.0.2:9200
    logging:
      level:
        tracer: trace # 设置日志级别为trace,开发时可开启以打印ES全部请求信息及DSL语句

    四、Spring Boot 启动类

    SpringBoot中easy-es入门实战(最完整-结合官方文档版)

    import org.dromara.easyes.starter.register.EsMapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    @SpringBootApplication
    @EsMapperScan("com.bluefoxyu.easyes.sample.mapper")
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }

    五、准备实体类

    SpringBoot中easy-es入门实战(最完整-结合官方文档版)

    @Data
    @IndexName(value = "document")
    public class Document {
        /**
         * es对应主键id标识,自定义es中的id为我提供的id
         */
        @TableId
        @IndexId(type = IdType.CUSTOMIZE)
        private String id;
        /**
         * 文档标题,分析:IK_MAX_WORD,查找:IK_SMART
         */
        @IndexField(fieldType = FieldType.TEXT,analyzer = Analyzer.IK_MAX_WORD,searchAnalyzer = Analyzer.IK_SMART)
        private String title;
        /**
         * 文档内容,分析:IK_MAX_WORD,查找:IK_SMART
         */
        @IndexField(fieldType = FieldType.TEXT,analyzer = Analyzer.IK_MAX_WORD,searchAnalyzer = Analyzer.IK_SMART)
        private String content;
        private LocalDateTime createTime;
    }

    注意:

    • String类型默认会被EE创建为keyword类型,keyword类型支持精确查询等
    • 如需分词查询,可像上面content一样,在字段上加上@IndexField注解并指明字段类型为text,并指定分词器

      六、编写Mapper类

      SpringBoot中easy-es入门实战(最完整-结合官方文档版)

      import com.bluefoxyu.easyes.sample.domain.Document;
      import org.dromara.easyes.core.kernel.BaseEsMapper;
      public interface DocumentMapper extends BaseEsMapper {
      }

      七、编写接口进行CRUD

      这里一些提示方法案例都来源于官方文档,这里只是作为一次演示和笔记

      数据CRUD | Easy-Es傻瓜级ElasticSearch搜索引擎ORM框架icon-default.png?t=O83Ahttps://www.easy-es.cn/pages/f3ee10/

      SpringBoot中easy-es入门实战(最完整-结合官方文档版)

      1.创建索引

      documentMapper.createIndex()
      documentMapper.deleteIndex()

      SpringBoot中easy-es入门实战(最完整-结合官方文档版)

          @PostMapping("/createIndex")
          public Boolean createIndex() {
              // 1.初始化-> 创建索引(相当于mysql中的表)
              return documentMapper.createIndex();
          }

      SpringBoot中easy-es入门实战(最完整-结合官方文档版)

      在kibana中可以看到时间类型是使用"yyyy-MM-dd HH:mm:ss"这个格式进行格式化:

      SpringBoot中easy-es入门实战(最完整-结合官方文档版)

      如果换成Timestamp:

      SpringBoot中easy-es入门实战(最完整-结合官方文档版)

      删除索引再新建查看:

      SpringBoot中easy-es入门实战(最完整-结合官方文档版)

      可以看到是test类型的,后面会进行这个的测试,如果是这个类型会发生什么,下面是我之前遇到的一个bug:

      关于easy-es对时间范围查询遇到的小bug-CSDN博客文章浏览阅读195次。在使用easy-es之前作为一个小白的我只有es原生查询的基础,在自己通过查看官方文档自学easy-es遇到了一个挫折,其他的还好语法和MybatisPlus差不多,正以为我觉得很快就能入手,在对。https://blog.csdn.net/qq_73440769/article/details/144786181?spm=1001.2014.3001.5501保留为Timestamp加上一个注解再去测试:

      @IndexField(fieldType = FieldType.DATE)

      SpringBoot中easy-es入门实战(最完整-结合官方文档版)

      SpringBoot中easy-es入门实战(最完整-结合官方文档版)

      可以看到可以格式化了,也是一个时间的格式,后面的演示我们使用LocalDateTime这个类型

      2.批量新增文档数据

      // 插入一条记录,默认插入至当前mapper对应的索引
      Integer insert(T entity);
      // 插入一条记录 可指定具体插入的路由
      Integer insert(String routing, T entity);
      // 父子类型 插入一条记录 可指定路由, 父id
      Integer insert(String routing, String parentId, T entity);
      // 插入数据 可指定具体插入的索引,多个用逗号隔开
      Integer insert(T entity, String... indexNames);
      // 插入数据,可指定路由及多索引插入
      Integer insert(String routing, T entity, String... indexNames);
      // 父子类型 插入数据,可指定路由,父id及多索引插入
      Integer insert(String routing, String parentId, T entity, String... indexNames);
      // 批量插入多条记录
      Integer insertBatch(Collection entityList)
      // 批量插入 可指定路由
      Integer insertBatch(String routing, Collection entityList);
      // 父子类型 批量插入 可指定路由, 父id
      Integer insertBatch(String routing, String parentId, Collection entityList);
      // 批量插入多条记录 可指定具体插入的索引,多个用逗号隔开 
      Integer insertBatch(Collection entityList, String... indexNames);
      // 批量插入 可指定路由及多索引
      Integer insertBatch(String routing, Collection entityList, String... indexNames);
      // 父子类型 批量插入 可指定路由,父id及多索引
      Integer insertBatch(String routing, String parentId, Collection entityList, String... indexNames);
      

      参数说明:

      类型参数名描述
      Stringrouting路由
      StringindexNames索引列表
      Tentity实体对象
      CollectionentityList实体对象集合

      SpringBoot中easy-es入门实战(最完整-结合官方文档版)

          /**
          * 

      * description: 批量新增文档 *

      * * @param count 前端入参:需要插入文档数量 * @return: java.lang.Integer * @author: bluefoxyu * @date: 2024-12-28 16:38:26 */ @PostMapping("/insertBatch") public Integer insertBatch(@RequestParam Integer count) { return insertDocuments(count); } /** * 批量插入 1000 条数据到 Elasticsearch */ public int insertDocuments(int count) { List documents = generateDocuments(count); documentMapper.insertBatch(documents); System.out.println("成功插入 1000 条数据到 Elasticsearch"); return count; } /** * 生成指定数量的 Document 数据 * * @param count 数据数量 * @return 数据列表 */ public List generateDocuments(int count) { List documents = new ArrayList(); Random random = new Random(); // 当前时间为基准 LocalDateTime baseTime = LocalDateTime.now(); for (int i = 1; i
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。

目录[+]

取消
微信二维码
微信二维码
支付宝二维码