perf(ebook-search): run phrase detection in parallel with retrieval

Move protected phrase detection into the retrieval gather so it runs
concurrently with vector and BM25 candidates instead of sequentially
before them. Make the search API accept real bool form fields for
rerank/phrase_matching, gate phrase matching on both the request and
config kill switch, and reflow log f-strings for readability.
This commit is contained in:
2026-07-24 11:38:51 -04:00
parent 8f1a69529c
commit 0028237579
5 changed files with 113 additions and 37 deletions
+61 -2
View File
@@ -40,7 +40,9 @@ async def test_search_ebooks_runs_vector_and_bm25_in_parallel(mocker: MockerFixt
mocker.patch("python.ebook_search.search.bm25_candidates", side_effect=fake_bm25_candidates)
config = EbookSearchConfig(rerank=RerankConfig(enabled=False))
response = await search_ebooks(engine, mocker.Mock(), "what is parallel", config)
response = await search_ebooks(
engine, mocker.Mock(), "what is parallel", config, rerank=False, phrase_matching=False
)
timings = {step.name: step for step in response.timings}
assert [result.chunk_id for result in response.results] == [1, 2]
@@ -50,6 +52,36 @@ async def test_search_ebooks_runs_vector_and_bm25_in_parallel(mocker: MockerFixt
assert received_engines == [engine]
async def test_search_ebooks_runs_phrase_detection_in_parallel_with_retrieval(mocker: MockerFixture) -> None:
"""Phrase detection joins the retrieval gather instead of running before it."""
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
phrase_started = Event()
async def fake_vector_candidates(_engine, _client, _query, _config):
"""Return vector candidates only once phrase detection has started."""
assert await asyncio.to_thread(phrase_started.wait, 2)
return [SearchResult(chunk_id=1, text="vector", source_title="Vector", vector_score=0.9)]
async def fake_query_phrase_matches(_engine, _query, _config):
"""Record that phrase detection started and return no matches."""
phrase_started.set()
return []
mocker.patch("python.ebook_search.search.vector_candidates", side_effect=fake_vector_candidates)
mocker.patch("python.ebook_search.search.bm25_candidates", return_value=[])
mocker.patch("python.ebook_search.search.query_phrase_matches", side_effect=fake_query_phrase_matches)
config = EbookSearchConfig(rerank=RerankConfig(enabled=False))
response = await search_ebooks(
engine, mocker.Mock(), "what is parallel", config, rerank=False, phrase_matching=True
)
timings = {step.name: step for step in response.timings}
assert [result.chunk_id for result in response.results] == [1]
assert timings["Protected phrase detection"].counts_toward_total is False
assert timings["Hybrid retrieval"].counts_toward_total is True
async def test_search_ebooks_skips_phrase_matching_when_disabled(mocker: MockerFixture) -> None:
"""Phrase matching can be disabled for one search request."""
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
@@ -62,7 +94,34 @@ async def test_search_ebooks_skips_phrase_matching_when_disabled(mocker: MockerF
boost_mock = mocker.patch("python.ebook_search.search.apply_phrase_mention_boosts")
config = EbookSearchConfig(rerank=RerankConfig(enabled=False))
response = await search_ebooks(engine, mocker.Mock(), "what is parallel", config, phrase_matching=False)
response = await search_ebooks(
engine, mocker.Mock(), "what is parallel", config, rerank=False, phrase_matching=False
)
timing_names = {step.name for step in response.timings}
assert [result.chunk_id for result in response.results] == [1]
assert response.phrase_matches == ()
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()
async def test_search_ebooks_ignores_phrase_matching_when_config_disabled(mocker: MockerFixture) -> None:
"""The config kill switch overrides a request that asks for phrase matching."""
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
mocker.patch(
"python.ebook_search.search.vector_candidates",
return_value=[SearchResult(chunk_id=1, text="vector", source_title="Vector", vector_score=0.9)],
)
mocker.patch("python.ebook_search.search.bm25_candidates", return_value=[])
detect_mock = mocker.patch("python.ebook_search.search.query_phrase_matches")
boost_mock = mocker.patch("python.ebook_search.search.apply_phrase_mention_boosts")
config = EbookSearchConfig(rerank=RerankConfig(enabled=False), phrase_matching_enabled=False)
response = await search_ebooks(
engine, mocker.Mock(), "what is parallel", config, rerank=False, phrase_matching=True
)
timing_names = {step.name for step in response.timings}
assert [result.chunk_id for result in response.results] == [1]