Skip to content

fix(faiss): keep searching when filters drop the first candidates - #4323

Merged
davidsbatista merged 4 commits into
deepset-ai:mainfrom
astroknot-sheep:fix/faiss-filtered-search-top-k
Nov 27, 2026
Merged

davidsbatista merged 4 commits into
deepset-ai:mainfrom
astroknot-sheep:fix/faiss-filtered-search-top-k

Conversation

@astroknot-sheep

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

With filters, FAISSDocumentStore.search() fetched min(ntotal, top_k * 10) neighbours once and filtered them afterwards, so matching documents outside that window were never returned (the retriever could return [] while filter_documents() found matches).

Now, if fewer than top_k fetched neighbours match the filters, the search is repeated with double the k, up to the whole index. Searches without filters, and filtered searches that already get top_k matches in the first pass, behave exactly as before.

How did you test it?

  • Added test_search_with_filters_finds_match_outside_first_candidates (the only matching document is the farthest one). It fails on main and passes with this change.
  • In integrations/faiss: hatch run test:unit 14 passed; pytest tests/test_document_store.py 96 passed, 2 skipped; hatch run fmt-check clean.
  • Ran the repro from the issue: search() and FAISSEmbeddingRetriever now return ['de'].

Notes for the reviewer

I used AI assistance while investigating and drafting this PR. I reviewed every line and ran the tests myself.

Checklist

@astroknot-sheep
astroknot-sheep requested a review from a team as a code owner November 24, 2026 09:24
@astroknot-sheep
astroknot-sheep requested review from davidsbatista and removed request for a team November 24, 2026 09:24
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report (faiss)

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  integrations/faiss/src/haystack_integrations/document_stores/faiss
  document_store.py 263, 267
Project Total  

This report was generated by python-coverage-comment-action

@github-actions github-actions Bot added the type:documentation Improvements or additions to documentation label Nov 27, 2026
@davidsbatista

Copy link
Copy Markdown
Contributor

Thanks @astroknot-sheep

I pushed two commits:

  • Retry once over the whole index instead of doubling k: at most 2 searches (was up to 9) and ~2.5× faster on selective filters, with the same results.

  • Replaced the outdated "doesn't support pre-filtering" comment in search().

@davidsbatista
davidsbatista merged commit 04146f6 into deepset-ai:main Nov 27, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

integration:faiss type:documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FAISS: filtered search misses matching documents outside the first top_k * 10 neighbours

2 participants