Springboot整合Easy-Es

这篇具有很好参考价值的文章主要介绍了Springboot整合Easy-Es。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

版本说明

  • Springboot 2.7.5
  • JDK 17
  • Elasticsearch 7.14.0
  • Easy-Es 1.1.1
  • 《点我进入Easy-Es官网》PS:目前Easy-Es暂不支持SpringBoot3.X

Windows10安装Elasticsearch

《安装Elasticsearch教程》

pom.xml

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.aoxqwl</groupId>
    <artifactId>eedemo</artifactId>
    <version>1</version>
    <name>eedemo</name>
    <description>eedemo</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- lombok插件依赖 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
        <!-- Easy-Es暂不支持SpringBoot3.X,且推荐Elasticsearch版本为7.14.0 -->
        <dependency>
            <groupId>cn.easy-es</groupId>
            <artifactId>easy-es-boot-starter</artifactId>
            <version>1.1.1</version>
            <exclusions>
                <exclusion>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.elasticsearch.client</groupId>
                    <artifactId>elasticsearch-rest-high-level-client</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.elasticsearch</groupId>
                    <artifactId>elasticsearch</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>7.14.0</version>
        </dependency>

        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
            <version>7.14.0</version>
        </dependency>
    </dependencies>

application.yml

easy-es:
  enable: true #默认为true,若为false则认为不启用本框架
  banner: false # 默认为true 打印banner 若您不期望打印banner,可配置为false
  address : 127.0.0.1:9200 # es的连接地址,必须含端口 若为集群,则可以用逗号隔开 例如:127.0.0.1:9200,127.0.0.2:9200
  username: elastic #若无 则可省略此行配置
  password: WG7WVmuNMtM4GwNYkyWH #若无 则可省略此行配置
  # 请求通信超时时间 单位:ms 在平滑模式下,由于要迁移数据,用户可根据数据量大小调整此参数值大小,否则请求容易超时导致索引托管失败,建议您尽量给大不给小
  socketTimeout: 5000
  global-config:
    # 项目是否分布式环境部署,默认为true, 如果是单机运行可填false,将不加分布式锁,效率更高.
    distributed: false

Document实体类

import cn.easyes.annotation.HighLight;
import cn.easyes.annotation.IndexField;
import cn.easyes.annotation.IndexId;
import cn.easyes.annotation.IndexName;
import cn.easyes.annotation.rely.FieldType;
import cn.easyes.annotation.rely.IdType;
import lombok.Data;

@Data
@IndexName(value = "document")
public class Document {
    /**
     * es中的唯一id,如果你想自定义es中的id为你提供的id,比如MySQL中的id,请将注解中的type指定为customize,如此id便支持任意数据类型)
     */
    @IndexId(type = IdType.NONE)
    private String id;

    /**
     * 文档标题,不指定类型默认被创建为keyword类型,可进行精确查询
     */
    @IndexField(value = "title")
    private String title;

    /**
     * 文档内容,指定了类型及存储/查询分词器
     */
    @HighLight(mappingField = "highlightContent") //对应下面的highlightContent变量名
    @IndexField(value = "content", fieldType = FieldType.TEXT)
    private String content;

    /**
     * 创建时间
     */
    @IndexField(value = "create_time", fieldType = FieldType.DATE, dateFormat = "yyyy-MM-dd HH:mm:ss")
    private String createTime;

    /**
     * 不存在的字段
     */
    @IndexField(exist = false)
    private String notExistsField;

    /**
     * 地理位置经纬度坐标 例如: "40.13933715136454,116.63441990026217"
     */
    @IndexField(fieldType = FieldType.GEO_POINT)
    private String location;

    /**
     * 图形(例如圆心,矩形)
     */
    @IndexField(fieldType = FieldType.GEO_SHAPE)
    private String geoLocation;

    /**
     * 自定义字段名称
     */
    @IndexField(value = "wu-la")
    private String customField;

    /**
     * 高亮返回值被映射的字段
     */
    private String highlightContent;

}

DocumentMapper (官方建议:Easy-Es的Mapper和MyBatis-Plus分开存放)

Springboot整合Easy-Es,# Spring Boot,搜索引擎:ElasticSearch,elasticsearch,spring boot,搜索引擎
和MyBatis-Plus差不多都是继承mapper,但是Easy-Es不需要继承Service和ServiceImpl

import cn.easyes.core.conditions.interfaces.BaseEsMapper;
import org.springframework.stereotype.Repository;

@Repository
public interface DocumentMapper extends BaseEsMapper<Document> {

}

DocumentService

人家没有帮我们写,那我们就自己写

public interface DocumentService {

    Integer insert(Document document);

    Document selectById(String id);

    Integer updateById(Document document);

    Integer deleteById(String id);

    List<Document> selectList();

    EsPageInfo<Document> pageQuery(Integer pageIndex, Integer pageSize);

    Integer deleteBatchIds(List<String> ids);
}

DocumentServiceImpl

@Service
public class DocumentServiceImpl implements DocumentService {
    @Resource
    private DocumentMapper documentMapper;

    @Override
    public Integer insert(Document document) {
        return this.documentMapper.insert(document);
    }

    @Override
    public Document selectById(String id) {
        return this.documentMapper.selectById(id);
    }

    @Override
    public Integer updateById(Document document) {
        return this.documentMapper.updateById(document);
    }

    @Override
    public Integer deleteById(String id) {
        return this.documentMapper.deleteById(id);
    }

    @Override
    public List<Document> selectList() {
        LambdaEsQueryWrapper<Document> leqw = new LambdaEsQueryWrapper<>();
        return this.documentMapper.selectList(leqw);
    }

    @Override
    public EsPageInfo<Document> pageQuery(Integer pageIndex, Integer pageSize) {
        LambdaEsQueryWrapper<Document> leqw = new LambdaEsQueryWrapper<>();
        return this.documentMapper.pageQuery(leqw, pageIndex, pageSize);
    }

    @Override
    public Integer deleteBatchIds(List<String> ids) {
        return this.documentMapper.deleteBatchIds(ids);
    }

}

DocumentController

@RestController
@RequestMapping("document")
public class DocumentController {

    @Resource
    private DocumentService documentService;

    /**
     * 测试程序是否正常运行
     */
    @GetMapping("hello")
    public String hello() {
        return "hello world!";
    }

    /**
     * 新增
     */
    @PostMapping
    public Integer insert(@RequestBody Document document) {
        return this.documentService.insert(document);
    }

    /**
     * 查询
     */
    @GetMapping("{id}")
    public Document selectById(@PathVariable String id) {
        return this.documentService.selectById(id);
    }

    /**
     * 更新
     */
    @PutMapping
    public Integer updateById(@RequestBody Document document) {
        return this.documentService.updateById(document);
    }

    /**
     * 删除
     */
    @DeleteMapping("{id}")
    public Integer deleteById(@PathVariable String id) {
        return this.documentService.deleteById(id);
    }

    /**
     * 查询列表
     */
    @GetMapping
    public List<Document> selectList() {
        return this.documentService.selectList();
    }

    /**
     * 分页查询
     */
    @GetMapping("paging")
    public EsPageInfo<Document> pageQuery(@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                                          @RequestParam(value = "pageSize", defaultValue = "1") Integer pageSize) {
        return this.documentService.pageQuery(pageIndex, pageSize);
    }

    /**
     * 批量删除
     */
    @DeleteMapping
    public Integer deleteBatchIds(@RequestBody List<String> ids) {
        return this.documentService.deleteBatchIds(ids);
    }

}

Application启动类加上扫描EsMapper接口注解

@SpringBootApplication
@EsMapperScan("com.aoxqwl.eedemo.mapper.ee") //还是那句话,建议和MyBatis-Plus的mapper分开存放
public class EedemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(EedemoApplication.class, args);
    }
}

结束语

整体下来和MyBatis-Plus是非常贴合的,如果你的项目中使用到了MyBatis-Plus,那么首选是Easy-Es!同样的Easy-Es目前占卜支持SpringBoot3.X,也是个美中不足吧。但是我想目前大家的生产环境都没上SpringBoot3.X吧!接口那些就自己用Postman测一下吧,都是没有问题的。文章来源地址https://www.toymoban.com/news/detail-692838.html

到了这里,关于Springboot整合Easy-Es的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • easy-es 使用

    1、pom中引入依赖 2、yml文件增加配置 3、实体索引 4、mapper 5、service 6、controller 7、新增或修改数据 8、浅分页  9、searchAfter 分页,必须有一个唯一的排序属性,例如:id 下一页: 当使用  searchAfter分页时,想使用销量、价格 等排序来展示数据,需要结合一个唯一且连续的属

    2024年02月11日
    浏览(35)
  • Easy-Es笔记

    Easy-Es(简称EE)是一款由国内开发者打造并完全开源的ElasticSearch-ORM框架。在原生 RestHighLevelClient 的基础上,只做增强不做改变,为简化开发、提高效率而生。Easy-Es采用和MP一致的语法设计,降低ElasticSearch搜索引擎使用门槛,和额外学习成本,并大幅减少开发者工作量,帮助

    2024年02月15日
    浏览(33)
  • ES增强框架easy-es

    因为最近做的功能是关于舆情的,所以数据量比较大的,本来打算用MySQL做时间分表来做,但是经过一段时间的测试,发现数据量太大,用时间分表不能满足性能的要求,所以决定将数据存储改为ES,但是短时间内改底层框架又不是一个小工程,时间上不允许,所以找到了一个很合适的框架

    2024年04月25日
    浏览(29)
  • easy-es使用详解与源码解析

    1.git clone后,easy-es-core中的pom中需要引入: 2.easy-es-sample 中提供了基本案例,可以用来解析源码。 3.easy-es-common中的pom里可以看到,它是基于elasticsearch-rest-high-level-client的。 如果不熟悉elasticsearch-rest-high-level-client,建议先熟悉一下。 查询所有:match_all (一般也就是测试用用)

    2024年02月09日
    浏览(49)
  • ES商品搜索实战 Easy-Es搭配原生SearchSourceBuilder

    组装查询条件LambdaEsQueryWrapper,包含查询条件,排序,分页,高亮,去重 这里是搜索的条件组装,封装BoolQueryBuilder 这里就是方法的入口,productMapper.selectList(wrapper)和Mybatis-Plus的写法基本上一摸一样,如果不懂可以去看一下EE官方使用方法 Easy-Es

    2024年02月17日
    浏览(39)
  • Easy-Es框架实践测试整理 基于ElasticSearch的ORM框架

    Easy-Es(简称EE)是一款基于ElasticSearch(简称Es)官方提供的RestHighLevelClient打造的ORM开发框架,在 RestHighLevelClient 的基础上,只做增强不做改变,为简化开发、提高效率而生。EE是Mybatis-Plus的Es平替版,在有些方面甚至比MP更简单,同时也融入了更多Es独有的功能,助力您快速实现各种场

    2024年01月16日
    浏览(45)
  • 关于这款开源的ES的ORM框架-Easy-Es适合初学者入手不?

    最近笔者为了捡回以前自学的ES知识,准备重新对ES的一些基础使用做个大致学习总结。然后在摸鱼逛开源社区时无意中发现了一款不错的ElasticSearch插件-Easy-ES,可称之为“ES界的MyBatis-Plus”。联想到之前每次用RestHighLevelClient写一些DSL操作时都很麻烦(复杂点的搜索代码量确实

    2024年02月07日
    浏览(45)
  • 【别再做XX外卖啦!和我从零到1编写Mini版Easy-ES】完成一个Mapper模型

    作者:沈自在 代码仓库:https://gitee.com/tian-haoran/mini-easy-es 本节教程分支:https://gitee.com/tian-haoran/mini-easy-es/tree/course_02_create_mapper/ ⚠️注意:本项目会持续更新,直到功能完善 1.1.1 什么是 FactoryBean接口? 很多同学都知道 BeanFactory 接口,这个是大名鼎鼎的Spring中的核心接口,

    2024年02月04日
    浏览(44)
  • 运用easy-es保存数据时,报错:cn.easyes.common.exception.EasyEsException: no such method:

    cn.easyes.common.exception.EasyEsException: no such method:     at cn.easyes.common.utils.ExceptionUtils.eee(ExceptionUtils.java:39)     at cn.easyes.core.cache.BaseCache.lambda$setterMethod$6(BaseCache.java:127)     at cn.easyes.core.cache.BaseCache$$Lambda$2307/809171830.get(Unknown Source)     at java.util.Optional.orElseThrow(Optional.java:29

    2024年02月03日
    浏览(29)
  • springboot 整合ES

    springboot整个es有四种方法,分别是TransportClient、 RestClient 、SpringData-Es、Elasticsearch-SQL。 官方推荐的是 RestClient 创建SpringBoot工程,引入依赖,在spring-boot-dependencies中所依赖的ES版本位可能不一致,要改掉 1.1 添加文档

    2024年02月16日
    浏览(28)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包