optimize plain search

This commit is contained in:
2026-07-13 18:49:24 +02:00
parent 61580d0f15
commit 34d3b38730
3 changed files with 52 additions and 56 deletions
+25 -7
View File
@@ -1,9 +1,12 @@
from pathlib import Path
from typing import Any
import pytest
import corpus_mcp.database as database_module
from corpus_mcp.database import (
CorpusError,
SEARCH_CANDIDATE_MULTIPLIER,
build_database,
read_document_excerpt,
search_database,
@@ -50,8 +53,8 @@ def test_search_returns_separate_excerpts_from_a_large_document(tmp_path: Path)
assert excerpts[1]["chunk_end_char"] <= len(content)
def test_large_document_does_not_hide_other_matching_documents(
tmp_path: Path,
def test_common_term_search_uses_a_bounded_candidate_pool(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
source = tmp_path / "source"
source.mkdir()
@@ -59,13 +62,28 @@ def test_large_document_does_not_hide_other_matching_documents(
(source / "small.txt").write_text("common term", encoding="utf-8")
database = tmp_path / "corpus.sqlite"
build_database(source, database)
connection = database_module._connect_read_only(database)
executed: list[tuple[str, tuple[Any, ...]]] = []
result = search_database(database, "common", limit=2)
class RecordingConnection:
def execute(self, statement: str, parameters: tuple[Any, ...] = ()) -> Any:
executed.append((statement, parameters))
return connection.execute(statement, parameters)
assert {match["path"] for match in result["matches"]} == {
"large.txt",
"small.txt",
}
def close(self) -> None:
connection.close()
monkeypatch.setattr(
database_module,
"_connect_read_only",
lambda _database: RecordingConnection(),
)
search_database(database, "common", limit=2, excerpts_per_document=3)
assert len(executed) == 1
assert "GROUP BY" not in executed[0][0]
assert executed[0][1][-1] == 2 * 3 * SEARCH_CANDIDATE_MULTIPLIER
def test_read_document_excerpt_is_bounded_and_navigable(