springAi集成es向量库

1. pom

xml 复制代码
<!-- Vector Store for RAG -->
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-vector-store-elasticsearch</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>co.elastic.clients</groupId>
                    <artifactId>elasticsearch-java</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 与自己的es版本对应 -->
        <dependency>
            <groupId>co.elastic.clients</groupId>
            <artifactId>elasticsearch-java</artifactId>
            <version>8.18.8</version>
        </dependency>

2. yaml

yaml 复制代码
spring:
    dashscope:
      api-key: sk-4xxx
      # 生成"结构化大输出/工具调用多轮"时,默认读超时过短会触发 timeout
      # 单位为毫秒(Integer),按需调整
      # 连接建立超时(毫秒)
      connect-timeout: 30000
      read-timeout: 1200000
      embedding:
        options:
          model: text-embedding-v3

3. EsRagConfig 配置类

java 复制代码
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.TokenCountBatchingStrategy;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.elasticsearch.ElasticsearchVectorStore;
import org.springframework.ai.vectorstore.elasticsearch.ElasticsearchVectorStoreOptions;
import org.springframework.ai.vectorstore.elasticsearch.SimilarityFunction;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EsRagConfig {

    @Bean
    public RestClient restClient() {
        return RestClient.builder(new HttpHost("localhost", 9200, "http")) // 配置 ES 地址和端口
                .build();
    }

    @Bean
    public VectorStore vectorStore(RestClient restClient, EmbeddingModel dashscopeEmbeddingModel) {
        ElasticsearchVectorStoreOptions options = new ElasticsearchVectorStoreOptions();
        options.setIndexName("testRag"); // 设置索引名称
        options.setSimilarity(SimilarityFunction.cosine);
        options.setDimensions(1024); // 设置向量维度
        return ElasticsearchVectorStore.builder(restClient, dashscopeEmbeddingModel)
                .options(options)
                .initializeSchema(true) // 没有索引时自动创建索引
                .batchingStrategy(new TokenCountBatchingStrategy())
                .build();
    }

}

然后就可以看官网示例进行rag相关操作了比如:

java 复制代码
// 向量搜索
vectorStore.similaritySearch(
            SearchRequest.builder()
                    .query(userQuestion)
                    .topK(topK)
                    .similarityThreshold(0.3)
                    .build()
        )
相关推荐
TechubNews2 小时前
新火集团首席经济学家付鹏演讲——2026 年是 Crypto 加入到 FICC 资产配置框架元年
大数据·人工智能
Elastic 中国社区官方博客2 小时前
为 Elastic Cloud Serverless 和 Elasticsearch 引入统一的 API 密钥
大数据·运维·elasticsearch·搜索引擎·云原生·serverless
vastsmile5 小时前
(R)26.04.23 hermes agent执行本地命令超级慢的原因
开发语言·elasticsearch·r语言
CS创新实验室5 小时前
CS实验室行业报告:机器人领域就业分析报告
大数据·人工智能·机器人
老陈头聊SEO7 小时前
生成引擎优化(GEO)为内容创作引入新视角与用户体验提升策略
其他·搜索引擎·seo优化
LinuxGeek10249 小时前
Kylin-Server-V11、openEuler-22.03和openEuler-24.03的MySQL 9.7.0版本正式发布
大数据·mysql·kylin
容智信息9 小时前
国家级算力底座+企业级智能体:容智Agent OS 获选入驻移动云能中心,联手赋能千行百业
大数据·人工智能·自然语言处理·智慧城市
Chuer_9 小时前
讲透财务Agent核心概念,深度拆解财务Agent应用趋势
大数据·数据库·安全·数据分析·甘特图
gushinghsjj9 小时前
什么是主数据管理平台?怎么构建主数据管理平台?
大数据·数据库
焦糖玛奇朵婷9 小时前
解锁扭蛋机小程序的五大优势
java·大数据·服务器·前端·小程序