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。

相关推荐
.生产的驴25 分钟前
SpringBoot 接口国际化i18n 多语言返回 中英文切换 全球化 语言切换
java·开发语言·spring boot·后端·前端框架
-曾牛38 分钟前
Spring Boot中@RequestParam、@RequestBody、@PathVariable的区别与使用
java·spring boot·后端·intellij-idea·注解·spring boot 注解·混淆用法
软件20541 分钟前
【UserDetailsService】
spring boot
Best_Liu~2 小时前
TransactionTemplate 与@Transactional 注解的使用
java·开发语言·spring boot·后端
胡斌附体3 小时前
idea启动springboot方式及web调用
java·spring boot·intellij-idea
曹天骄3 小时前
设计并实现一个基于 Java + Spring Boot + MySQL 的通用多租户权限系统
java·spring boot·mysql
~ 小团子4 小时前
前后端分离: vue3+SpringBoot+ElementPlus+Axios+MyBatisPuls
java·spring boot·后端
BillKu4 小时前
Java + Spring Boot + MyBatis获取以及持久化sql语句的方法
java·spring boot·mybatis
m0_564264184 小时前
springboot项目之websocket的坑:spring整合websocket后进行单元测试后报错的解决方案
java·经验分享·spring boot·websocket·spring·单元测试·报错