Quick revision
The short answer
We index each chunk as a hash with a TAG field for the tenant, a TEXT field for the content and a VECTOR field using HNSW with FLOAT32, 1536 dimensions and cosine distance, then run a hybrid query that filters by tenant and asks for the 5 nearest neighbours with KNN. HNSW is an approximate graph index: M and EF_CONSTRUCTION buy recall with memory and build time, EF_RUNTIME buys recall with query time. Spring AI's Redis vector store creates this index and runs these queries behind its VectorStore API, but the tenant filter must always be part of the query.
What to remember
In this answer, we stored each chunk as a hash with a tenant TAG, a content TEXT field and a FLOAT32 vector, created an HNSW index with DIM 1536 and cosine distance, and ran a hybrid query that filters by tenant before taking the 5 nearest neighbours. We saw that M and EF_CONSTRUCTION trade memory and build time for recall, EF_RUNTIME trades query time for recall, FLAT is exact but linear, and Spring AI wraps the index and query behind VectorStore while we keep the tenant filter in the query itself.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We index each chunk as a hash with a TAG field for the tenant, a TEXT field for the content and a VECTOR field using HNSW with FLOAT32, 1536 dimensions and cosine distance, then run a hybrid query that filters by tenant and asks for the 5 nearest neighbours with KNN. HNSW is an approximate graph index: M and EF_CONSTRUCTION buy recall with memory and build time, EF_RUNTIME buys recall with query time. Spring AI's Redis vector store creates this index and runs these queries behind its VectorStore API, but the tenant filter must always be part of the query.
Why this question is asked
Retrieval-augmented generation lives or dies by its retrieval step: if the search returns the wrong chunks, the model answers confidently from the wrong documents, and in a multi-tenant product the wrong chunks may belong to another customer. Interviewers ask this question to see whether we understand vector indexes beyond calling an SDK: how the vectors are stored, what the HNSW knobs trade, and where the tenant boundary is enforced. In this answer, we will recap embeddings and nearest-neighbour search, write the index and store a chunk from Java, run the filtered KNN query, explain the HNSW parameters, and show how Spring AI wraps all of it.
1A Quick Recap of Embeddings and Nearest Neighbours
An embedding is a list of numbers that an embedding model produces for a piece of text, here 1536 floats per chunk. Texts with similar meaning get vectors that point in similar directions, so semantic search means finding the stored vectors closest to the vector of the question. Cosine distance measures how far apart two directions are, which is the usual choice for text embeddings.
Comparing a query with all 2 million vectors would cost 2 million x 1536 multiplications per query, about 3 billion. A FLAT index does exactly that and is perfectly accurate. HNSW (hierarchical navigable small world) builds a graph in which each vector is linked to some of its neighbours, and a query walks the graph towards closer and closer vectors, visiting a few thousand instead of 2 million. It is approximate: it may occasionally miss a true neighbour.
2The Index Definition and Storing a Chunk
Each chunk is a hash with the key prefix chunk:. The vector goes into the hash as raw bytes, 1536 float32 values in little-endian order, which is 6,144 bytes per chunk.
The 6 after HNSW counts the attribute words that follow (three name-value pairs). If the byte length does not match DIM x 4, the document is not indexed and FT.INFO counts an indexing failure, which is the most common mistake when writing vectors by hand.
3The Filtered KNN Query
The query has two parts joined by =>. Before the arrow is an ordinary filter; after it is the vector clause, which asks for the 5 nearest neighbours of the parameter $vec among the documents that passed the filter.
7 messages in order. Play them, or step through them one at a time.
PARAMS passes the binary vector safely instead of pasting bytes into the query string, and DIALECT 2 is the query syntax that supports these parameters. The score is a distance, so smaller means closer and SORTBY score puts the best match first. Because the tenant filter is inside the query, the engine never considers another tenant's chunks.
That last point is a security rule, not a tuning detail. If we searched all tenants and asked the model in the prompt to ignore other tenants' text, a single unlucky retrieval would place another customer's document in front of the model. The filter must be part of the query itself, built by the server from the authenticated tenant, never from user input.
4What the HNSW Parameters Trade
M is the number of links each node keeps to its neighbours (16 by default). More links give the search more paths to the true neighbours, so recall rises, but every link costs memory on every one of the 2 million nodes. EF_CONSTRUCTION (200 by default) is how many candidates the builder considers when choosing those links; higher values make a better graph but slower inserts. EF_RUNTIME (10 by default) is how many candidates a query keeps while walking the graph; raising it improves recall at query time and costs latency, and it can be set per query.
FLAT, the alternative, compares against every vector. It is exact and simple, but its cost grows linearly with the data. At 2 million vectors per index, HNSW is the usual choice; FLAT is right for small sets or when a filter leaves only a few thousand candidates.
| Parameter | Raises | Costs |
|---|---|---|
| M | recall | memory per node, build time |
EF_CONSTRUCTION | graph quality, recall | slower inserts and builds |
EF_RUNTIME | recall per query | query latency |
| FLAT instead of HNSW | exact results | time grows with every vector |
5How Spring AI Fits In
Spring AI's Redis vector store does this work behind the VectorStore interface. Configured with an embedding model and a Redis connection, it can create the index at startup (spring.ai.vectorstore.redis.initialize-schema=true, with the index name and prefix as properties), embeds documents when we call add, and turns similaritySearch into the KNN query above. Metadata fields that we want to filter on must be declared, for example the tenant as a tag field; check the builder names for your Spring AI version.
The benefit is portability: the same code runs against PostgreSQL with pgvector or another store. The cost is that index tuning such as M and EF_RUNTIME may be less exposed, so we check what the store lets us configure, and we test that the filter really appears in the query Redis receives, for example with MONITOR in a test environment.
6Follow-up questions
When is a FLAT index the better choice?
When the data is small, up to perhaps a few hundred thousand vectors, so brute force is still fast; when we need exact results, for example to measure the recall of an HNSW index; and when filters are so selective that only a small set of vectors remains, such as a tenant with 5,000 chunks. FLAT also builds instantly and uses no graph memory.
Why store vectors as FLOAT32 bytes rather than JSON numbers?
In a hash, the vector field must be a binary blob of the declared type and size. It is compact (6,144 bytes here) and needs no parsing. In JSON documents, vectors are stored as arrays of numbers instead, which are easier to read but larger in memory.
How do I check that the index is healthy after loading 2 million chunks?
FT.INFO idx:chunks shows num_docs, hash_indexing_failures and percent_indexed. num_docs should match the number of chunk keys, failures should be zero, and percent_indexed should be 1 before we send traffic.
7Conclusion
In this answer, we stored each chunk as a hash with a tenant TAG, a content TEXT field and a FLOAT32 vector, created an HNSW index with DIM 1536 and cosine distance, and ran a hybrid query that filters by tenant before taking the 5 nearest neighbours. We saw that M and EF_CONSTRUCTION trade memory and build time for recall, EF_RUNTIME trades query time for recall, FLAT is exact but linear, and Spring AI wraps the index and query behind VectorStore while we keep the tenant filter in the query itself.
Open with
I would store each chunk as a hash under a chunk: prefix, with a tenant field, the content, and the embedding as a binary array of 1536 float32 values, which is 6 kilobytes.
Then, if they want more
The index is FT.CREATE idx:chunks ON HASH PREFIX 1 chunk: with tenant as a TAG, content as TEXT, and embedding as a VECTOR using HNSW with TYPE FLOAT32, DIM 1536 and DISTANCE_METRIC COSINE.
The query is a hybrid one: a tenant filter in parentheses, then the arrow and KNN 5 at embedding with the query vector passed as a parameter, sorted by the score alias, with DIALECT 2. That returns the five closest chunks only from tenant t42. HNSW is an approximate graph index.
M is the number of links per node and EF_CONSTRUCTION the effort when building, and both raise recall at the cost of memory and build time; EF_RUNTIME is the search effort per query and trades latency for recall. FLAT is exact but compares against every vector.
With Spring AI, the Redis vector store creates the index and maps add and similaritySearch with a filter expression onto these commands, so the code stays portable. The one thing I never leave to the prompt is the tenant filter: it is built from the authenticated user and sent in the query, so one tenant can never retrieve another's documents.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Redis documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Show the index definition and a filtered KNN query, explain the HNSW parameters, and say...": https://scrivio-interview-library.netlify.app/databases/redis-patterns/1982312017/