📝 Milvus
Description
< What is it? >
- Meaning: Milvus is a vector database that stores and searches embeddings alongside metadata. A RAG application can use it to retrieve relevant document chunks for a language model.
- Role in a RAG pipeline: in the LlamaIndex integration example, LlamaIndex generates embeddings and Milvus stores and searches them. Retrieved text is then supplied to the LLM to generate an answer.
Implementation
< Filter Milvus search by metadata and permissions >
-
Standard filtered search: Milvus can filter metadata before vector similarity search. It restricts the search to records matching the filter, then performs approximate nearest-neighbor (ANN) search within that scope. See Milvus filtered search.
-
Example record: store metadata alongside each chunk's embedding. The vector below is illustrative; real embeddings must match the collection's vector dimension.
{"chunk_id": 101,"text": "Employees can claim travel expenses...","tenant_id": "company_a","label": "finance","allowed_groups": ["finance_team", "managers"],"embedding": [0.12, -0.34, 0.56]} -
Python search: this snippet uses the native PyMilvus client, rather than LlamaIndex's vector-store adapter. It assumes
clientis connected,document_chunksis loaded, andquery_embeddingwas generated with the same embedding model used for the stored vectors. Defineallowed_groupsas an ARRAY of strings andembeddingas the vector field. Adapt field names to the actual collection schema when using a LlamaIndex-created collection.results = client.search(collection_name="document_chunks",anns_field="embedding",data=[query_embedding],filter=('tenant_id == "company_a" ''and label == "finance" ''and ARRAY_CONTAINS_ANY(allowed_groups, ["finance_team"])'),limit=5,output_fields=["text", "label"],)ARRAY_CONTAINS_ANYchecks whether a document permits at least one of the user's groups. See Milvus ARRAY operators. -
Illustrative filtering result: for the filter above, only chunk A is eligible. Similarity scores are not shown because this snippet has not been executed against a collection.
Chunk Tenant Label Allowed groups Eligible for this search? A company_a finance finance_team ✅ B company_a finance executives ❌ C company_b finance finance_team ❌ D company_a engineering finance_team ❌ -
Permission enforcement: derive the tenant and groups from the authenticated user in the backend and include the required filter on every retrieval. The hardcoded values above represent one example user. Milvus evaluates the filter supplied by the application; it does not automatically infer the application's document permissions. Keep permission metadata synchronized on all chunks, including when access is revoked, and do not rely on frontend filtering after retrieval.
-
Filtering modes: standard filtering applies metadata constraints before ANN search. Milvus also offers iterative filtering, which interleaves vector retrieval and filtering. Use standard filtering for the metadata-first behavior described here.
Related ideas
- LlamaIndex covers document ingestion and indexing into Milvus.
- Search & Retrieval explains semantic retrieval and reranking.
- Retrieval Augmented Generation (RAG) combines retrieved evidence with answer generation.
Reference
- Filtered search (Milvus)
- ARRAY operators (Milvus)
- LlamaIndex integration (Milvus)