liveBook · Manning

Metadata filter

As explained in Build an Advanced RAG Application (From Scratch)

Metadata filtering is the restriction of retrieval results according to structured attributes associated with stored items.

Metadata filtering restricts vector-search results using structured attributes stored with each item. In a RAG pipeline, it combines semantic similarity with constraints such as city, locality, language, or year, reducing irrelevant context and improving personalization. section 6.2 section 6.6

A practical implementation follows this sequence:

  • Store filterable fields as payload metadata alongside each embedding, such as hotel name, review text, and locality. section 6.2
  • Construct a filter only when a constraint is supplied; a city value becomes a Qdrant field condition matching the locality payload. section 6.2
  • Pass the filter into vector search so Qdrant returns the highest-scoring matching points and their payloads for prompt construction. section 6.2

City-specific hotel retrieval

For the query “Amazing hotel close to everything,” setting city_filter to “Istanbul” limits retrieval to Istanbul records. The generated answer can then cite matching hotels, localities, review text, and similarity scores rather than mixing results from other cities. section 6.2

def search_qdrant_with_filter(query, model, client, city=None, k=10): #A
   
    query_filter = None
    if city:
        query_filter = models.Filter( #B
            must=[
                models.FieldCondition(
                    key="locality",
                    match=models.MatchValue(value=city)
                )
            ]
        )
 
 
 
    text_hits = client.query_points(
    collection_name="hotel_reviews",  
    query=query_embedding,
    query_filter=query_filter,   
    limit=k,                             
    with_payload=True, 
).points                                 
 
 
    return text_hits

Inspect the incremental Python implementation that builds an optional locality filter and passes it to Qdrant vector search. section 6.2

How Build an Advanced RAG Application (From Scratch) covers it

How deep it goes
Implements payload storage, conditional filter construction, filtered vector queries, and generated context, but does not detail advanced filter expressions.
Examples
Uses hotel reviews with locality payloads and an Istanbul query to demonstrate city-restricted recommendations.
What it assumes
Assumes embeddings and vector retrieval, while demonstrating metadata payloads and filtering through a concrete Qdrant workflow.
What you can do after
Apply it via Qdrant to build targeted vector retrieval and pass cleaner, more personalized context into RAG generation.
How it connects
Connects vector databases, embeddings, similarity search, Qdrant, FAISS, and retrieval-augmented generation.

Also explained in

Compare how 5 books explain metadata filter →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build an Advanced RAG Application (From Scratch). The section links point to the parts of the book it draws on.