You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add approximate k-NN search support to OpenSearchVectorStore
This commit adds HNSW-based approximate k-NN search as an alternative to
the existing exact k-NN search, providing better performance and
scalability for large vector datasets.
Key changes:
- Add useApproximateKnn, dimensions, and similarity configuration options
- Implement buildApproximateQuery() using OpenSearch native knn query
- Maintain buildExactQuery() for backwards compatibility (default)
- Add comprehensive test coverage for both exact and approximate modes
- Test multiple similarity functions: cosinesimil, l1, l2, linf, innerproduct
- Fix test isolation by using dedicated beans and indexes per similarity function
- Update documentation with new properties and usage examples
The implementation maintains full backward compatibility with approximate
k-NN disabled by default.
Signed-off-by: Jemin Huh <hjm1980@gmail.com>
Co-authored-by: Soby Chacko <soby.chacko@broadcom.com>
Copy file name to clipboardExpand all lines: auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-opensearch/src/main/java/org/springframework/ai/vectorstore/opensearch/autoconfigure/OpenSearchVectorStoreAutoConfiguration.java
+8-3Lines changed: 8 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -88,14 +88,19 @@ OpenSearchVectorStore vectorStore(OpenSearchVectorStoreProperties properties, Op
Copy file name to clipboardExpand all lines: auto-configurations/vector-stores/spring-ai-autoconfigure-vector-store-opensearch/src/main/java/org/springframework/ai/vectorstore/opensearch/autoconfigure/OpenSearchVectorStoreProperties.java
+30Lines changed: 30 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -38,6 +38,12 @@ public class OpenSearchVectorStoreProperties extends CommonVectorStoreProperties
38
38
39
39
privateStringpassword;
40
40
41
+
privateBooleanuseApproximateKnn;
42
+
43
+
privateIntegerdimensions;
44
+
45
+
privateStringsimilarity;
46
+
41
47
privateStringmappingJson;
42
48
43
49
/**
@@ -100,6 +106,30 @@ public String getMappingJson() {
Copy file name to clipboardExpand all lines: spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs/opensearch.adoc
+6-1Lines changed: 6 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -108,7 +108,10 @@ Properties starting with `spring.ai.vectorstore.opensearch.*` are used to config
108
108
|`spring.ai.vectorstore.opensearch.password`| Password for the specified username | -
109
109
|`spring.ai.vectorstore.opensearch.index-name`| Name of the index to store vectors | `spring-ai-document-index`
110
110
|`spring.ai.vectorstore.opensearch.initialize-schema`| Whether to initialize the required schema | `false`
111
-
|`spring.ai.vectorstore.opensearch.similarity-function`| The similarity function to use | `cosinesimil`
111
+
|`spring.ai.vectorstore.opensearch.similarity-function`| The similarity function to use (cosinesimil, l1, l2, linf, innerproduct) | `cosinesimil`
112
+
|`spring.ai.vectorstore.opensearch.use-approximate-knn`| Whether to use approximate k-NN for faster searches. If true, uses HNSW-based approximate search. If false, uses exact brute-force k-NN. See link:https://opensearch.org/docs/latest/search-plugins/knn/approximate-knn/[Approximate k-NN] and link:https://opensearch.org/docs/latest/search-plugins/knn/knn-score-script/[Exact k-NN] | `false`
113
+
|`spring.ai.vectorstore.opensearch.dimensions`| Number of dimensions for vector embeddings. Used when creating index mapping for approximate k-NN. If not set, uses the embedding model's dimensions. | `1536`
114
+
|`spring.ai.vectorstore.opensearch.mapping-json`| Custom JSON mapping for the index. Overrides default mapping generation. | -
112
115
|`spring.ai.vectorstore.opensearch.read-timeout`| Time to wait for response from the opposite endpoint. 0 - infinity. | -
113
116
|`spring.ai.vectorstore.opensearch.connect-timeout`| Time to wait until connection established. 0 - infinity. | -
114
117
|`spring.ai.vectorstore.opensearch.path-prefix`| Path prefix for OpenSearch API endpoints. Useful when OpenSearch is behind a reverse proxy with a non-root path. | -
@@ -191,6 +194,8 @@ public VectorStore vectorStore(OpenSearchClient openSearchClient, EmbeddingModel
Copy file name to clipboardExpand all lines: vector-stores/spring-ai-opensearch-store/src/main/java/org/springframework/ai/vectorstore/opensearch/OpenSearchVectorStore.java
+102-5Lines changed: 102 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -170,6 +170,10 @@ public class OpenSearchVectorStore extends AbstractObservationVectorStore implem
170
170
171
171
privateStringsimilarityFunction;
172
172
173
+
privatefinalbooleanuseApproximateKnn;
174
+
175
+
privatefinalintdimensions;
176
+
173
177
/**
174
178
* Creates a new OpenSearchVectorStore using the builder pattern.
0 commit comments