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 builder pattern to OpenSearchVectorStore and refactor package name
Add builder pattern to OpenSearchVectorStore
Introduces a builder pattern for OpenSearchVectorStore configuration and
refactors the package structure to org.springframework.ai.vectorstore.opensearch
for better organization and consistency with other vector stores.
The builder pattern improves usability by:
* Providing a fluent API for configuring store instances
* Making configuration options more discoverable through method names
* Enabling better validation of configuration parameters
* Supporting optional parameters with sensible defaults
* The package refactoring aligns with the project's standard package naming
conventions and improves code organization. All constructors are deprecated
in favor of the new builder pattern to guide users toward the preferred
configuration approach.
This section guides you through setting up the OpenSearch `VectorStore` to store document embeddings and perform similarity searches.
3
+
This section walks you through setting up `OpenSearchVectorStore` to store document embeddings and perform similarity searches.
4
4
5
-
link:https://opensearch.org[OpenSearch] is an open-source search and analytics engine originally forked from Elasticsearch, distributed under the Apache License 2.0. It enhances AI application development by simplifying the integration and management of AI-generated assets. OpenSearch supports vector, lexical, and hybrid search capabilities, leveraging advanced vector database functionalities to facilitate low-latency queries and similarity searches as detailed on the link:https://opensearch.org/platform/search/vector-database.html[vector database page]. This platform is ideal for building scalable AI-driven applications and offers robust tools for data management, fault tolerance, and resource access controls.
5
+
link:https://opensearch.org[OpenSearch] is an open-source search and analytics engine originally forked from Elasticsearch, distributed under the Apache License 2.0. It enhances AI application development by simplifying the integration and management of AI-generated assets. OpenSearch supports vector, lexical, and hybrid search capabilities, leveraging advanced vector database functionalities to facilitate low-latency queries and similarity searches as detailed on the link:https://opensearch.org/platform/search/vector-database.html[vector database page].
6
+
7
+
The link:https://opensearch.org/docs/latest/search-plugins/knn/index/[OpenSearch k-NN] functionality allows users to query vector embeddings from large datasets. An embedding is a numerical representation of a data object, such as text, image, audio, or document. Embeddings can be stored in the index and queried using various similarity functions.
6
8
7
9
== Prerequisites
8
10
9
11
* A running OpenSearch instance. The following options are available:
* `EmbeddingModel` instance to compute the document embeddings. Several options are available:
13
-
- If required, an API key for the xref:api/embeddings.adoc#available-implementations[EmbeddingModel] to generate the
14
-
embeddings stored by the `OpenSearchVectorStore`.
14
+
* If required, an API key for the xref:api/embeddings.adoc#available-implementations[EmbeddingModel] to generate the embeddings stored by the `OpenSearchVectorStore`.
15
15
16
-
== Dependencies
16
+
== Auto-configuration
17
17
18
-
Add the OpenSearch Vector Store dependency to your project:
18
+
Spring AI provides Spring Boot auto-configuration for the OpenSearch Vector Store.
19
+
To enable it, add the following dependency to your project's Maven `pom.xml` file:
Please have a look at the list of xref:#_configuration_properties[configuration parameters] for the vector store to learn about the default values and configuration options.
60
+
61
+
Additionally, you will need a configured `EmbeddingModel` bean. Refer to the xref:api/embeddings.adoc#available-implementations[EmbeddingModel] section for more information.
62
+
63
+
Now you can auto-wire the `OpenSearchVectorStore` as a vector store in your application:
64
+
65
+
[source,java]
66
+
----
67
+
@Autowired VectorStore vectorStore;
68
+
69
+
// ...
70
+
71
+
List<Document> documents = List.of(
72
+
new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),
73
+
new Document("The World is Big and Salvation Lurks Around the Corner"),
74
+
new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));
|`spring.ai.vectorstore.opensearch.uris`| URIs of the OpenSearch cluster endpoints | -
116
+
|`spring.ai.vectorstore.opensearch.username`| Username for accessing the OpenSearch cluster | -
117
+
|`spring.ai.vectorstore.opensearch.password`| Password for the specified username | -
118
+
|`spring.ai.vectorstore.opensearch.index-name`| Name of the index to store vectors | `spring-ai-document-index`
119
+
|`spring.ai.vectorstore.opensearch.initialize-schema`| Whether to initialize the required schema | `false`
120
+
|`spring.ai.vectorstore.opensearch.similarity-function`| The similarity function to use | `cosinesimil`
121
+
|`spring.ai.vectorstore.opensearch.batching-strategy`| Strategy for batching documents when calculating embeddings. Options are `TOKEN_COUNT` or `FIXED_SIZE` | `TOKEN_COUNT`
122
+
|`spring.ai.vectorstore.opensearch.aws.host`| Hostname of the OpenSearch instance | -
123
+
|`spring.ai.vectorstore.opensearch.aws.service-name`| AWS service name | -
Then use the `spring.ai.vectorstore.opensearch.*` properties to configure the connection to the self-managed OpenSearch instance.
131
+
* `cosinesimil` - Default, suitable for most use cases. Measures cosine similarity between vectors.
132
+
* `l1` - Manhattan distance between vectors.
133
+
* `l2` - Euclidean distance between vectors.
134
+
* `linf` - Chebyshev distance between vectors.
102
135
103
-
=== Amazon OpenSearch Service
136
+
== Manual Configuration
104
137
105
-
To enable Amazon OpenSearch Service., add the following dependency to your project's Maven `pom.xml` or Gradle `build.gradle` build files:
138
+
Instead of using the Spring Boot auto-configuration, you can manually configure the OpenSearch vector store. For this you need to add the `spring-ai-opensearch-store` to your project:
NOTE: Those (portable) filter expressions get automatically converted into the proprietary OpenSearch link:https://opensearch.org/docs/latest/query-dsl/full-text/query-string/[Query string query].
Copy file name to clipboardExpand all lines: spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/vectorstore/opensearch/OpenSearchVectorStoreAutoConfiguration.java
0 commit comments