Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Fix Terms.search AttributeError on an empty terms index (#1143)

Andrew Chen3bb34eae2e6a
diff --git a/src/python/txtai/scoring/terms.py b/src/python/txtai/scoring/terms.py
index 4d06c637..b93a1676 100644
--- a/src/python/txtai/scoring/terms.py
+++ b/src/python/txtai/scoring/terms.py
@@ -181,6 +181,10 @@ class Terms:
list of (id, score)
"""
+ # Nothing indexed - term database was never initialized (self.cursor is None)
+ if not self.ids:
+ return []
+
# Initialize scores array
scores = np.zeros(len(self.ids), dtype=np.float32)
diff --git a/test/python/testscoring/testkeyword.py b/test/python/testscoring/testkeyword.py
index 261d93e5..b195e3d1 100644
--- a/test/python/testscoring/testkeyword.py
+++ b/test/python/testscoring/testkeyword.py
@@ -49,6 +49,20 @@ class TestKeyword(unittest.TestCase):
self.runTests("txtai.scoring.BM25")
+ def testTermsEmpty(self):
+ """
+ Test searching a terms index with nothing indexed
+ """
+
+ # No documents ever inserted
+ scoring = ScoringFactory.create({"method": "bm25", "terms": True})
+ self.assertEqual(scoring.search("bear", 1), [])
+
+ # Documents present but all skipped (missing text field) - term database never initialized
+ scoring = ScoringFactory.create({"method": "bm25", "terms": True})
+ scoring.index([(0, {"other": "value"}, None)])
+ self.assertEqual(scoring.count(), 0)
+ self.assertEqual(scoring.search("bear", 1), [])
def testCustomInvalid(self):
"""
Test invalid custom method