Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Save empty keyword and content indexes without errors (#1224)

Morgan Carr20e2dc41e169
diff --git a/src/python/txtai/database/embedded.py b/src/python/txtai/database/embedded.py
index a71b022b..80d72cd5 100644
--- a/src/python/txtai/database/embedded.py
+++ b/src/python/txtai/database/embedded.py
@@ -31,6 +31,9 @@ class Embedded(RDBMS):
self.path = path
def save(self, path):
+ # Initialize connection if not open
+ self.initialize()
+
# Temporary database
if not self.path:
# Save temporary database
diff --git a/src/python/txtai/embeddings/base.py b/src/python/txtai/embeddings/base.py
index 3a43d263..9a119466 100644
--- a/src/python/txtai/embeddings/base.py
+++ b/src/python/txtai/embeddings/base.py
@@ -136,7 +136,7 @@ class Embeddings:
self.ann.index(embeddings)
# Save indexids-ids mapping for indexes with no database, except when this is a reindex
- if ids and not reindex and not self.database:
+ if ids is not None and not reindex and not self.database:
self.ids = self.createids(ids)
# Index scoring, if necessary
diff --git a/src/python/txtai/scoring/terms.py b/src/python/txtai/scoring/terms.py
index ed7e5cda..b20d5db1 100644
--- a/src/python/txtai/scoring/terms.py
+++ b/src/python/txtai/scoring/terms.py
@@ -293,6 +293,9 @@ class Terms:
path: path to write terms database
"""
+ # Initialize database, if necessary
+ self.initialize()
+
# Clear documents table
self.cursor.execute(Terms.DELETE_DOCUMENTS)
diff --git a/test/python/testembeddings.py b/test/python/testembeddings.py
index 221f142d..a1c42d2a 100644
--- a/test/python/testembeddings.py
+++ b/test/python/testembeddings.py
@@ -192,6 +192,27 @@ class TestEmbeddings(unittest.TestCase):
embeddings.upsert([])
self.assertIsNotNone(embeddings.ann)
+ def testEmptySave(self):
+ """
+ Test saving an empty index
+ """
+
+ for content in [False, True]:
+ # Keyword index, no data ever indexed
+ embeddings = Embeddings({"keyword": True, "content": content})
+ embeddings.index([])
+
+ # Generate temp file path
+ index = os.path.join(tempfile.gettempdir(), f"embeddings.emptysave.{content}")
+
+ # Test save/load
+ embeddings.save(index)
+ embeddings.load(index)
+
+ # Validate index is still empty
+ self.assertEqual(embeddings.count(), 0)
+ self.assertEqual(embeddings.search("test"), [])
+
def testEmptyString(self):
"""
Test empty string indexing
diff --git a/test/python/testscoring/testkeyword.py b/test/python/testscoring/testkeyword.py
index 6220f6cb..50ffbe46 100644
--- a/test/python/testscoring/testkeyword.py
+++ b/test/python/testscoring/testkeyword.py
@@ -147,6 +147,23 @@ class TestKeyword(unittest.TestCase):
self.assertEqual(scoring.count(), 0)
self.assertEqual(scoring.search("bear", 1), [])
+ def testTermsEmptySave(self):
+ """
+ Test saving and loading a terms index with nothing indexed
+ """
+
+ for method in ["bm25", "tfidf"]:
+ config = {"method": method, "terms": True}
+
+ # No documents ever inserted - term database never initialized
+ scoring = ScoringFactory.create(config)
+ scoring.index([])
+
+ # Save/load and validate index is still empty
+ scoring = self.save(scoring, config, f"scoring.{method}.empty")
+ self.assertEqual(scoring.count(), 0)
+ self.assertEqual(scoring.search("bear", 1), [])
+
def testDeleteUnknownId(self):
"""
Test that deleting an id that was never indexed is a no-op, not a crash