Skip to content

FilterPolicy.MERGE silently drops init filters in InMemoryBM25Retriever and InMemoryEmbeddingRetriever #12065

Closed

Description

@Aarkin7

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:

  • InMemoryBM25Retriever
  • InMemoryEmbeddingRetriever

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

  1. Create an InMemoryDocumentStore with these documents:
  • python article with meta={"type": "article", "year": 2020}
  • python blog with meta={"type": "blog", "year": 2021}
  1. Initialize an InMemoryBM25Retriever with:
  • filters={"field": "meta.type", "operator": "==", "value": "article"}
  • filter_policy=FilterPolicy.MERGE
  1. Run:
  • query="python"
  • filters={"field": "meta.year", "operator": ">=", "value": 2020}
  1. Observe that both documents are returned.
  2. 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

System:

  • OS: macOS
  • Haystack version: main (source checkout)

Activity

  1. added theissue type on Jul 19, 2026
  2. Aarkin7 commented on Jul 19, 2026

    @Aarkin7
    ContributorAuthor

    I'll raise a PR shortly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions