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."""
|
"""Rebuild from SQLite if dirty."""
|
||||||
if not self._dirty:
|
if not self._dirty:
|
||||||
return
|
return
|
||||||
conn = sqlite3.connect(db_path)
|
try:
|
||||||
conn.row_factory = sqlite3.Row
|
conn = sqlite3.connect(db_path)
|
||||||
rows = conn.execute(
|
conn.row_factory = sqlite3.Row
|
||||||
"SELECT id, doc_id, page_number, text FROM page_chunks ORDER BY doc_id, page_number"
|
try:
|
||||||
).fetchall()
|
rows = conn.execute(
|
||||||
conn.close()
|
"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._load_chunks([dict(r) for r in rows])
|
||||||
self._dirty = False
|
self._dirty = False
|
||||||
logger.info("BM25 index rebuilt: %d chunks", len(self._chunks))
|
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():
|
def test_query_respects_top_k():
|
||||||
|
# "action" matches all three chunks; top_k=2 must hard-cap the result list
|
||||||
idx = _seeded_index()
|
idx = _seeded_index()
|
||||||
results = idx.query("rules", top_k=2)
|
results = idx.query("action", top_k=2)
|
||||||
assert len(results) <= 2
|
assert len(results) == 2
|
||||||
|
|
||||||
|
|
||||||
def test_query_filters_by_doc_id():
|
def test_query_filters_by_doc_id():
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue