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 还是不错的选择

相关推荐
wxin_VXbishe17 分钟前
C#(asp.net)学员竞赛信息管理系统-计算机毕业设计源码28790
java·vue.js·spring boot·spring·django·c#·php
森焱森24 分钟前
详解 Spring Boot、Flask、Nginx、Redis、MySQL 的关系与协作
spring boot·redis·python·nginx·flask
无心水25 分钟前
分布式定时任务与SELECT FOR UPDATE:从致命陷阱到优雅解决方案(实战案例+架构演进)
服务器·人工智能·分布式·后端·spring·架构·wpf
Coder_Boy_1 小时前
Deeplearning4j+ Spring Boot 电商用户复购预测案例
java·人工智能·spring boot·后端·spring
金牌归来发现妻女流落街头1 小时前
【Springboot基础开发】
java·spring boot·后端
历程里程碑2 小时前
普通数组----轮转数组
java·数据结构·c++·算法·spring·leetcode·eclipse
callJJ2 小时前
Spring AI ImageModel 完全指南:用 OpenAI DALL-E 生成图像
大数据·人工智能·spring·openai·springai·图像模型
星辰_mya2 小时前
Elasticsearch线上问题之OOM
大数据·elasticsearch·搜索引擎
Elastic 中国社区官方博客2 小时前
使用 Groq 与 Elasticsearch 进行智能查询
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
是梦终空2 小时前
计算机毕业设计264—基于Springboot+Vue3+协同过滤的房屋租赁管理系统(源代码+数据库+万字论文+设计文档)
spring boot·毕业设计·vue3·课程设计·毕业论文·协同过滤·房屋租赁管理系统