Springboot整合ES

  1. 添加依赖
    在 pom.xml 中添加以下依赖:

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
  2. 配置 Elasticsearch
    在 application.properties 中配置 Elasticsearch 连接信息:

    spring.elasticsearch.uris=http://localhost:9200

  3. 创建实体类
    使用 @Document 注解标记实体类:

    import org.springframework.data.annotation.Id;
    import org.springframework.data.elasticsearch.annotations.Document;

    @Document(indexName = "products")
    public class Product {
    @Id
    private String id;
    @Field(type = FieldType.Text, name = "name")
    private String name;
    @Field(type = FieldType.Double, name = "price")
    private double price;
    @Field(type = FieldType.Date, name = "created_at")
    private Date createdAt;

    复制代码
     // Getters and Setters

    }

  4. 创建 Repository 接口
    继承 ElasticsearchRepository 接口:

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

    public interface ProductRepository extends ElasticsearchRepository<Product, String> {
    }

  5. 使用 Repository
    在服务类中注入 ProductRepository 并使用:

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;

    @Service
    public class ProductService {

    复制代码
     @Autowired
     private ProductRepository productRepository;
    
     public void saveProduct(Product product) {
         productRepository.save(product);
     }
    
     public Product findProductById(String id) {
         return productRepository.findById(id).orElse(null);
     }

    }

  6. 测试
    编写测试类验证 Elasticsearch 集成:

    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;

    @SpringBootTest
    public class ProductServiceTest {

    复制代码
     @Autowired
     private ProductService productService;
    
     @Test
     public void testSaveAndFindProduct() {
         Product product = new Product();
         product.setId("1");
         product.setName("Laptop");
         product.setPrice(1200.0);
    
         productService.saveProduct(product);
    
         Product foundProduct = productService.findProductById("1");
         System.out.println(foundProduct.getName());
     }

    }

  7. 总结

    依赖: 添加 spring-boot-starter-data-elasticsearch 依赖。
    配置: 在 application.properties 中配置 Elasticsearch 连接信息。
    实体类: 使用 @Document 注解标记实体类。
    Repository: 继承 ElasticsearchRepository 接口。
    使用: 在服务类中注入并使用 Repository。

相关推荐
程序员小单22 分钟前
WebSocket 与 Spring Boot 整合实践
spring boot·websocket·网络协议
q***96581 小时前
深入解析Spring Boot中的@ConfigurationProperties注解
java·spring boot·后端
发现你走远了1 小时前
2025 idea 指定配置环境运行springboot 设置active和env启动端口,多端口启动 (保姆级图文)
java·spring boot·intellij-idea
e***0962 小时前
SpringBoot下获取resources目录下文件的常用方法
java·spring boot·后端
JIngJaneIL3 小时前
远程在线诊疗|在线诊疗|基于java和小程序的在线诊疗系统小程序设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·小程序·毕设·在线诊疗小程序
e***95644 小时前
springboot项目架构
spring boot·后端·架构
q***21604 小时前
Spring Boot项目接收前端参数的11种方式
前端·spring boot·后端
j***12154 小时前
Spring Boot与MyBatis
spring boot·后端·mybatis
optimistic_chen4 小时前
【Java EE进阶 --- SpringBoot】Spring事务传播机制
spring boot·后端·spring·java-ee·事务·事务传播机制
weixin_456904275 小时前
Git大文件管理与版本回退
大数据·git·elasticsearch