Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Renumber sections with ROW_NUMBER() when reindexing

Tanishq4db5db47cf54
diff --git a/src/python/txtai/database/schema/statement.py b/src/python/txtai/database/schema/statement.py
index 3652766b..790cb0d2 100644
--- a/src/python/txtai/database/schema/statement.py
+++ b/src/python/txtai/database/schema/statement.py
@@ -73,7 +73,7 @@ class Statement:
INSERT_SECTION = "INSERT INTO sections VALUES (?, ?, ?, ?, ?)"
DELETE_SECTIONS = "DELETE FROM sections WHERE id IN (SELECT id FROM batch)"
COPY_SECTIONS = (
- "INSERT INTO %s SELECT (select count(*) - 1 from sections s1 where s.indexid >= s1.indexid) indexid, "
+ "INSERT INTO %s SELECT ROW_NUMBER() OVER (ORDER BY s.indexid) - 1 AS indexid, "
+ "s.id, %s AS text, s.tags, s.entry FROM sections s LEFT JOIN documents d ON s.id = d.id ORDER BY indexid"
)
STREAM_SECTIONS = (
diff --git a/test/python/testdatabase/testrdbms.py b/test/python/testdatabase/testrdbms.py
index 96d633b0..aa0a3eec 100644
--- a/test/python/testdatabase/testrdbms.py
+++ b/test/python/testdatabase/testrdbms.py
@@ -656,6 +656,27 @@ class Common:
self.assertEqual(result["text"], self.data[4])
+ def testReindexDelete(self):
+ """
+ Test reindex renumbers indexids after a delete
+ """
+
+ # Index documents and delete two of them, which leaves gaps in the indexids
+ embeddings = Embeddings({"keyword": True, "content": self.backend})
+ embeddings.index([(uid, text, None) for uid, text in enumerate(self.data)])
+ embeddings.delete([0, 2])
+
+ # Reindex
+ embeddings.reindex({"keyword": True})
+
+ # Test indexids are sequential and still point to the same documents
+ result = embeddings.search("select indexid, id from txtai order by indexid", 10)
+ self.assertEqual([(row["indexid"], row["id"]) for row in result], [(0, "1"), (1, "3"), (2, "4"), (3, "5")])
+
+ # Search for best match
+ result = embeddings.search("lottery ticket", 1)[0]
+ self.assertEqual(result["text"], self.data[4])
+
def testReindexStore(self):
"""
Test reindex when the JSON data doesn't store the text column