Semantic Search with Spring Boot & Redis

Semantic Search with Spring Boot & Redis

TL;DR: You’re building a semantic search app using Spring Boot and Redis.

Instead of matching exact words, semantic search finds meaning using Vector Similarity Search (VSS).

It works by turning movie synopses into vectors with embedding models, storing them in Redis (as a vector database), and finding the closest matches to user queries.


Video: What is semantic search?

A traditional searching system works by matching the words a user types with the words stored in a database or document collection. It usually looks for exact or partial matches without understanding the meaning behind the words.

Semantic searching, on the other hand, shines because instead of just matching words, it tries to understand the meaning behind what the user is asking. It focuses on the concepts, not just the keywords, making it much easier for users to find what they really want.

In a movie streaming service, for example, if a movie’s synopsis is stored in a database as “A cowboy doll feels threatened when a new space toy becomes his owner’s favorite,” but the user searches for “jealous toy struggles with new rival,” a traditional search system might not find the movie because the exact words don’t line up.

But a semantic a semantic search system can still connect the two ideas and bring up the right movie. It understands the meaning behind your query — not just the exact words.

Behind the scenes, this works thanks to vector similarity search. It turns text (or images, or audio) into vectors — lists of numbers —store them in a vector database and then finds the ones closest to your query.

Today, we’re gonna build a vector similarity search app that lets users find movies based on the meaning of their synopsis — not just exact keyword matches. So that even if they don’t know the title, they can still get the right movie based on a generic description of the synopsis.

To do that, we’ll build a Spring Boot app from scratch and plug in Redis OM Spring. It’ll handle turning our data into vectors, storing them in Redis, and running fast vector searches when users send a query.

Redis as a Vector Database

Video: What is a vector database?

In the last 15 years, Redis became the foundational infrastructure for realtime applications. Today, with Redis 8, it’s commited to becoming the foundational infrastructure for AI applications as well.

Redis 8 not only turns the community version of Redis into a Vector Database, but also makes it the fastest and most scalable database in the market today. Redis 8 allows you to scale to one billion vectors without penalizing latency.

Learn more: https://redis.io/blog/searching-1-billion-vectors-with-redis-8/

Redis OM Spring

To allow our users and customers to take full advantage of everything Redis can do — with the speed Redis is known for — we decided to implement Redis OM Spring, a library built on top of Spring Data Redis.

Redis OM Spring allows our users to easily communicate with Redis, model their entities as JSONs or Hashes, efficiently query them by levaraging the Redis Query Engine and even take advantage of probabilistic data structures such as Count-min Sketch, Bloom Filters, Cuckoo Filters, and more.

Redis OM Spring on GitHub: https://github.com/redis/redis-om-spring

Dataset

The dataset we’ll be looking is a catalog of thousands of movies. Each of these movies has metadata such as its title, cast, genre, year, and synopsis. The JSON file representing this dataset can be found in the repository that accompanies this article.

Sample:

Building the Application

Our application will be built using Spring Boot with Redis OM Spring. It will allow movies to be searched by their synopsis based on semantic search rather than keyword matching. Besides that, our application will also allow its users to perform hybrid search, a technique that combines vector similarity with traditional filtering and sorting.

0. GitHub Repository

The full application can be found on GitHub: https://github.com/redis/redis-om-spring/tree/main/demos/roms-vss-movies/

1. Add the required dependencies

From a Spring Boot application, add the following dependencies to your Maven or Gradle file:

2. Define the Movie entity

Redis OM Spring provides two annotations that makes it easy to vectorize data and perform vector similarity search from within Spring Boot.

  • : Automatically generates vector embeddings from the text field

  • : Enables vector indexing on a field for efficient search

The core of the implementation is the class with Redis vector indexing annotations:

In this example we're using OpenAI's embedding model that requires an OpenAI API Key to be set in the file of your application:

If an embedding model is not specified, Redis OM Spring will use a Hugging Face’s Transformers model all-MiniLM-L6-v2 by default. In this case, make sure you match the number of dimensions in the indexed annotation to 384 which is the number of dimensions created by the default embedding model.

3. Repository Interface

A simple repository interface that extends . This will be used to load the data into Redis using the method:

This provides basic CRUD operations for entities, with the first generic parameter being the entity type and the second being the ID type.

4. Search Service

The search service uses two beans provided by Redis OM Spring:

  • : For creating a stream of entities to perform searches. The Entity Stream must not be confused with the Java Streams API. The Entity Stream will generate a Redis Command that will be sent to Redis so that Redis can perform the searching, filtering and sorting efficiently on its side.

  • : Used for generating the embedding for the query sent by the user. It will be generated following the configuration of the annotation defined in the class.

The search functionality is implemented in the :

Key features of the search service:

  • Uses to create a search stream for entities

  • Converts the text query into a vector embedding

  • Uses K-nearest neighbors (KNN) search to find similar vectors

  • Applies additional filters for hybrid search (combining vector and traditional search)

  • Returns pairs of movies and their similarity scores

5. Movie Service for Data Loading

The handles loading movie data into Redis. It reads a JSON file containing movie date and save the movies into Redis.

It may take one or two minutes to load the data for the thousands of movies in the file because the embedding generation is done in the background. The annotation will generate the embeddings for the field before the movie is saved into Redis.

5. Search Controller

The REST controller exposes the search endpoint:

6. Application Bootstrap

The main application class initializes Redis OM Spring and loads data. The annotation activates Redis OM Spring's repository support:

7. Sample Requests

You can make requests to the search endpoint:

Sample request:

Sample response:

Wrapping up

And that’s it — you now have a working semantic search app using Spring Boot and Redis.

Instead of relying on exact keyword matches, your app understands the meaning behind the query. Redis handles the heavy part: embedding storage, similarity search, and even traditional filters — all at lightning speed.

With Redis OM Spring, you get an easy way to integrate this into your Java apps. You only need two annotations: and and two Beans: and .

Whether you’re building search, recommendations, or AI-powered assistants, this setup gives you a solid and scalable foundation.

Try it out, tweak the filters, explore other models, and see how far you can go!

More AI Resources

The best way to stay on the path of learning AI is by following the recipes available on the Redis AI Resources GitHub repository. There you can find dozens of recipes that will get you to start building AI apps, fast!

GitHub - redis-developer/redis-ai-resources: ✨ A curated list of awesome community resources… ✨ A curated list of awesome community resources, integrations, and examples of Redis in the AI ecosystem. …github.com

Stay Curious!

To view or add a comment, sign in

Others also viewed

Explore topics