mirror of
https://github.com/neuml/txtai.git
synced 2026-10-12 02:19:52 +00:00
Save empty keyword and content indexes without errors (#1224)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user