To add filters or constraints to search queries in Haystack, you use the Filter
class or query parameters specific to your document store. Filters let you narrow results by metadata fields (e.g., dates, categories) or apply logical conditions. For example, in Haystack’s pipeline-based workflow, you can attach filters directly to a Retriever
component or modify the query object before execution. This approach works across supported databases like Elasticsearch, Weaviate, or PostgreSQL.
Start by defining filters using Haystack’s Filter
syntax. For a basic equality check, create a Filter
object specifying the field and value:
from haystack import Filter
news_filter = Filter(field="category", value="news")
Pass this to the filters
parameter in your retriever’s run
method:
result = retriever.run(query="climate", filters=news_filter)
For multiple conditions, combine filters logically. Use Filter.and_(filter1, filter2)
for “AND” logic or Filter.or_(filter1, filter2)
for "OR". To find articles published after 2020 in the “tech” category:
date_filter = Filter(field="publish_date", operator=">", value="2020-01-01")
tech_filter = Filter(field="category", value="tech")
combined_filter = Filter.and_(date_filter, tech_filter)
For advanced constraints, use the query builder or database-specific syntax. If using Elasticsearch, leverage its DSL directly:
from elasticsearch_dsl import Q
es_query = Q("match", title="AI") & Q("range", views={"gt": 1000})
result = retriever.run(query=es_query)
When working with Haystack’s Pipeline
, apply dynamic filters by modifying the query
object in a custom component. For instance, restrict results to a user’s saved preferences by injecting a filter based on their profile. Always check your document store’s documentation for operator support (e.g., in
, exists
) and performance considerations. Filters are applied at the database level, ensuring efficient query execution without post-processing.
Zilliz Cloud is a managed vector database built on Milvus perfect for building GenAI applications.
Try FreeLike the article? Spread the word