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。

相关推荐
IT毕设实战小研2 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
一只爱撸猫的程序猿3 小时前
使用Spring AI配合MCP(Model Context Protocol)构建一个"智能代码审查助手"
spring boot·aigc·ai编程
甄超锋4 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
武昌库里写JAVA6 小时前
JAVA面试汇总(四)JVM(一)
java·vue.js·spring boot·sql·学习
Pitayafruit7 小时前
Spring AI 进阶之路03:集成RAG构建高效知识库
spring boot·后端·llm
zru_96027 小时前
Spring Boot 单元测试:@SpyBean 使用教程
spring boot·单元测试·log4j
甄超锋8 小时前
Java Maven更换国内源
java·开发语言·spring boot·spring·spring cloud·tomcat·maven
还是鼠鼠9 小时前
tlias智能学习辅助系统--Maven 高级-私服介绍与资源上传下载
java·spring boot·后端·spring·maven
水无痕simon12 小时前
5 索引的操作
数据库·elasticsearch
舒一笑13 小时前
Started TttttApplication in 0.257 seconds (没有 Web 依赖导致 JVM 正常退出)
jvm·spring boot·后端