SpringBoot 快速整合spring-data-elasticsearch

spring-boot.version : 2.5.15

1.引入依赖

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-data-elasticsearch</artifactId>

</dependency>

2.配置application.properties

spring.elasticsearch.rest.uris=http://127.0.0.1:9200

spring.data.elasticsearch.repositories.enabled=true

spring.data.elasticsearch.client.reactive.endpoints=127.0.0.1:9200

3.启动类添加注解:@EnableElasticsearchRepositories(basePackages = {"com.xxx.dao"})

4.编写文档对象实体类

@Data

@Document(indexName = "products", createIndex = true)

public class Product implements Serializable {

private static final long serialVersionUID = -7509473661142683567L;

@Id

private Long id;

@Field(type = FieldType.Keyword)

private String title;

@Field(type = FieldType.Float)

private Double price;

@Field(type = FieldType.Text)

private String desc;

}

5.编写 Repository 接口 - @EnableElasticsearchRepositories 注解要去扫描的接口

import org.springframework.data.domain.Page;

import org.springframework.data.domain.Pageable;

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

import org.springframework.stereotype.Repository;

import com.sky.biz.doc.Product;

@Repository -- 该注解一般可以省略

public interface ProductRepository extends ElasticsearchRepository<Product, Long> {

Page<Product> findByTitle(String word, Pageable pageable);

}

6.CRUD - 业务操作

@Autowired

private ProductRepository productRepository;

@Test

public void testSave() {

Product product = new Product();

product.setId(1L);

product.setTitle("Java虚拟机调优实战");

product.setPrice(36.00);

product.setDesc("图灵奖获奖图书");

Product result = productRepository.save(product);

System.out.println("保存成功:" + JSONObject.toJSONString(result));

}

@Test

public void testQuery() {

Optional<Product> productOptional = productRepository.findById(1L);

if(productOptional.isPresent()) {

Product product = productOptional.get();

System.out.println("ES查询结果:" + JSONObject.toJSONString(product));

}

}

7.重要说明:

spring-data-elasticsearch 一般主要用于简单对象的CRUD操作,

对于业务比较复杂的操作建议还是需要使用其他的java client来补充操作了,比如HighLevelRestClient,

不过大家要了解的是es官方已经不推荐使用Rest Client和Transport Client了,并且会在es8.x时废除。

从7.15版本开始更加推荐使用Java Client,但是因为学习成本、使用习惯以及很多公司仍然在使用6.x或更早版本的es,

所以在es7.x版本或更早,HighLevelRestClient 还是不错的选择

相关推荐
...对方正在输入......18 分钟前
Elasticsearch集群部署实战指南
elasticsearch·jenkins
Hello.Reader1 小时前
使用 Flink CDC 搭建跨库 Streaming ETLMySQL + Postgres → Elasticsearch 实战
大数据·elasticsearch·flink
Q_Q5110082851 小时前
python+django/flask的结合人脸识别和实名认证的校园论坛系统
spring boot·python·django·flask·node.js·php
Q_Q5110082851 小时前
python+django/flask的选课系统与课程评价整合系统
spring boot·python·django·flask·node.js·php
老华带你飞1 小时前
社区养老保障|智慧养老|基于springboot+小程序社区养老保障系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·小程序·毕设·社区养老保障
q***87601 小时前
springboot下使用druid-spring-boot-starter
java·spring boot·后端
东南门吹雪1 小时前
Spring的Bean相关
java·spring·bean·aop
q***69771 小时前
Y20030018基于Java+Springboot+mysql+jsp+layui的家政服务系统的设计与实现 源代码 文档
java·spring boot·mysql
Elasticsearch2 小时前
Elastic AI agent builder 介绍(五)- 备份 tools 及 agents
elasticsearch