feat(search): enhance phrase matching and reranking logic with improved async handling

This commit is contained in:
2026-07-24 11:38:51 -04:00
parent e510c94b95
commit 94f18722e4
3 changed files with 47 additions and 37 deletions
+12 -4
View File
@@ -92,7 +92,10 @@ async def test_search_ebooks_skips_phrase_matching_when_disabled(mocker: MockerF
)
mocker.patch("python.ebook_search.search.bm25_candidates", return_value=[])
detect_mock = mocker.patch("python.ebook_search.search.detect_protected_phrases_for_query")
boost_mock = mocker.patch("python.ebook_search.search.apply_phrase_mention_boosts")
boost_mock = mocker.patch(
"python.ebook_search.search.apply_phrase_mention_boosts",
side_effect=lambda _engine, candidates, *_args, **_kwargs: candidates,
)
config = EbookSearchConfig(rerank=RerankConfig(enabled=False))
response = await search_ebooks(
@@ -105,7 +108,8 @@ async def test_search_ebooks_skips_phrase_matching_when_disabled(mocker: MockerF
assert "Protected phrase detection skipped" in timing_names
assert "Phrase mention boost skipped" in timing_names
detect_mock.assert_not_called()
boost_mock.assert_not_called()
boost_mock.assert_awaited_once()
assert boost_mock.await_args.kwargs == {"phrase_matching": False}
async def test_search_ebooks_ignores_phrase_matching_when_config_disabled(mocker: MockerFixture) -> None:
@@ -117,7 +121,10 @@ async def test_search_ebooks_ignores_phrase_matching_when_config_disabled(mocker
)
mocker.patch("python.ebook_search.search.bm25_candidates", return_value=[])
detect_mock = mocker.patch("python.ebook_search.search.detect_protected_phrases_for_query")
boost_mock = mocker.patch("python.ebook_search.search.apply_phrase_mention_boosts")
boost_mock = mocker.patch(
"python.ebook_search.search.apply_phrase_mention_boosts",
side_effect=lambda _engine, candidates, *_args, **_kwargs: candidates,
)
config = EbookSearchConfig(rerank=RerankConfig(enabled=False), phrase_matching_enabled=False)
response = await search_ebooks(
@@ -130,4 +137,5 @@ async def test_search_ebooks_ignores_phrase_matching_when_config_disabled(mocker
assert "Protected phrase detection skipped" in timing_names
assert "Phrase mention boost skipped" in timing_names
detect_mock.assert_not_called()
boost_mock.assert_not_called()
boost_mock.assert_awaited_once()
assert boost_mock.await_args.kwargs == {"phrase_matching": False}
+4 -4
View File
@@ -10,7 +10,7 @@ import pytest
from python.ebook_search.config import EbookSearchConfig, RerankConfig, load_rerank_config
from python.ebook_search.rerank import rerank_chunks
from python.ebook_search.search import SearchResult, apply_rerank, skip_rerank
from python.ebook_search.search import SearchResult, apply_rerank
if TYPE_CHECKING:
from pytest_mock import MockerFixture
@@ -57,10 +57,10 @@ def test_config_defaults_enable_reranking(mocker: MockerFixture) -> None:
assert config.timeout_seconds == 30
def test_reranking_disabled_returns_original_fused_order() -> None:
async def test_reranking_disabled_returns_original_fused_order(mocker: MockerFixture) -> None:
config = EbookSearchConfig(rerank=RerankConfig(enabled=False), top_k=2)
response = skip_rerank("query", candidates(), config)
response = await apply_rerank(mocker.Mock(), "query", candidates(), config, rerank=False)
assert response.rank_label == "Hybrid"
assert [result.chunk_id for result in response.results] == [1, 2]
@@ -133,7 +133,7 @@ async def test_vllm_rerank_timeout_raises(mocker: MockerFixture) -> None:
config = EbookSearchConfig(rerank=RerankConfig(enabled=True), top_k=2)
with pytest.raises(httpx.TimeoutException, match="timeout"):
await apply_rerank(mocker.Mock(), "query", candidates(), config)
await apply_rerank(mocker.Mock(), "query", candidates(), config, rerank=True)
async def test_malformed_vllm_rerank_json_does_not_crash_search(mocker: MockerFixture) -> None: