Repository navigation
Description
Describe the bug
When filter_policy=FilterPolicy.MERGE is used with an init-time filter and a runtime filter, the in-memory retrievers do not actually combine them for the common comparison-filter shape ({"field": ..., "operator": ..., "value": ...}).
Instead, the runtime filter overwrites the init filter, so MERGE behaves like REPLACE in practice. This leads to silently broader retrieval results than expected.
This affects:
InMemoryBM25RetrieverInMemoryEmbeddingRetriever
I observed the same behavior in both sync and async paths.
Error message
No error is thrown. This is a silent correctness bug: the retriever returns plausible-looking but incorrect results.
Expected behavior
With FilterPolicy.MERGE, I expect the init filter and runtime filter to be combined so both are applied.
For example:
- init filter:
meta.type == "article" - runtime filter:
meta.year >= 2020
Expected result:
- only documents that satisfy both conditions
Actual result:
- documents matching only the runtime filter can be returned, because the init filter is effectively dropped.
Additional context
This seems to happen because the retrievers locally merge filters in a way that overwrites the init comparison filter instead of composing it logically.
This is especially risky when init filters are used for fixed scoping and runtime filters are used for query-specific narrowing.
A correct merge helper already exists in the codebase (apply_filter_policy()), so this looks like a wiring gap rather than missing merge logic.
To Reproduce
- Create an InMemoryDocumentStore with these documents:
- python article with meta={"type": "article", "year": 2020}
- python blog with meta={"type": "blog", "year": 2021}
- Initialize an InMemoryBM25Retriever with:
- filters={"field": "meta.type", "operator": "==", "value": "article"}
- filter_policy=FilterPolicy.MERGE
- Run:
- query="python"
- filters={"field": "meta.year", "operator": ">=", "value": 2020}
- Observe that both documents are returned.
- Expected: only python article should be returned.
Minimal example:
from haystack import Document
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack.document_stores.types import FilterPolicy
store = InMemoryDocumentStore()
store.write_documents([
Document(content="python article", meta={"type": "article", "year": 2020}),
Document(content="python blog", meta={"type": "blog", "year": 2021}),
])
retriever = InMemoryBM25Retriever(
store,
filters={"field": "meta.type", "operator": "==", "value": "article"},
filter_policy=FilterPolicy.MERGE,
)
docs = retriever.run(
query="python",
filters={"field": "meta.year", "operator": ">=", "value": 2020},
)["documents"]
print([doc.content for doc in docs])
# Actual: ['python article', 'python blog']
# Expected: ['python article']The same issue is reproducible with InMemoryEmbeddingRetriever as well.
FAQ Check
- Have you had a look at our new FAQ page?
System:
- OS: macOS
- Haystack version: main (source checkout)
Activity
Aarkin7 commentedon Jul 19, 2026
I'll raise a PR shortly