Springboot整合Easy-Es

版本说明

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分开存放)

在这里插入图片描述
和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测一下吧,都是没有问题的。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些关于Spring Boot整合Easy Elasticsearch的指导。 1. 添加依赖 首先,在`pom.xml`文件中添加Easy ElasticsearchElasticsearch的依赖: ```xml <dependency> <groupId>com.jun</groupId> <artifactId>easy-elasticsearch-spring-boot-starter</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.12.0</version> </dependency> ``` 2. 配置Elasticsearch 在`application.yml`中添加Elasticsearch的配置信息: ```yaml spring: elasticsearch: rest: uris: http://localhost:9200 ``` 3. 创建Elasticsearch的Repository 创建一个继承自`ElasticsearchRepository`的接口,用于定义Elasticsearch的操作方法: ```java public interface BookRepository extends ElasticsearchRepository<Book, Long> { List<Book> findBooksByAuthor(String author); } ``` 其中,`Book`是我们要操作的实体类,`Long`是这个实体类的ID类型。 4. 测试Elasticsearch 可以编写一个测试方法来测试Elasticsearch是否成功整合: ```java @SpringBootTest class BookRepositoryTest { @Autowired private BookRepository bookRepository; @Test public void testSave() { Book book = new Book(); book.setId(1L); book.setTitle("Java编程思想"); book.setAuthor("Bruce Eckel"); bookRepository.save(book); } @Test public void testFind() { List<Book> books = bookRepository.findBooksByAuthor("Bruce Eckel"); System.out.println(books); } } ``` 执行测试方法后,如果能够正确输出结果,则说明Easy Elasticsearch已经成功整合到了Spring Boot中。 希望这些步骤能够对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值