Skip to main content

📝 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 client is connected, document_chunks is loaded, and query_embedding was generated with the same embedding model used for the stored vectors. Define allowed_groups as an ARRAY of strings and embedding as 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_ANY checks 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.

    ChunkTenantLabelAllowed groupsEligible for this search?
    Acompany_afinancefinance_team✅
    Bcompany_afinanceexecutives❌
    Ccompany_bfinancefinance_team❌
    Dcompany_aengineeringfinance_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.

Reference​