Keyword search finds text that contains the words you typed. But a user who asks for "something creamy for a quick dinner" may not use a single word that appears in the recipe they would love. To search by meaning, you need embeddings and a place to search them: a vector store.
In this topic, you'll learn what embeddings are, how to store documents and search them with Spring AI, and why constraints belong in a filter and not in the query.
Embeddings
An embedding is a list of numbers, a vector, that represents the meaning of a text. An embedding model turns text into such a vector, and its length (the dimensionality) depends on the model. Texts with similar meaning get vectors that are close to each other, and the distance between two vectors measures how similar the texts are. This is the representational type of model from the introduction to LLMs: it doesn't generate text, it encodes it.
Searching with embeddings works in two steps. First, you embed every document once and store the vectors. Then, to answer a query, you embed the query with the same model and find the stored vectors nearest to it. "Cooking cream" ends up close to "cream for sauces" even though they share few words.
Spring AI represents an embedding model as an EmbeddingModel. Some models are hosted (you call an API with a key), and some run inside your application. Spring AI's Transformers (ONNX) model is one of the latter: by default it uses all-MiniLM-L6-v2, which produces 384-dimensional vectors and needs no API key. When its model files are fetched from a URL, they are downloaded the first time the model is used and cached locally, so the first start is slower.
Two rules follow from how embeddings work:
Use the same model for documents and queries. Vectors from different models aren't comparable.
If you change the model, re-embed everything. The new vectors differ, and the dimensionality may change too.
You rarely call the EmbeddingModel yourself. The vector store uses it for you.
The vector store
A vector store stores vectors together with the original text and metadata, and it can quickly find the vectors nearest to a query. It doesn't create embeddings itself. In Spring AI, the VectorStore implementation takes the EmbeddingModel configured in your application and uses it both when you add documents and when you search.
Spring AI supports many stores behind the same VectorStore interface. Qdrant is one of them. With Spring Boot, you add the store's starter and configure it in application.properties:
spring.ai.vectorstore.qdrant.host=localhost
spring.ai.vectorstore.qdrant.port=6334
spring.ai.vectorstore.qdrant.collection-name=recipes
spring.ai.vectorstore.qdrant.initialize-schema=trueinitialize-schema is off by default. When enabled, the store creates the collection if it doesn't exist, using the dimensionality of your embedding model. Port 6334 is Qdrant's gRPC port, which the store uses.
Adding documents
Spring AI wraps what you store in a Document: an ID, text, and metadata.
val documents = recipes.map { recipe ->
Document(
recipe.id, // a stable ID, in a format the store accepts (Qdrant takes UUIDs)
"${recipe.title}. ${recipe.description}",
mapOf(
"cuisine" to recipe.cuisine,
"minutes" to recipe.minutes,
"vegetarian" to recipe.vegetarian,
),
)
}
vectorStore.add(documents)The choices you make here decide how good the search is:
The text is what gets embedded. Write it the way a user would describe the thing, and include its name. Text that is a bare list of codes embeds poorly.
The metadata is what you can filter on. Put in the fields that will be hard requirements, such as a diet, a time limit, or a category. The documentation restricts metadata values to simple types (strings, numbers, booleans), so think about how to represent anything more complex.
Leave out anything that changes. The embedding and the metadata are a snapshot from the moment of insertion. If you store a price or a stock level, it becomes stale. Store only stable facts, and read changing data from your database after the search.
Use stable IDs. With them, adding a document again normally replaces it instead of creating a duplicate, but check how your store behaves. Embedding a large collection is the slowest part of a start, so it's worth skipping when nothing has changed, for example by storing a hash of the source data.
Searching
You search with a SearchRequest:
val results: List<Document> = vectorStore.similaritySearch(
SearchRequest.builder()
.query("something creamy for a quick dinner")
.topK(5)
.similarityThreshold(0.5)
.filterExpression("vegetarian == true && minutes <= 30")
.build()
)
results.forEach { doc ->
println("${doc.score} ${doc.text} ${doc.metadata["minutes"]}")
}The request has these parts:
queryis embedded and compared with the stored vectors.topKis the maximum number of results. The default is 4.similarityThresholdis a value between 0 and 1. Results below it are dropped. The default is 0, which accepts everything.filterExpressionrestricts the search by metadata. The text syntax supports==,!=,<,<=,>,>=,&&/AND,||/OR,NOT, andin [...]/nin [...].
Each result is a Document with its text, its metadata, and a score, which is null unless the store sets it during retrieval. The documentation doesn't define the scale of the score, so look at real values for your own data before you rely on specific numbers.
A search always returns the nearest documents, even if none of them is actually close. Asking for something that isn't in your collection still returns the least bad matches. That is why the threshold matters. Try some queries for things you know are absent, and choose a threshold that drops them. You can also compare each score with the best one in the results, which keeps a clear winner on its own while still returning several similar items for a vague query.
Filter in the store, not afterwards
A similarity search ranks by meaning, not by constraints. "A vegetarian creamy pasta" is close in meaning to a creamy pasta with bacon. If you search first and filter the results afterwards, the closest five might all contain bacon. The filter then removes all five, and you return nothing, even though vegetarian recipes exist further down the ranking.
The fix is to give the constraint to the store as a filter. The store then considers only the documents that qualify. Qdrant, for example, applies the filter inside the search: it checks each candidate against the filter while walking its index, so documents that fail are skipped and never occupy a place in the results.
This gives you a simple rule:
Put the soft part of the request, the meaning, into the query.
Put the hard requirements, the ones that must hold, into the filter.
Be careful when the values come from a model. A filter written as text is just a string, and a value the model invents or a user manipulates can break it or widen it. Check each value against a known list before you put it into a filter, as with any argument from a model.
As a tool for an agent
In an agent, the search is usually wrapped in a tool, so the model decides when to retrieve. The tool takes the query and the optional constraints as arguments, validates them, builds the filter in code, trims the results, and returns a small JSON result. Remember to read anything that changes from the database, and not from the stored documents.
Conclusion
Embeddings turn text into vectors so that search can follow meaning. A VectorStore keeps documents with their vectors and metadata and finds the nearest ones, using an EmbeddingModel that must be the same for documents and queries. The text you embed should describe the item the way users do, the metadata should hold the stable facts you want to filter on, and anything that changes should stay out.
A search returns the nearest documents even when none is close, so use a threshold and test it. Put the meaning in the query and the hard requirements in a metadata filter that the store applies during the search, never in a step after it.