ToB企服应用市场:ToB评测及商务社交产业平台

标题: SpringBoot中easy-es入门实战(最完整-结合官方文档版) [打印本页]

作者: 小秦哥    时间: 前天 16:25
标题: SpringBoot中easy-es入门实战(最完整-结合官方文档版)
媒介:本文主要是参考官方文档举行编写,记录一下自己一些比较常利用easy-es利用方法和内容,其实他的利用和MybatisPlus差不多的,之前我还写了一些关于es的博客可以参考一下:
Springboot中利用Elasticsearch(部署+利用+讲解 最完整)_spring boot elasticsearch-CSDN博客文章欣赏阅读5.3k次,点赞29次,收藏51次。最完整最具体的springboot中利用es,在前面有服务器部署es相干的东西,在后面有利用java的实战,对于实战的方法利用结合官网深度去研究和讲解。在这篇文章前面是实战,后面是具体讲解~~~假如只想实战就只看一和二,深入了解就继续看,在将来还会继续更新对这个实战,另有es技术的更新,几万字大长文。_spring boot elasticsearch
https://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框架
https://www.easy-es.cn/pages/7ead0d/#%E7%AE%80%E4%BB%8B

二、添加依赖

我的项目版本是

其实在这次演示中mysql相干是没有效到的

  1.         <!-- 排除springboot中内置的es依赖,以防和easy-es中的依赖冲突-->
  2.         <dependency>
  3.             <groupId>org.springframework.boot</groupId>
  4.             <artifactId>spring-boot-starter-web</artifactId>
  5.             <exclusions>
  6.                 <exclusion>
  7.                     <groupId>org.elasticsearch.client</groupId>
  8.                     <artifactId>elasticsearch-rest-high-level-client</artifactId>
  9.                 </exclusion>
  10.                 <exclusion>
  11.                     <groupId>org.elasticsearch</groupId>
  12.                     <artifactId>elasticsearch</artifactId>
  13.                 </exclusion>
  14.             </exclusions>
  15.         </dependency>
  16.         <dependency>
  17.             <groupId>org.elasticsearch.client</groupId>
  18.             <artifactId>elasticsearch-rest-high-level-client</artifactId>
  19.             <version>7.14.0</version>
  20.         </dependency>
  21.         <dependency>
  22.             <groupId>org.elasticsearch</groupId>
  23.             <artifactId>elasticsearch</artifactId>
  24.             <version>7.14.0</version>
  25.         </dependency>
  26.         <dependency>
  27.             <groupId>org.dromara.easy-es</groupId>
  28.             <artifactId>easy-es-boot-starter</artifactId>
  29.             <!--这里Latest Version是指最新版本的依赖,比如2.0.0,可以通过下面的图片获取-->
  30.             <version>2.0.0</version>
  31.         </dependency>
  32.         <!--数据库驱动-->
  33.         <dependency>
  34.             <groupId>com.mysql</groupId>
  35.             <artifactId>mysql-connector-j</artifactId>
  36.             <version>8.2.0</version>
  37.         </dependency>
  38.         <!--mybatis的起步依赖-->
  39.         <dependency>
  40.             <groupId>org.mybatis.spring.boot</groupId>
  41.             <artifactId>mybatis-spring-boot-starter</artifactId>
  42.             <version>3.0.3</version>
  43.         </dependency>
  44.         <!--mybatisPlus的起步依赖-->
  45.         <dependency>
  46.             <groupId>com.baomidou</groupId>
  47.             <artifactId>mybatis-plus-boot-starter</artifactId>
  48.             <version>3.5.5</version>
  49.         </dependency>
复制代码
三、设置


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


  1. import org.dromara.easyes.starter.register.EsMapperScan;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. @EsMapperScan("com.bluefoxyu.easyes.sample.mapper")
  6. public class Application {
  7.     public static void main(String[] args) {
  8.         SpringApplication.run(Application.class, args);
  9.     }
  10. }
复制代码
五、准备实体类


  1. @Data
  2. @IndexName(value = "document")
  3. public class Document {
  4.     /**
  5.      * es对应主键id标识,自定义es中的id为我提供的id
  6.      */
  7.     @TableId
  8.     @IndexId(type = IdType.CUSTOMIZE)
  9.     private String id;
  10.     /**
  11.      * 文档标题,分析:IK_MAX_WORD,查找:IK_SMART
  12.      */
  13.     @IndexField(fieldType = FieldType.TEXT,analyzer = Analyzer.IK_MAX_WORD,searchAnalyzer = Analyzer.IK_SMART)
  14.     private String title;
  15.     /**
  16.      * 文档内容,分析:IK_MAX_WORD,查找:IK_SMART
  17.      */
  18.     @IndexField(fieldType = FieldType.TEXT,analyzer = Analyzer.IK_MAX_WORD,searchAnalyzer = Analyzer.IK_SMART)
  19.     private String content;
  20.     private LocalDateTime createTime;
  21. }
复制代码
注意:

六、编写Mapper类


  1. import com.bluefoxyu.easyes.sample.domain.Document;
  2. import org.dromara.easyes.core.kernel.BaseEsMapper;
  3. public interface DocumentMapper extends BaseEsMapper<Document> {
  4. }
复制代码
七、编写接口举行CRUD

   这里一些提示方法案例都来源于官方文档,这里只是作为一次演示和条记
  数据CRUD | Easy-Es傻瓜级ElasticSearch搜刮引擎ORM框架
https://www.easy-es.cn/pages/f3ee10/

1.创建索引

  1. documentMapper.createIndex()
  2. documentMapper.deleteIndex()
复制代码

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

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

假如换成Timestamp:

删除索引再新建查看:

可以看到是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加上一个注解再去测试:
  1. @IndexField(fieldType = FieldType.DATE)
复制代码


可以看到可以格式化了,也是一个时间的格式,后面的演示我们利用LocalDateTime这个类型
2.批量新增文档数据

  1. // 插入一条记录,默认插入至当前mapper对应的索引
  2. Integer insert(T entity);
  3. // 插入一条记录 可指定具体插入的路由
  4. Integer insert(String routing, T entity);
  5. // 父子类型 插入一条记录 可指定路由, 父id
  6. Integer insert(String routing, String parentId, T entity);
  7. // 插入数据 可指定具体插入的索引,多个用逗号隔开
  8. Integer insert(T entity, String... indexNames);
  9. // 插入数据,可指定路由及多索引插入
  10. Integer insert(String routing, T entity, String... indexNames);
  11. // 父子类型 插入数据,可指定路由,父id及多索引插入
  12. Integer insert(String routing, String parentId, T entity, String... indexNames);
  13. // 批量插入多条记录
  14. Integer insertBatch(Collection<T> entityList)
  15. // 批量插入 可指定路由
  16. Integer insertBatch(String routing, Collection<T> entityList);
  17. // 父子类型 批量插入 可指定路由, 父id
  18. Integer insertBatch(String routing, String parentId, Collection<T> entityList);
  19. // 批量插入多条记录 可指定具体插入的索引,多个用逗号隔开
  20. Integer insertBatch(Collection<T> entityList, String... indexNames);
  21. // 批量插入 可指定路由及多索引
  22. Integer insertBatch(String routing, Collection<T> entityList, String... indexNames);
  23. // 父子类型 批量插入 可指定路由,父id及多索引
  24. Integer insertBatch(String routing, String parentId, Collection<T> entityList, String... indexNames);
复制代码
参数说明:
类型参数名形貌Stringrouting路由StringindexNames索引列表Tentity实体对象Collection<T>entityList实体对象聚集

  1.     /**
  2.     * <p>
  3.     * description: 批量新增文档
  4.     * </p>
  5.     *
  6.     * @param count 前端入参:需要插入文档数量
  7.     * @return: java.lang.Integer
  8.     * @author: bluefoxyu
  9.     * @date: 2024-12-28 16:38:26
  10.     */
  11.     @PostMapping("/insertBatch")
  12.     public Integer insertBatch(@RequestParam Integer count) {
  13.         return insertDocuments(count);
  14.     }
  15.     /**
  16.      * 批量插入 1000 条数据到 Elasticsearch
  17.      */
  18.     public int insertDocuments(int count) {
  19.         List<Document> documents = generateDocuments(count);
  20.         documentMapper.insertBatch(documents);
  21.         System.out.println("成功插入 1000 条数据到 Elasticsearch");
  22.         return count;
  23.     }
  24.     /**
  25.      * 生成指定数量的 Document 数据
  26.      *
  27.      * @param count 数据数量
  28.      * @return 数据列表
  29.      */
  30.     public List<Document> generateDocuments(int count) {
  31.         List<Document> documents = new ArrayList<>();
  32.         Random random = new Random();
  33.         // 当前时间为基准
  34.         LocalDateTime baseTime = LocalDateTime.now();
  35.         for (int i = 1; i <= count; i++) {
  36.             // 创建时间在基准时间基础上随机增加若干秒
  37.             LocalDateTime createTime = baseTime.plusSeconds(random.nextInt(86400)); // 随机一天内的秒数
  38.             Document document = Document.builder()
  39.                     .id(i) // 模拟自增 ID
  40.                     .title("Document Title " + i)
  41.                     .content("This is the content of document " + i)
  42.                     .createTime(createTime)
  43.                     .build();
  44.             documents.add(document);
  45.         }
  46.         return documents;
  47.     }
复制代码


kibana中测试:


3.批量删除

这里的删除和MybatisPlus一致,这里演示全部删除
  1.     /**
  2.     * <p>
  3.     * description: 全部删除
  4.     * </p>
  5.     *
  6.     * @return: java.lang.Integer
  7.     * @author: bluefoxyu
  8.     * @date: 2024-12-28 16:53:13
  9.     */
  10.     @DeleteMapping("/deleteAll")
  11.     public Integer deleteAll() {
  12.         // 创建删除条件:match_all 表示匹配所有文档
  13.         LambdaEsQueryWrapper<Document> deleteWrapper = new LambdaEsQueryWrapper<>();
  14.         deleteWrapper.matchAllQuery(); // 匹配所有文档
  15.         return documentMapper.delete(deleteWrapper);
  16.     }
复制代码


  1. // 根据 ID 删除
  2. Integer deleteById(Serializable id);
  3. // 根据 ID 删除 可指定路由
  4. Integer deleteById(String routing, Serializable id);
  5. // 根据 ID 删除 可指定具体的索引,多个用逗号隔开
  6. Integer deleteById(Serializable id, String... indexNames);
  7. // 根据 ID 删除 可指定路由及多索引
  8. Integer deleteById(String routing, Serializable id, String... indexNames);
  9. // 根据 entity 条件,删除记录
  10. Integer delete(LambdaEsQueryWrapper<T> wrapper);
  11. // 删除(根据ID 批量删除)
  12. Integer deleteBatchIds(Collection<? extends Serializable> idList);
  13. // 删除(根据ID 批量删除)可指定路由
  14. Integer deleteBatchIds(String routing, Collection<? extends Serializable> idList);
  15. // 删除(根据ID 批量删除)可指定具体的索引,多个用逗号隔开
  16. Integer deleteBatchIds(Collection<? extends Serializable> idList, String... indexNames);
  17. // 删除(根据ID 批量删除) 可指定路由及多索引
  18. Integer deleteBatchIds(String routing, Collection<? extends Serializable> idList, String... indexNames);
复制代码
参数说明:
类型参数名形貌Stringrouting路由StringindexNames索引列表Wrapper<T>queryWrapper实体包装类 QueryWrapperSerializableid主键IDCollection<? extends Serializable>idList主键ID列表 4.修改

  1. //根据 ID 更新
  2. Integer updateById(T entity);
  3. // 根据 ID 更新 可指定路由
  4. Integer updateById(String routing, T entity);
  5. // 根据 ID 更新 可指定具体的索引,多个用逗号隔开
  6. Integer updateById(T entity, String... indexNames);
  7. // 根据 ID 更新 可指定路由和多索引
  8. Integer updateById(String routing, T entity, String... indexNames);
  9. // 根据ID 批量更新
  10. Integer updateBatchByIds(Collection<T> entityList);
  11. // 根据ID 批量更新 可指定路由
  12. Integer updateBatchByIds(String routing, Collection<T> entityList);
  13. //根据 ID 批量更新 可指定具体的索引,多个用逗号隔开
  14. Integer updateBatchByIds(Collection<T> entityList, String... indexNames);
  15. // 根据ID 批量更新 可指定路由及多索引
  16. Integer updateBatchByIds(String routing, Collection<T> entityList, String... indexNames);
  17. // 根据动态条件 更新记录
  18. Integer update(T entity, LambdaEsUpdateWrapper<T> updateWrapper);
复制代码
参数说明
类型参数名形貌Stringrouting路由StringindexNames索引列表Tentity实体对象Wrapper<T>updateWrapper实体对象封装操纵类 UpdateWrapperCollection<T>entityList实体对象聚集 不过这里我比较喜欢利用链式的写法:
  1.     /**
  2.     * <p>
  3.     * description: 根据id修改文档内容
  4.     * </p>
  5.     *
  6.     * @param id 文档id
  7.     * @param title 文档title
  8.     * @param content 文档content
  9.     * @return: java.lang.Integer
  10.     * @author: bluefoxyu
  11.     * @date: 2024-12-28 17:18:56
  12.     */
  13.     @PutMapping("/updateById/{id}")
  14.     public Integer updateById(@PathVariable Integer id,@RequestParam String title,@RequestParam String content) {
  15.         return EsWrappers.lambdaChainUpdate(documentMapper)
  16.                 .eq(Document::getId, id)
  17.                 .set(Document::getTitle, title)
  18.                 .set(Document::getContent, content)
  19.                 .update();
  20.     }
复制代码


5.查询

  1.         // 获取总数
  2.     Long selectCount(LambdaEsQueryWrapper<T> wrapper);
  3.     // 获取总数 distinct为是否去重 若为ture则必须在wrapper中指定去重字段
  4.     Long selectCount(Wrapper<T> wrapper, boolean distinct);
  5.    
  6.         // 根据 ID 查询
  7.     T selectById(Serializable id);
  8.     // 根据 ID 查询 可指定路由
  9.     T selectById(String routing, Serializable id);
  10.     // 根据 ID 查询 可指定具体的索引,多个用逗号隔开
  11.     T selectById(Serializable id, String... indexNames);
  12.     // 根据 ID 查询 可指定路由及多索引
  13.     T selectById(String routing, Serializable id, String... indexNames);
  14.     // 查询(根据ID 批量查询)
  15.     List<T> selectBatchIds(Collection<? extends Serializable> idList);
  16.     // 查询(根据ID 批量查询) 可指定路由
  17.     List<T> selectBatchIds(String routing, Collection<? extends Serializable> idList);
  18.     // 查询(根据ID 批量查询)可指定具体的索引,多个用逗号隔开
  19.     List<T> selectBatchIds(Collection<? extends Serializable> idList, String... indexNames);
  20.     // 查询(根据ID 批量查询) 可指定路由及多索引
  21.     List<T> selectBatchIds(String routing, Collection<? extends Serializable> idList, String... indexNames);
  22.     // 根据动态查询条件,查询一条记录 若存在多条记录 会报错
  23.     T selectOne(LambdaEsQueryWrapper<T> wrapper);
  24.     // 根据动态查询条件,查询全部记录
  25.     List<T> selectList(LambdaEsQueryWrapper<T> wrapper);
复制代码
参数说明:
类型参数名形貌Stringrouting路由StringindexNames索引列表Wrapper<T>queryWrapper实体包装类 QueryWrapperSerializableid主键IDCollection<? extends Serializable>idList主键ID列表 普通写法:

  1.     /**
  2.     * <p>
  3.     * description: 根据title模糊查询(普通写法)
  4.     * </p>
  5.     *
  6.     * @return: java.util.List<com.bluefoxyu.easyes.sample.domain.Document>
  7.     * @author: bluefoxyu
  8.     * @date: 2024-12-28 17:36:58
  9.     */
  10.     @GetMapping("/searchByTitle1")
  11.     public List<Document> searchByTitle1() {
  12.         // 3.查询出所有标题为老汉的文档列表
  13.         LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
  14.         wrapper.like(Document::getTitle, "修改");
  15.         return documentMapper.selectList(wrapper);
  16.     }
复制代码

链式写法:

  1.     /**
  2.      * <p>
  3.      * description: 根据title模糊查询(链式写法)
  4.      * </p>
  5.      *
  6.      * @return: java.util.List<com.bluefoxyu.easyes.sample.domain.Document>
  7.      * @author: bluefoxyu
  8.      * @date: 2024-12-28 17:36:58
  9.      */
  10.     @GetMapping("/searchByTitle2")
  11.     public List<Document> searchByTitle2() {
  12.         // 3.查询出所有标题为老汉的文档列表
  13.         return EsWrappers.lambdaChainQuery(documentMapper)
  14.                 .like(Document::getTitle, "修改")
  15.                 .list();
  16.     }
复制代码

时间范围查询:

  1.     /**
  2.      * <p>
  3.      * description: 根据 createTime 查询文档列表(链式写法)
  4.      * </p>
  5.      *
  6.      * @param startTime 开始时间
  7.      * @param endTime 结束时间
  8.      * @return: java.util.List<com.bluefoxyu.easyes.sample.domain.Document>
  9.      * @author: bluefoxyu
  10.      * @date: 2024-12-28 17:40:58
  11.      */
  12.     @GetMapping("/searchByCreateTime")
  13.     public List<Document> searchByCreateTime(@RequestParam LocalDateTime startTime, @RequestParam LocalDateTime endTime) {
  14.         // 需要进行格式匹配
  15.         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
  16.         // 查询出 createTime 在 startTime 和 endTime 之间的文档列表
  17.         return EsWrappers.lambdaChainQuery(documentMapper)
  18.                 .ge(Document::getCreateTime, startTime.format(formatter)) // 大于或等于开始时间
  19.                 .le(Document::getCreateTime, endTime.format(formatter))   // 小于或等于结束时间
  20.                 .list();
  21.     }
复制代码
需要举行格式化:



八、四大嵌套查询

这里主要内容都是参考官方文档,可以移步官方文档举行学习:
四大嵌套查询 | Easy-Es傻瓜级ElasticSearch搜刮引擎ORM框架
https://www.easy-es.cn/pages/17ea0a/
ES四大嵌套查询

MySQLMybatis-PlusESEasy-Esand 嵌套and(Consumer)mustand(Consumer)or 嵌套or (Consumer)shouldor (Consumer)无无filterfilter(Consumer)无无must_notnot(Consumer) ES四大拼接查询

MySQLMybatis-PlusESEasy-Esand 拼接默认must默认or 拼接or()shouldor()无无filterfilter()无无must_notnot() 官方文档案例

下面是官方文档的一些案例,在现实的开发中都经常利用:
  1.     /**
  2.      * 场景一: 嵌套and的使用
  3.      */
  4.     @Test
  5.     public void testNestedAnd() {
  6.         // 下面查询条件等价于MySQL中的 select * from document where star_num in (1, 2) and (title = '老汉' or title = '推*')
  7.         LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
  8.         wrapper.in(Document::getStarNum, 1, 2)
  9.                .and(w -> w.eq(Document::getTitle, "老汉").or().eq(Document::getTitle, "推*"));
  10.         List<Document> documents = documentMapper.selectList(wrapper);
  11.     }
  12.     /**
  13.      * 场景二: 拼接and的使用
  14.      */
  15.     @Test
  16.     public void testAnd(){
  17.         // 下面查询条件等价于MySQL中的 select * from document where title = '老汉' and content like '推*'
  18.         // 拼接and比较特殊,因为使用场景最多,所以条件与条件之间默认就是拼接and,所以可以直接省略,这点和MP是一样的
  19.         LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
  20.         wrapper.eq(Document::getTitle, "老汉")
  21.                .match(Document::getContent, "推*");
  22.         List<Document> documents = documentMapper.selectList(wrapper);
  23.     }
  24.    
  25.     /**
  26.      * 场景二: 嵌套or的使用
  27.      */
  28.     @Test
  29.     public void testNestedOr() {
  30.         // 下面查询条件等价于MySQL中的 select * from document where star_num = 1 or (title = '老汉' and creator = '糟老头子')
  31.         LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
  32.         wrapper.eq(Document::getStarNum, 1)
  33.                 .or(i -> i.eq(Document::getTitle, "老汉").eq(Document::getCreator, "糟老头子"));
  34.         List<Document> documents = documentMapper.selectList(wrapper);
  35.     }
  36.     /**
  37.      * 场景三: 拼接or的使用
  38.      */
  39.     @Test
  40.     public void testOr() {
  41.         // 下面查询条件等价于MySQL中的 select * from document where title = '老汉' or title = '痴汉'
  42.         LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
  43.         wrapper.eq(Document::getTitle, "老汉")
  44.                 .or()
  45.                 .eq(Document::getTitle, "痴汉");
  46.         List<Document> documents = documentMapper.selectList(wrapper);
  47.     }
  48.     /**
  49.      * 场景四: 嵌套filter的使用 其实和场景一一样,只不过filter中的条件不计算得分,无法按得分排序,查询性能稍高
  50.      */
  51.     @Test
  52.     public void testNestedFilter() {
  53.         // 下面查询条件等价于MySQL中的 select * from document where star_num in (1, 2) and (title = '老汉' or title = '推*')
  54.         LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
  55.         wrapper.in(Document::getStarNum, 1, 2)
  56.                 .filter(w -> w.eq(Document::getTitle, "老汉").or().eq(Document::getTitle, "推*"));
  57.         List<Document> documents = documentMapper.selectList(wrapper);
  58.     }
  59.    
  60.     /**
  61.      * 场景五: 拼接filter的使用 filter中的条件不计算得分,无法按得分排序,查询性能稍高
  62.      */
  63.     @Test
  64.     public void testFilter() {
  65.         // 下面查询条件等价于MySQL中的 select * from document where title = '老汉'
  66.         LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
  67.         wrapper.filter().eq(Document::getTitle, "老汉");
  68.         List<Document> documents = documentMapper.selectList(wrapper);
  69.     }
  70.    
  71.     /**
  72.      * 场景六: 嵌套mustNot的使用
  73.      */
  74.     @Test
  75.     public void testNestedNot() {
  76.         // 下面查询条件等价于MySQL中的 select * from document where title = '老汉' and (size != 18 and age != 18)
  77.         LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
  78.         wrapper.eq(Document::getTitle, "老汉")
  79.                .not(i->i.eq(size,18).eq(age,18));
  80.         List<Document> documents = documentMapper.selectList(wrapper);
  81.     }
  82.    
  83.     /**
  84.      * 场景六: 拼接not()的使用
  85.      */
  86.     @Test
  87.     public void testNot() {
  88.         // 下面查询条件等价于MySQL中的 select * from document where title = '老汉' and  size != 18
  89.         LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
  90.         wrapper.eq(Document::getTitle, "老汉")
  91.                .not()
  92.                .eq(size,18);
  93.         List<Document> documents = documentMapper.selectList(wrapper);
  94.     }
复制代码
九、链式调用

参考官方文档:
链式调用 | Easy-Es傻瓜级ElasticSearch搜刮引擎ORM框架
https://www.easy-es.cn/pages/e36140/一样平常链式调用有下面三种,这些也是我比较喜欢的一致方法,一样平常利用的比较多:
  1. // 索引链式构造器
  2. LambdaEsIndexChainWrapper<T> lambdaChainIndex(BaseEsMapper<T> baseEsMapper);
  3. // 查询链式构造器
  4. LambdaEsQueryChainWrapper<T> lambdaChainQuery(BaseEsMapper<T> baseEsMapper);
  5. // 更新(含删除)链式构造器
  6. LambdaEsUpdateChainWrapper<T> lambdaChainUpdate(BaseEsMapper<T> baseEsMapper);
复制代码
官网案例:
  1. // 索引链式构造器
  2. LambdaEsIndexChainWrapper<T> lambdaChainIndex(BaseEsMapper<T> baseEsMapper);
  3. // 查询链式构造器
  4. LambdaEsQueryChainWrapper<T> lambdaChainQuery(BaseEsMapper<T> baseEsMapper);
  5. // 更新(含删除)链式构造器
  6. LambdaEsUpdateChainWrapper<T> lambdaChainUpdate(BaseEsMapper<T> baseEsMapper);
复制代码
上面这些对于我们一样平常开发结合MybatisPlus的语法已经够用了,假如想更好的拓展可以移步官方文档举行学习:
混合查询 | Easy-Es傻瓜级ElasticSearch搜刮引擎ORM框架
https://www.easy-es.cn/pages/5743eb/查询字段过滤 | Easy-Es傻瓜级ElasticSearch搜刮引擎ORM框架
https://www.easy-es.cn/pages/bbee1a/
十、MySQL和EE语法对比

参考官方文档:
MySQL和EE语法对比 | Easy-Es傻瓜级ElasticSearch搜刮引擎ORM框架
https://www.easy-es.cn/pages/8f3438/
MySQLEasy-Eses-DSL/es java apiandandboolQueryBuilder.must(queryBuilder) 盘算得分andfilterboolQueryBuilder.filter(queryBuilder)不盘算得分ororboolQueryBuilder.should(queryBuilder)!notboolQueryBuilder.mustNot(queryBuilder)=eqterm>gtQueryBuilders.rangeQuery('es field').gt()>=ge.rangeQuery('es field').gte()<lt.rangeQuery('es field').lt()<=le.rangeQuery('es field').lte()like '%field%'likeQueryBuilders.wildcardQuery(field,value)not like '%field%'notLikemust not wildcardQuery(field,value)like '%field'likeLeftQueryBuilders.wildcardQuery(field,*value)like 'field%'likeRightQueryBuilders.wildcardQuery(field,value*)betweenbetweenQueryBuilders.rangeQuery('es field').from(xx).to(xx)is nullisNullmust not QueryBuilders.existsQuery(field)ininQueryBuilders.termsQuery(" xx es field", xx)group bygroupByAggregationBuilders.terms()order byorderByfieldSortBuilder.order(ASC/DESC)minminAggregationBuilders.minmaxmaxAggregationBuilders.maxavgavgAggregationBuilders.avgsumsumAggregationBuilders.sumorder by xxx ascorderByAscfieldSortBuilder.order(SortOrder.ASC)order by xxx descorderByDescfieldSortBuilder.order(SortOrder.DESC)joinnestedQueryBuilders.nestedQuery()-matchmatchQuery-matchPhraseQueryBuilders.matchPhraseQuery-matchPrefixQueryBuilders.matchPhrasePrefixQuery-queryStringQueryQueryBuilders.queryStringQueryselect *matchAllQueryQueryBuilders.matchAllQuery()-highLightHighlightBuilder.Field......... 末了,本博客仅用为个人一样平常学习easy-es的学习条记,自己工作上利用的多的记一记,希望也可以给大家带来便捷的利用和学习。


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4