This version is still in development and is not considered stable yet. For the latest snapshot version, please use Spring AI 1.0.0-SNAPSHOT!spring-doc.cn

OpenSearch

This section guides you through setting up the OpenSearch VectorStore to store document embeddings and perform similarity searches.spring-doc.cn

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 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.spring-doc.cn

Prerequisites

Dependencies

Add the OpenSearch Vector Store dependency to your project:spring-doc.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-opensearch-store</artifactId>
</dependency>
dependencies {
    implementation 'org.springframework.ai:spring-ai-opensearch-store'
}
Refer to the Dependency Management section to add the Spring AI BOM to your build file.

Configuration

To connect to OpenSearch and use the OpenSearchVectorStore, you need to provide access details for your instance. A simple configuration can either be provided via Spring Boot’s application.yml,spring-doc.cn

spring:
  ai:
    vectorstore:
      opensearch:
        uris: <opensearch instance URIs>
        username: <opensearch username>
        password: <opensearch password>
        indexName: <opensearch index name>
        mappingJson: <JSON mapping for opensearch index>
        aws:
          host: <aws opensearch host>
          serviceName: <aws service name>
          accessKey: <aws access key>
          secretKey: <aws secret key>
          region: <aws region>
# API key if needed, e.g. OpenAI
    openai:
      apiKey: <api-key>
Check the list of configuration parameters to learn about the default values and configuration options.

Auto-configuration

Self-Managed OpenSearch

Spring AI provides Spring Boot auto-configuration for the OpenSearch Vector Store. To enable it, add the following dependency to your project’s Maven pom.xml or Gradle build.gradle build files:spring-doc.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-opensearch-store-spring-boot-starter</artifactId>
</dependency>
dependencies {
    implementation 'org.springframework.ai:spring-ai-opensearch-store-spring-boot-starter'
}

Then use the spring.ai.vectorstore.opensearch.* properties to configure the connection to the self-managed OpenSearch instance.spring-doc.cn

Amazon OpenSearch Service

To enable Amazon OpenSearch Service., add the following dependency to your project’s Maven pom.xml or Gradle build.gradle build files:spring-doc.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-aws-opensearch-store-spring-boot-starter</artifactId>
</dependency>
dependencies {
    implementation 'org.springframework.ai:spring-ai-aws-opensearch-store-spring-boot-starter'
}

Then use the spring.ai.vectorstore.opensearch.aws.* properties to configure the connection to the Amazon OpenSearch Service.spring-doc.cn

Refer to the Dependency Management section to add the Spring AI BOM to your build file.

Here is an example of the needed bean:spring-doc.cn

@Bean
public EmbeddingModel embeddingModel() {
    // Can be any other EmbeddingModel implementation
    return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("SPRING_AI_OPENAI_API_KEY")));
}

Now you can auto-wire the OpenSearchVectorStore as a vector store in your application.spring-doc.cn

@Autowired VectorStore vectorStore;

// ...

List <Document> documents = List.of(
    new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),
    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.", Map.of("meta2", "meta2")));

// Add the documents to OpenSearch
vectorStore.add(List.of(document));

// Retrieve documents similar to a query
List<Document> results = this.vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5));

Configuration properties

You can use the following properties in your Spring Boot configuration to customize the OpenSearch vector store.spring-doc.cn

Property Description Default value

spring.ai.vectorstore.opensearch.urisspring-doc.cn

URIs of the OpenSearch cluster endpoints.spring-doc.cn

-spring-doc.cn

spring.ai.vectorstore.opensearch.usernamespring-doc.cn

Username for accessing the OpenSearch cluster.spring-doc.cn

-spring-doc.cn

spring.ai.vectorstore.opensearch.passwordspring-doc.cn

Password for the specified username.spring-doc.cn

-spring-doc.cn

spring.ai.vectorstore.opensearch.indexNamespring-doc.cn

Name of the default index to be used within the OpenSearch cluster.spring-doc.cn

spring-ai-document-indexspring-doc.cn

spring.ai.vectorstore.opensearch.mappingJsonspring-doc.cn

JSON string defining the mapping for the index; specifies how documents and their fields are stored and indexed. Refer here for some sample configurationsspring-doc.cn

{ "properties":{ "embedding":{ "type":"knn_vector", "dimension":1536 } } }spring-doc.cn

spring.ai.vectorstore.opensearch.aws.hostspring-doc.cn

Hostname of the OpenSearch instance.spring-doc.cn

-spring-doc.cn

spring.ai.vectorstore.opensearch.aws.serviceNamespring-doc.cn

AWS service name for the OpenSearch instance.spring-doc.cn

-spring-doc.cn

spring.ai.vectorstore.opensearch.aws.accessKeyspring-doc.cn

AWS access key for the OpenSearch instance.spring-doc.cn

-spring-doc.cn

spring.ai.vectorstore.opensearch.aws.secretKeyspring-doc.cn

AWS secret key for the OpenSearch instance.spring-doc.cn

-spring-doc.cn

spring.ai.vectorstore.opensearch.aws.regionspring-doc.cn

AWS region for the OpenSearch instance.spring-doc.cn

-spring-doc.cn

Customizing OpenSearch Client Configuration

In cases where the Spring Boot auto-configured OpenSearchClient with Apache HttpClient 5 Transport bean is not what you want or need, you can still define your own bean. Please read the OpenSearch Java Client Documentationspring-doc.cn

Metadata Filtering

You can leverage the generic, portable metadata filters with OpenSearch as well.spring-doc.cn

For example, you can use either the text expression language:spring-doc.cn

vectorStore.similaritySearch(SearchRequest.defaults()
        .withQuery("The World")
        .withTopK(TOP_K)
        .withSimilarityThreshold(SIMILARITY_THRESHOLD)
        .withFilterExpression("author in ['john', 'jill'] && 'article_type' == 'blog'"));
FilterExpressionBuilder b = new FilterExpressionBuilder();

vectorStore.similaritySearch(SearchRequest.defaults()
        .withQuery("The World")
        .withTopK(TOP_K)
        .withSimilarityThreshold(SIMILARITY_THRESHOLD)
        .withFilterExpression(b.and(
                b.in("john", "jill"),
                b.eq("article_type", "blog")).build()));
Those (portable) filter expressions get automatically converted into the proprietary OpenSearch Query string query.

For example, this portable filter expression:spring-doc.cn

author in ['john', 'jill'] && 'article_type' == 'blog'

is converted into the proprietary OpenSearch filter format:spring-doc.cn

(metadata.author:john OR jill) AND metadata.article_type:blog