Qdrant
This section walks you through setting up the Qdrant VectorStore
to store document embeddings and perform similarity searches.
Qdrant is an open-source, high-performance vector search engine/database. It uses HNSW (Hierarchical Navigable Small World) algorithm for efficient k-NN search operations and provides advanced filtering capabilities for metadata-based queries.
Prerequisites
-
Qdrant Instance: Set up a Qdrant instance by following the installation instructions in the Qdrant documentation.
-
If required, an API key for the EmbeddingModel to generate the embeddings stored by the
QdrantVectorStore
.
It is recommended that the Qdrant collection is created in advance with the appropriate dimensions and configurations.
If the collection is not created, the QdrantVectorStore will attempt to create one using the Cosine similarity and the dimension of the configured EmbeddingModel .
|
Auto-configuration
Spring AI provides Spring Boot auto-configuration for the Qdrant Vector Store.
To enable it, add the following dependency to your project’s Maven pom.xml
file:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-qdrant-store-spring-boot-starter</artifactId>
</dependency>
or to your Gradle build.gradle
build file.
dependencies {
implementation 'org.springframework.ai:spring-ai-qdrant-store-spring-boot-starter'
}
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
Please have a look at the list of configuration parameters for the vector store to learn about the default values and configuration options.
Refer to the Repositories section to add Milestone and/or Snapshot Repositories to your build file. |
The vector store implementation can initialize the requisite schema for you, but you must opt-in by specifying the initializeSchema
boolean in the builder or by setting …initialize-schema=true
in the application.properties
file.
this is a breaking change! In earlier versions of Spring AI, this schema initialization happened by default. |
Additionally, you will need a configured EmbeddingModel
bean. Refer to the EmbeddingModel section for more information.
Now you can auto-wire the QdrantVectorStore
as a vector store in your application.
@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 Qdrant
vectorStore.add(documents);
// Retrieve documents similar to a query
List<Document> results = vectorStore.similaritySearch(SearchRequest.query("Spring").topK(5));
Configuration Properties
To connect to Qdrant and use the QdrantVectorStore
, you need to provide access details for your instance.
A simple configuration can be provided via Spring Boot’s application.yml
:
spring:
ai:
vectorstore:
qdrant:
host: <qdrant host>
port: <qdrant grpc port>
api-key: <qdrant api key>
collection-name: <collection name>
use-tls: false
initialize-schema: true
batching-strategy: TOKEN_COUNT # Optional: Controls how documents are batched for embedding
Properties starting with spring.ai.vectorstore.qdrant.*
are used to configure the QdrantVectorStore
:
Property | Description | Default Value |
---|---|---|
|
The host of the Qdrant server |
|
|
The gRPC port of the Qdrant server |
|
|
The API key to use for authentication |
- |
|
The name of the collection to use |
|
|
Whether to use TLS(HTTPS) |
|
|
Whether to initialize the schema |
|
|
Strategy for batching documents when calculating embeddings. Options are |
|
Manual Configuration
Instead of using the Spring Boot auto-configuration, you can manually configure the Qdrant vector store. For this you need to add the spring-ai-qdrant-store
to your project:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-qdrant-store</artifactId>
</dependency>
or to your Gradle build.gradle
build file.
dependencies {
implementation 'org.springframework.ai:spring-ai-qdrant-store'
}
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
Create a Qdrant client bean:
@Bean
public QdrantClient qdrantClient() {
QdrantGrpcClient.Builder grpcClientBuilder =
QdrantGrpcClient.newBuilder(
"<QDRANT_HOSTNAME>",
<QDRANT_GRPC_PORT>,
<IS_TLS>);
grpcClientBuilder.withApiKey("<QDRANT_API_KEY>");
return new QdrantClient(grpcClientBuilder.build());
}
Then create the QdrantVectorStore
bean using the builder pattern:
@Bean
public VectorStore vectorStore(QdrantClient qdrantClient, EmbeddingModel embeddingModel) {
return QdrantVectorStore.builder(qdrantClient)
.embeddingModel(embeddingModel)
.collectionName("custom-collection") // Optional: defaults to "vector_store"
.initializeSchema(true) // Optional: defaults to false
.batchingStrategy(new TokenCountBatchingStrategy()) // Optional: defaults to TokenCountBatchingStrategy
.build();
}
// This can be any EmbeddingModel implementation
@Bean
public EmbeddingModel embeddingModel() {
return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("OPENAI_API_KEY")));
}
Metadata Filtering
You can leverage the generic, portable metadata filters with Qdrant store as well.
For example, you can use either the text expression language:
vectorStore.similaritySearch(
SearchRequest.defaults()
.queryString("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression("author in ['john', 'jill'] && article_type == 'blog'"));
or programmatically using the Filter.Expression
DSL:
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(SearchRequest.defaults()
.queryString("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression(b.and(
b.in("author", "john", "jill"),
b.eq("article_type", "blog")).build()));
These (portable) filter expressions get automatically converted into the proprietary Qdrant filter expressions. |