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(scoring): keep count() accurate when an id is deleted more than once (#1202)

Sainikhil Julurie90c67ef067e
diff --git a/src/python/txtai/scoring/terms.py b/src/python/txtai/scoring/terms.py
index 0d53aa09..ea7d1314 100644
--- a/src/python/txtai/scoring/terms.py
+++ b/src/python/txtai/scoring/terms.py
@@ -129,8 +129,14 @@ class Terms:
ids: ids to delete
"""
- # Set index ids as deleted, ignore ids that were never indexed
- self.deletes.extend([self.ids.index(i) for i in ids if i in self.ids])
+ # Set index ids as deleted, ignoring ids that were never indexed and ids
+ # already marked deleted. Skipping duplicates keeps count() accurate when
+ # the same id is deleted more than once (e.g. repeated upsert/delete flows).
+ for i in ids:
+ if i in self.ids:
+ indexid = self.ids.index(i)
+ if indexid not in self.deletes:
+ self.deletes.append(indexid)
def index(self):
"""
diff --git a/test/python/testscoring/testkeyword.py b/test/python/testscoring/testkeyword.py
index 2d639607..7a3ba3b6 100644
--- a/test/python/testscoring/testkeyword.py
+++ b/test/python/testscoring/testkeyword.py
@@ -166,6 +166,26 @@ class TestKeyword(unittest.TestCase):
scoring.delete([0, len(self.data) + 100])
self.assertNotIn(0, scoring.documents)
+ def testDeleteIdempotent(self):
+ """
+ Test that deleting the same id more than once does not corrupt count()
+ """
+
+ # Terms index (bm25): Terms.delete extended self.deletes without dedup, so a
+ # repeated delete appended the same index again and count() undercounted.
+ scoring = ScoringFactory.create({"method": "bm25", "terms": True, "content": True})
+ scoring.index(self.data)
+
+ total = scoring.count()
+ self.assertEqual(total, len(self.data))
+
+ scoring.delete([0])
+ self.assertEqual(scoring.count(), total - 1)
+
+ # Deleting the same id again is a no-op for the count
+ scoring.delete([0])
+ self.assertEqual(scoring.count(), total - 1)
+
def testTFIDF(self):
"""
Test tfidf