Skip to content

Commit c66dfbf

Browse files
committed
GH-1589 Fix OpenSearch client indices mapping
- Fix the embedding dimension configuration for opensearch client indices mapping - The dimension config is obtained by the underlying embedding model's dimension Resolves #1589
1 parent 6acd202 commit c66dfbf

File tree

4 files changed

+8
-9
lines changed

4 files changed

+8
-9
lines changed

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/vectorstore/opensearch/OpenSearchVectorStoreAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ OpenSearchVectorStore vectorStore(OpenSearchVectorStoreProperties properties, Op
7575
BatchingStrategy batchingStrategy) {
7676
var indexName = Optional.ofNullable(properties.getIndexName()).orElse(OpenSearchVectorStore.DEFAULT_INDEX_NAME);
7777
var mappingJson = Optional.ofNullable(properties.getMappingJson())
78-
.orElse(OpenSearchVectorStore.DEFAULT_MAPPING_EMBEDDING_TYPE_KNN_VECTOR_DIMENSION_1536);
78+
.orElse(OpenSearchVectorStore.DEFAULT_MAPPING_EMBEDDING_TYPE_KNN_VECTOR_DIMENSION);
7979
return new OpenSearchVectorStore(indexName, openSearchClient, embeddingModel, mappingJson,
8080
properties.isInitializeSchema(), observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP),
8181
customObservationConvention.getIfAvailable(() -> null), batchingStrategy);

vector-stores/spring-ai-opensearch-store/src/main/java/org/springframework/ai/vectorstore/OpenSearchVectorStore.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,12 @@ public class OpenSearchVectorStore extends AbstractObservationVectorStore implem
6969

7070
public static final String DEFAULT_INDEX_NAME = "spring-ai-document-index";
7171

72-
public static final String DEFAULT_MAPPING_EMBEDDING_TYPE_KNN_VECTOR_DIMENSION_1536 = """
72+
public static final String DEFAULT_MAPPING_EMBEDDING_TYPE_KNN_VECTOR_DIMENSION = """
7373
{
7474
"properties":{
7575
"embedding":{
7676
"type":"knn_vector",
77-
"dimension":1536
77+
"dimension":%s
7878
}
7979
}
8080
}
@@ -100,8 +100,7 @@ public class OpenSearchVectorStore extends AbstractObservationVectorStore implem
100100

101101
public OpenSearchVectorStore(OpenSearchClient openSearchClient, EmbeddingModel embeddingModel,
102102
boolean initializeSchema) {
103-
this(openSearchClient, embeddingModel, DEFAULT_MAPPING_EMBEDDING_TYPE_KNN_VECTOR_DIMENSION_1536,
104-
initializeSchema);
103+
this(openSearchClient, embeddingModel, DEFAULT_MAPPING_EMBEDDING_TYPE_KNN_VECTOR_DIMENSION, initializeSchema);
105104
}
106105

107106
public OpenSearchVectorStore(OpenSearchClient openSearchClient, EmbeddingModel embeddingModel, String mappingJson,
@@ -263,7 +262,7 @@ private CreateIndexResponse createIndexMapping(String index, String mappingJson)
263262
@Override
264263
public void afterPropertiesSet() {
265264
if (this.initializeSchema && !exists(this.index)) {
266-
createIndexMapping(this.index, this.mappingJson);
265+
createIndexMapping(this.index, String.format(this.mappingJson, this.embeddingModel.dimensions()));
267266
}
268267
}
269268

vector-stores/spring-ai-opensearch-store/src/test/java/org/springframework/ai/vectorstore/OpenSearchVectorStoreIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ public OpenSearchVectorStore anotherVectorStore(EmbeddingModel embeddingModel) {
411411
new OpenSearchClient(ApacheHttpClient5TransportBuilder
412412
.builder(HttpHost.create(opensearchContainer.getHttpHostAddress()))
413413
.build()),
414-
embeddingModel, OpenSearchVectorStore.DEFAULT_MAPPING_EMBEDDING_TYPE_KNN_VECTOR_DIMENSION_1536,
414+
embeddingModel, OpenSearchVectorStore.DEFAULT_MAPPING_EMBEDDING_TYPE_KNN_VECTOR_DIMENSION,
415415
true);
416416
}
417417
catch (URISyntaxException e) {

vector-stores/spring-ai-opensearch-store/src/test/java/org/springframework/ai/vectorstore/OpenSearchVectorStoreObservationIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,8 @@ public OpenSearchVectorStore vectorStore(EmbeddingModel embeddingModel,
209209
new OpenSearchClient(ApacheHttpClient5TransportBuilder
210210
.builder(HttpHost.create(opensearchContainer.getHttpHostAddress()))
211211
.build()),
212-
embeddingModel, OpenSearchVectorStore.DEFAULT_MAPPING_EMBEDDING_TYPE_KNN_VECTOR_DIMENSION_1536,
213-
true, observationRegistry, null, new TokenCountBatchingStrategy());
212+
embeddingModel, OpenSearchVectorStore.DEFAULT_MAPPING_EMBEDDING_TYPE_KNN_VECTOR_DIMENSION, true,
213+
observationRegistry, null, new TokenCountBatchingStrategy());
214214
}
215215
catch (URISyntaxException e) {
216216
throw new RuntimeException(e);

0 commit comments

Comments
 (0)