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。

相关推荐
14L7 小时前
互联网大厂Java面试:从Spring Cloud到Kafka的技术考察
spring boot·redis·spring cloud·kafka·jwt·oauth2·java面试
地藏Kelvin8 小时前
Spring Ai 从Demo到搭建套壳项目(二)实现deepseek+MCP client让高德生成昆明游玩4天攻略
人工智能·spring boot·后端
一个有女朋友的程序员8 小时前
Spring Boot 缓存注解详解:@Cacheable、@CachePut、@CacheEvict(超详细实战版)
spring boot·redis·缓存
wh_xia_jun8 小时前
在 Spring Boot 中使用 JSP
java·前端·spring boot
yuren_xia9 小时前
在Spring Boot中集成Redis进行缓存
spring boot·redis·缓存
yuren_xia9 小时前
Spring Boot + MyBatis 集成支付宝支付流程
spring boot·tomcat·mybatis
我爱Jack11 小时前
Spring Boot统一功能处理深度解析
java·spring boot·后端
Elastic 中国社区官方博客11 小时前
Elastic 获得 AWS 教育 ISV 合作伙伴资质,进一步增强教育解决方案产品组合
大数据·人工智能·elasticsearch·搜索引擎·云计算·全文检索·aws
RainbowJie112 小时前
Spring Boot 使用 SLF4J 实现控制台输出与分类日志文件管理
spring boot·后端·单元测试
面朝大海,春不暖,花不开12 小时前
Spring Boot MVC自动配置与Web应用开发详解
前端·spring boot·mvc