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

This commit is contained in:
Morgan Carr
2026-09-04 08:21:11 -04:00
committed by GitHub
parent eafe574d92
commit 20e2dc41e1
5 changed files with 45 additions and 1 deletions
+3
View File
@@ -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
+1 -1
View File
@@ -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
+3
View File
@@ -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)
+21
View File
@@ -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
+17
View File
@@ -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