Redis
This section walks you through setting up RedisVectorStore
to store document embeddings and perform similarity searches.
What is Redis?
Redis is an open source (BSD licensed), in-memory data structure store used as a database, cache, message broker, and streaming engine. Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams.
What is Redis Vector Search?
Redis Search and Query extends the core features of Redis OSS and allows you to use Redis as a vector database:
-
Store vectors and the associated metadata within hashes or JSON documents
-
Retrieve vectors
-
Perform vector searches
Prerequisites
-
EmbeddingClient
instance to compute the document embeddings. Several options are available:-
Transformers Embedding
- computes the embedding in your local environment. Follow the ONNX Transformers Embedding instructions. -
OpenAI Embedding
- uses the OpenAI embedding endpoint. You need to create an account at OpenAI Signup and generate the api-key token at API Keys. -
You can also use the
Azure OpenAI Embedding
.
-
-
A Redis Stack instance
-
Redis Cloud (recommended)
-
Docker image redis/redis-stack:latest
-
Dependencies
Add these dependencies to your project:
-
Embedding Client boot starter, required for calculating embeddings.
-
Transformers Embedding (Local) and follow the ONNX Transformers Embedding instructions.
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-transformers-spring-boot-starter</artifactId>
</dependency>
or use OpenAI (Cloud)
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
You’ll need to provide your OpenAI API Key. Set it as an environment variable like so:
export SPRING_AI_OPENAI_API_KEY='Your_OpenAI_API_Key'
-
Add the Redis Vector Store and Jedis dependencies
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>5.1.0</version>
</dependency>
Refer to the Dependency Management section to add the Spring AI BOM to your build file. |
Usage
Create a RedisVectorStore instance connected to your Redis database:
@Bean
public VectorStore vectorStore(EmbeddingClient embeddingClient) {
RedisVectorStoreConfig config = RedisVectorStoreConfig.builder()
.withURI("redis://localhost:6379")
// Define the metadata fields to be used
// in the similarity search filters.
.withMetadataFields(
MetadataField.tag("country"),
MetadataField.numeric("year"))
.build();
return new RedisVectorStore(config, embeddingClient);
}
It is more convenient and preferred to create the RedisVectorStore
as a Bean. But if you decide to create it manually, then you must call theRedisVectorStore#afterPropertiesSet()
after setting the properties and before using the client.
You must list explicitly all metadata field names and types ( TAG
,TEXT
, orNUMERIC
) for any metadata field used in filter expression. ThewithMetadataFields
above registers filterable metadata fields:country
of typeTAG
,year
of typeNUMERIC
.
Then in your main code, create some documents:
List<Document> documents = List.of(
new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("country", "UK", "year", 2020)),
new Document("The World is Big and Salvation Lurks Around the Corner", Map.of()),
new Document("You walk forward facing the past and you turn back toward the future.", Map.of("country", "NL", "year", 2023)));
Now add the documents to your vector store:
vectorStore.add(documents);
And finally, retrieve documents similar to a query:
List<Document> results = vectorStore.similaritySearch(
SearchRequest
.query("Spring")
.withTopK(5));
If all goes well, you should retrieve the document containing the text "Spring AI rocks!!".
Metadata filtering
You can leverage the generic, portable metadata filters with RedisVectorStore as well.
For example, you can use either the text expression language:
vectorStore.similaritySearch(
SearchRequest
.query("The World")
.withTopK(TOP_K)
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
.withFilterExpression("country in ['UK', 'NL'] && year >= 2020"));
or programmatically using the expression DSL:
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(
SearchRequest
.query("The World")
.withTopK(TOP_K)
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
.withFilterExpression(b.and(
b.in("country", "UK", "NL"),
b.gte("year", 2020)).build()));
The portable filter expressions get automatically converted into Redis search queries. For example, the following portable filter expression:
country in ['UK', 'NL'] && year >= 2020
is converted into Redis query:
@country:{UK | NL} @year:[2020 inf]