💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Fix Scoring.delete() crashing on ids that were never indexed
Amir Fathibd199158b6b0
diff --git a/src/python/txtai/scoring/terms.py b/src/python/txtai/scoring/terms.pyindex b93a1676..0d53aa09 100644--- a/src/python/txtai/scoring/terms.py+++ b/src/python/txtai/scoring/terms.py@@ -129,8 +129,8 @@ class Terms:ids: ids to delete"""- # Set index ids as deleted- self.deletes.extend([self.ids.index(i) for i in ids])+ # 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])def index(self):"""diff --git a/src/python/txtai/scoring/tfidf.py b/src/python/txtai/scoring/tfidf.pyindex 69f518d9..f4eac335 100644--- a/src/python/txtai/scoring/tfidf.py+++ b/src/python/txtai/scoring/tfidf.py@@ -95,10 +95,10 @@ class TFIDF(Scoring):if self.terms:self.terms.delete(ids)- # Delete content+ # Delete content, ignore ids that were never indexedif self.documents:for uid in ids:- self.documents.pop(uid)+ self.documents.pop(uid, None)def index(self, documents=None):# Call base methoddiff --git a/test/python/testscoring/testkeyword.py b/test/python/testscoring/testkeyword.pyindex 540fc9c2..2d639607 100644--- a/test/python/testscoring/testkeyword.py+++ b/test/python/testscoring/testkeyword.py@@ -147,6 +147,25 @@ class TestKeyword(unittest.TestCase):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+ """++ # Terms index (bm25): Terms.delete used self.ids.index(), raising ValueError+ scoring = ScoringFactory.create({"method": "bm25", "terms": True, "content": True})+ scoring.index(self.data)++ scoring.delete([0, len(self.data) + 100])+ self.assertFalse(scoring.search("tops", 1))++ # TFIDF content only, terms disabled: TFIDF.delete used dict.pop(), raising KeyError+ scoring = ScoringFactory.create({"method": "tfidf", "content": True})+ scoring.index(self.data)++ scoring.delete([0, len(self.data) + 100])+ self.assertNotIn(0, scoring.documents)+def testTFIDF(self):"""Test tfidf