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

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。

http://www.kler.cn/a/547064.html

相关文章:

  • Linux系统使用ollama本地安装部署DeepSeekR1 + open-webui
  • 字玩FontPlayer开发笔记14 Vue3实现多边形工具
  • wps接入DeepSeek教程
  • uniapp语音时的动态音波的实现
  • SpringBoot中Mybatis记录执行sql日志
  • 基于 STM32 平台的音频特征提取与歌曲风格智能识别系统
  • TypeScript装饰器 ------- 学习笔记分享
  • DeepSeek 模型部署与使用技术评测(基于阿里云零门槛解决方案)
  • 部署onlyoffice后,php版的callback及小魔改(logo和关于)
  • Java项目引入DeepSeek搭建私有AI
  • React历代主要更新
  • 使用EVE-NG-锐捷实现NAT
  • 尚硅谷爬虫note003
  • 微软AutoGen介绍——Managing State保存并加载持续会话的Agents和Teams
  • ML.NET库学习006:成人人口普查数据分析与分类预测
  • 第十一篇:EMC的“电磁护盾”——三电系统干扰抑制实战
  • uniapp中对于文件和文件夹的处理,内存的查询
  • 132,【1】 buuctf web [EIS 2019]EzPOP
  • Scrapy:任务队列底层设计详解
  • Unity 接入Tripo API 文生模型,模型制作动画并下载使用