liveBook · Manning

Metadata filter

As explained in Building Reliable AI Systems

Selecting or excluding records according to their associated descriptive attributes.

A metadata filter selects retrieved records by structured attributes as well as content. Semantic search may find a policy that sounds relevant while missing that it is archived, applies to another region, or addresses a different audience. Filtering on attributes such as status, department, effective date, and region narrows the evidence available to a retrieval-augmented generation (RAG) system. section 4.4.1 section 3.6.5

A practical pipeline has four parts:

  • Extract attributes from documents. Combine sources such as file paths, date patterns, and LLM classification where implicit meaning requires it; handle extraction and parsing errors. section 4.4.1
  • Store attributes with searchable records or vector chunks. A policy chunk can carry its department, effective date, and status alongside its embedding. section 4.5.4
  • Analyze the question for applicable constraints. Explicit values such as a plan name can become filters; rules can also map terms such as “latest” to an active-status constraint. section 3.6.6 section 4.4.1
  • Apply filters during retrieval when supported, or filter candidate results afterward; then pass a concise set of relevant passages to the generator. section 4.5.4 section 4.4.1 section 3.6.5

Two support queries

For “How do I set up international roaming on my iPhone 14 Pro? I’m on the Mobile Plus plan,” a telecom chatbot can use topic, plan, and device metadata rather than relying on matching words alone. In a policy assistant, “active policies about physical therapy after surgery from the benefits department” combines semantic relevance with status and department constraints. A filter is useful only if its document tags and query interpretation are trustworthy: excluding the right record leaves the generator without the evidence it needs. section 3.6.6 section 4.5.4 section 4.4.1

def search_policies(query, filters=None, top_k=3):
    """
    Search policy documents with semantic search and optional filters

    Args:
        query: User question or query string
        filters: Dictionary of metadata filters to apply
        top_k: Number of results to return

    Returns:
        List of relevant policy chunks with metadata
    """
    query_embedding = model.encode([query])[0].tolist()    #1

    filter_dict = None    #2
    if filters:    #2
        filter_dict = {}    #2
        for key, value in filters.items():    #2
            filter_dict[key] = value    #2

    query_results = index.query(    #3
        vector=query_embedding,    #3
        filter=filter_dict,    #3
        top_k=top_k,    #3
        include_metadata=True    #3
    )

    results = []    #4
    for match in query_results['matches']:    #4
        results.append({ #4
            "score": match['score'],
            "content": match['metadata']['content'],
            "section_title": match['metadata']['section_title'],
            "policy_id": match['metadata']['policy_id'],
            "department": match['metadata']['department'],
            "effective_date": match['metadata']['effective_date']
        })

    return results

query = "What's covered for physical therapy after knee surgery?"   #5
filters = {"status": "Active", "department": "Benefits"}    #5
results = search_policies(query, filters)    #5

for i, result in enumerate(results):    #6
    print(f"\nResult {i+1} (Score: {result['score']:.4f})")
    print(f"Policy: {result['policy_id']} ({result['department']})")
    print(f"Section: {result['section_title']}")
    print(f"Content: {result['content'][:200]}...")

The Pinecone search function passes optional metadata constraints through the query’s filter parameter and returns matching policy chunks with their metadata. section 4.5.4

Filtering improves relevance but does not by itself prove an answer correct. Check the quality and freshness of tags, inspect which records the filters remove, and evaluate retrieval precision and answer factual accuracy. LLM-based tagging may help with semantic attributes, but running a full LLM over a large corpus has a cost; smaller models are suggested as a production alternative. section 4.4.1 section 3.9 section 3.6.5

How Building Reliable AI Systems covers it

How deep it goes
Works through document extraction, query analysis, and filter application, including Python implementations and extraction-cost caveats.
Examples
Uses telecom roaming support and health-care policy searches, with Haystack and Pinecone pipeline examples.
What it assumes
Assumes familiarity with Python retrieval pipelines while explaining BM25 before the telecom implementation.
What you can do after
Apply it via Haystack or Pinecone to constrain retrieval by document attributes and supply more targeted context for answers.
How it connects
Connects semantic search, BM25, vector databases, query analysis, and retrieval-augmented generation.

Also explained in

Compare how 2 books explain metadata filter →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Reliable AI Systems. The section links point to the parts of the book it draws on.