fix(services): add SQLite error handling and strengthen top_k test
This commit is contained in:
parent
2253cd7da3
commit
47914cebeb
2 changed files with 15 additions and 8 deletions
|
|
@ -47,12 +47,18 @@ class BM25Index:
|
|||
"""Rebuild from SQLite if dirty."""
|
||||
if not self._dirty:
|
||||
return
|
||||
conn = sqlite3.connect(db_path)
|
||||
conn.row_factory = sqlite3.Row
|
||||
rows = conn.execute(
|
||||
"SELECT id, doc_id, page_number, text FROM page_chunks ORDER BY doc_id, page_number"
|
||||
).fetchall()
|
||||
conn.close()
|
||||
try:
|
||||
conn = sqlite3.connect(db_path)
|
||||
conn.row_factory = sqlite3.Row
|
||||
try:
|
||||
rows = conn.execute(
|
||||
"SELECT id, doc_id, page_number, text FROM page_chunks ORDER BY doc_id, page_number"
|
||||
).fetchall()
|
||||
finally:
|
||||
conn.close()
|
||||
except sqlite3.Error as exc:
|
||||
logger.error("BM25 index rebuild failed: %s", exc)
|
||||
return
|
||||
self._load_chunks([dict(r) for r in rows])
|
||||
self._dirty = False
|
||||
logger.info("BM25 index rebuilt: %d chunks", len(self._chunks))
|
||||
|
|
|
|||
|
|
@ -43,9 +43,10 @@ def test_query_returns_relevant_result():
|
|||
|
||||
|
||||
def test_query_respects_top_k():
|
||||
# "action" matches all three chunks; top_k=2 must hard-cap the result list
|
||||
idx = _seeded_index()
|
||||
results = idx.query("rules", top_k=2)
|
||||
assert len(results) <= 2
|
||||
results = idx.query("action", top_k=2)
|
||||
assert len(results) == 2
|
||||
|
||||
|
||||
def test_query_filters_by_doc_id():
|
||||
|
|
|
|||
Loading…
Reference in a new issue