Skip to content

fix(dataset): keep unscored full-text documents in hybrid search threshold filter - #47411

Open
MindOfqc wants to merge 2 commits into
langgenius:mainfrom
MindOfqc:fix/hybrid-search-zero-results
Open

MindOfqc wants to merge 2 commits into
langgenius:mainfrom
MindOfqc:fix/hybrid-search-zero-results

Conversation

@MindOfqc

Copy link
Copy Markdown
Contributor

Summary

  • Hybrid search with score_threshold enabled returned 0 results in the Retrieval Testing page while Vector search and Full-Text search on the same dataset returned hits (reported in Hybrid Search returns 0 results while Vector and Full-Text Search work correctly (Dify 1.19, Weaviate, Ollama) #47357).
  • Root cause: since Score threshold not applied to reranked score in hybrid search #35233 hybrid search skips the early embedding-score threshold (embedding_score_threshold = 0.0) and defers thresholding to the merged post-retrieval stage. When no rerank runner is configured, RetrievalService._filter_documents_by_vector_score_threshold filters the merged candidates by their stored metadata["score"]. Full-text search results carry no stored score, so metadata.get("score", 0) evaluated them as 0 and dropped every full-text candidate whenever a positive threshold was set. Vector results kept their real cosine score, which is why each single method worked but hybrid returned nothing.
  • Fix: documents without a stored score are now kept by the threshold filter (the threshold cannot be evaluated for them); scored vector candidates are still filtered as before, and the filter remains skipped entirely when a rerank runner is present.
  • Tests: test_filter_documents_by_vector_score_threshold_keeps_unscored_documents covers the filter helper directly; test_retrieve_internal_hybrid_keeps_unscored_fulltext_documents_without_rerank_runner reproduces the 0-result flow end to end; test_retrieve_internal_hybrid_skips_threshold_filter_when_rerank_runner_present pins the unchanged reranker behavior.

Screenshots

N/A (backend fix, verified by unit tests: 42 passed in tests/unit_tests/core/rag/datasource/test_retrieval_service.py).

Checklist

The PR meets the requirements listed in the contribution guide.

  • I have performed a self-review of the code
  • I have commented the code in hard-to-understand areas
  • I have made corresponding changes to the documentation and updated the docstring
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

…shold filter

Hybrid search defers score thresholding to the post-retrieval stage (langgenius#35233),
so the vector store returns all candidates with embedding_score_threshold=0.0.
When no rerank runner is configured, _filter_documents_by_vector_score_threshold
filtered the merged candidates by their stored metadata score. Full-text search
results carry no metadata score, so the get("score", 0) default treated every
one of them as below any positive threshold and silently dropped them. With
score_threshold enabled, hybrid search returned 0 results while vector and
full-text search each worked correctly.

Keep documents that have no stored score: the threshold cannot be evaluated for
them, so they pass through instead of being dropped. Scored documents (vector
candidates) are still filtered as before, and when a rerank runner exists the
filter is skipped entirely, so reranking behavior is unchanged.

Regression tests cover the filter helper directly and the hybrid _retrieve flow
both without and with a rerank runner.

Fixes langgenius#47357
@MindOfqc
MindOfqc requested a review from JohnJyong as a code owner November 28, 2026 09:31
@github-actions github-actions Bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Nov 28, 2026
@github-actions

github-actions Bot commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 88.08% 88.08% +0.00%
Strict coverage 87.71% 87.71% +0.00%
Typed symbols 98,742 98,749 +7
Untyped symbols 13,424 13,424 0
Modules 4713 4713 0

@codecov

codecov Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.62%. Comparing base (4cbb38e) to head (863ed5e).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #47411      +/-   ##
==========================================
- Coverage   90.62%   90.62%   -0.01%     
==========================================
  Files        6665     6665              
  Lines      415339   415343       +4     
  Branches    83296    83297       +1     
==========================================
+ Hits       376412   376413       +1     
- Misses      32541    32544       +3     
  Partials     6386     6386              
Flag Coverage Δ
api 90.35% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link
Copy Markdown
Contributor Author

Updated in 863ed5ee after the test-doubles guard failure. The two hybrid retrieval tests now use Flask, the SQLite fixture and the real DataPostProcessor; they stub only the retrieval inputs. The no-runner test returns no results with the base implementation and keeps only the unscored full-text hit with this PR. The separate weighted test uses the real reranker and checks that the vector fallback is not called. All 42 retrieval service tests, Ruff, and the guard against the target branch passed locally. This verifies the no-runner regression, not the reporter’s specific weighted embedding setup.

@github-actions

Copy link
Copy Markdown
Contributor

Pyrefly Diff

base → PR
--- /tmp/pyrefly_base.txt	2026-11-28 10:00:29.277910340 +0000
+++ /tmp/pyrefly_pr.txt	2026-11-28 10:00:18.245820481 +0000
@@ -3819,7 +3819,7 @@
 ERROR Argument `dict[str, str]` is not assignable to parameter `reranking_model` with type `RerankingModelDict | None` in function `core.rag.data_post_processor.data_post_processor.DataPostProcessor._get_rerank_model_instance` [bad-argument-type]
    --> tests/unit_tests/core/rag/data_post_processor/test_data_post_processor.py:192:33
 ERROR Argument `str` is not assignable to parameter `doc_form` with type `IndexStructureType | SQLCoreOperations[IndexStructureType]` in function `models.dataset.Document.__init__` [bad-argument-type]
-   --> tests/unit_tests/core/rag/datasource/test_retrieval_service.py:148:18
+   --> tests/unit_tests/core/rag/datasource/test_retrieval_service.py:149:18
 ERROR Argument `list[FromClause]` is not assignable to parameter `tables` with type `Sequence[Table] | None` in function `sqlalchemy.sql.schema.MetaData.create_all` [bad-argument-type]
   --> tests/unit_tests/core/rag/extractor/test_excel_extractor.py:17:56
 ERROR Argument `object | None` is not assignable to parameter `session` with type `Session | None` in function `core.rag.extractor.extract_processor.ExtractProcessor.extract` [bad-argument-type]

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

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant