20250720 es向量检索(引入依赖的版本等问题调试)

This commit is contained in:
liangjinglin 2025-07-20 22:55:05 +08:00
parent c17113c9be
commit 276cccf506
4 changed files with 114 additions and 9 deletions

34
pom.xml
View File

@ -7,7 +7,7 @@
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.6</version> <version>3.2.5</version>
<relativePath/> <relativePath/>
</parent> </parent>
@ -19,6 +19,8 @@
<properties> <properties>
<java.version>17</java.version> <java.version>17</java.version>
<!-- 需要用8.15.0版本因为旧的版本KnnSearch.k方法的入参是long但是新版改成int了 -->
<elasticsearch.version>8.15.0</elasticsearch.version>
</properties> </properties>
<dependencies> <dependencies>
@ -44,28 +46,37 @@
<artifactId>spring-ai-starter-model-deepseek</artifactId> <artifactId>spring-ai-starter-model-deepseek</artifactId>
</dependency> </dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.ai</groupId>-->
<!-- <artifactId>spring-ai-starter-model-zhipuai</artifactId>-->
<!-- </dependency>-->
<dependency> <dependency>
<groupId>org.springframework.ai</groupId> <groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-starter-model-zhipuai</artifactId> <artifactId>spring-ai-alibaba-starter-dashscope</artifactId>
</dependency> </dependency>
<!-- Elasticsearch Vector Store --> <!-- Elasticsearch Vector Store -->
<!-- <dependency>-->
<!-- <groupId>org.springframework.ai</groupId>-->
<!-- <artifactId>spring-ai-starter-vector-store-elasticsearch</artifactId>-->
<!-- </dependency>-->
<dependency> <dependency>
<groupId>org.springframework.ai</groupId> <groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-elasticsearch</artifactId> <artifactId>spring-ai-elasticsearch-store</artifactId>
</dependency> </dependency>
<!-- 对于Spring Boot 3.3.6 + Spring AI 1.0.0显式指定Elasticsearch版本 --> <!-- 对于Spring Boot 3.3.6 + Spring AI 1.0.0显式指定Elasticsearch版本 -->
<dependency> <dependency>
<groupId>co.elastic.clients</groupId> <groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId> <artifactId>elasticsearch-java</artifactId>
<version>8.13.4</version> <version>${elasticsearch.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.elasticsearch.client</groupId> <groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId> <artifactId>elasticsearch-rest-client</artifactId>
<version>8.13.4</version> <version>${elasticsearch.version}</version>
</dependency> </dependency>
<!-- Jackson Core 和 Databind for JSON processing --> <!-- Jackson Core 和 Databind for JSON processing -->
@ -125,6 +136,17 @@
<type>pom</type> <type>pom</type>
<scope>import</scope> <scope>import</scope>
</dependency> </dependency>
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
</dependencies> </dependencies>
</dependencyManagement> </dependencyManagement>

View File

@ -0,0 +1,72 @@
package com.example.springaidemo.config;
import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.message.BasicHeader;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.springframework.ai.embedding.EmbeddingModel;
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.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
@Configuration
public class ElasticVectorConfig {
@Value("${elasticsearch.host:154.12.80.119}")
private String host;
@Value("${elasticsearch.port:9200}")
private int port;
@Value("${elasticsearch.username:elastic}")
private String username;
@Value("${elasticsearch.password:123456}")
private String password;
@Value("${elasticsearch.index.name:custom-index}")
private String indexName;
@Value("${elasticsearch.index.dimensions:1536}")
private int dimensions;
@Bean
public RestClient restClient() {
RestClientBuilder builder = RestClient.builder(new HttpHost(host, port, null));
// 如果提供了用户名和密码添加基础认证
if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
String auth = username + ":" + password;
String encodedAuth = Base64.getEncoder()
.encodeToString(auth.getBytes(StandardCharsets.UTF_8));
builder.setDefaultHeaders(new Header[]{
new BasicHeader("Authorization", "Basic " + encodedAuth)
});
}
return builder.build();
}
@Bean
public VectorStore vectorStore(RestClient restClient, EmbeddingModel dashscopeEmbeddingModel) {
ElasticsearchVectorStoreOptions options = new ElasticsearchVectorStoreOptions();
options.setIndexName(indexName);
options.setSimilarity(SimilarityFunction.cosine);
options.setDimensions(dimensions);
return ElasticsearchVectorStore.builder(restClient, dashscopeEmbeddingModel)
.options(options)
.initializeSchema(true)
.build();
}
}

View File

@ -3,7 +3,6 @@ package com.example.springaidemo.service;
import org.springframework.ai.document.Document; import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest; import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore; import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.elasticsearch.ElasticsearchVectorStore;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -20,10 +19,14 @@ public class VectorService {
new Document("The World is Big and Salvation Lurks Around the Corner"), new Document("The World is Big and Salvation Lurks Around the Corner"),
new Document("You walk forward facing the past and you turn back toward the future.")); new Document("You walk forward facing the past and you turn back toward the future."));
// Add the documents to Elasticsearch // Add the documents to Elasticsearch 需要注意的是elasticSearch的版本需要大于8.10.0
vectorStore.add(documents); vectorStore.add(documents);
// Retrieve documents similar to a query // Retrieve documents similar to a query
List<Document> results = this.vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build()); SearchRequest searchRequest = SearchRequest.builder().query("Spring")
.topK(5)
.build();
List<Document> results = this.vectorStore.similaritySearch(searchRequest);
System.out.println(results);
} }
} }

View File

@ -8,6 +8,11 @@ spring:
ai: ai:
deepseek: deepseek:
api-key: sk-3043bb4777404970a22c7544dd30aaa2 api-key: sk-3043bb4777404970a22c7544dd30aaa2
dashscope:
api-key: sk-2f703a41fff0488e9b6888013d2ee58a
chat:
options:
model: qwen-plus
zhipuai: zhipuai:
api-key: 73f440ddeafc47ba94ed66e35fbd63d7.VmlulRZ4BMWexncF api-key: 73f440ddeafc47ba94ed66e35fbd63d7.VmlulRZ4BMWexncF
openai: openai:
@ -29,6 +34,9 @@ spring:
index-name: custom-index index-name: custom-index
dimensions: 1536 dimensions: 1536
similarity: cosine similarity: cosine
uris: http://154.12.80.119:9200
username: elastic
password: 123456
# ollama: # ollama:
# base-url: ${OLLAMA_BASE_URL:http://localhost:11434} # base-url: ${OLLAMA_BASE_URL:http://localhost:11434}
# chat: # chat: