当前位置: 首页 > news >正文

Springboot整合ES

  1. 添加依赖
    在 pom.xml 中添加以下依赖:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

  1. 配置 Elasticsearch
    在 application.properties 中配置 Elasticsearch 连接信息:
spring.elasticsearch.uris=http://localhost:9200
  1. 创建实体类
    使用 @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
}
  1. 创建 Repository 接口
    继承 ElasticsearchRepository 接口:
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;

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

  1. 使用 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);
    }
}

  1. 测试
    编写测试类验证 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());
    }
}

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

相关文章:

  • 【NLP251】命名实体识别常用模块(基于Transformer分类)
  • PyTorch 混合精度训练中的警告处理与代码适配指南
  • 部署 ollama + deepseek + open-webui 遇到的常见问题与解决建议
  • CEF132编译指南 MacOS 篇 - 获取 CEF 源码 (五)
  • SpringBoot异步调用相同类的方法
  • Cloud: aws:network: limit 含有pps这种限制
  • linux deepseek-r1模型安装
  • 详解如何使用Pytest内置Fixture tmp_path 管理临时文件
  • [创业之路-300]:进一步理解货币与金钱, 货币与货币政策
  • Element Plus table 去除行hover效果
  • DeepSeek 使用教程
  • Docker 容器基础:创建、运行、管理
  • 配置 Nginx 以支持 HTTPS
  • 动手学Agent——Day2
  • 2025.2.14——1400
  • DataX使用时常见问题(持续更新)
  • 从零开始人工智能Matlab案例-粒子群优化
  • 【LeetCode】20. 有效的括号
  • dify.ai 配置链接到阿里云百练等云厂商的 DeepSeek 模型
  • 基于SpringBoot的“高考志愿智能推荐系统”的设计与实现(源码+数据库+文档+PPT)
  • 杨文庄当选中国人口学会会长,曾任国家卫健委人口家庭司司长
  • 马上评|安排见义勇为学生补考,善意与善意的双向奔赴
  • 陈吉宁龚正黄莉新胡文容等在警示教育基地参观学习,出席深入贯彻中央八项规定精神学习教育交流会
  • 陕西省安康市汉阴县县长陈永乐已任汉阴县委书记
  • “老中青少”四代同堂,季春艳携锡剧《玲珑女》冲击梅花奖
  • 阿尔巴尼亚执政党连续第四次赢得议会选举,反对党此前雇用特朗普竞选经理