Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Update reindex action to support new 6.x configuration, closes #557

davidmezzettiaadda6b5f7c6
diff --git a/src/python/txtai/database/base.py b/src/python/txtai/database/base.py
index f7ef14d3..58afff04 100644
--- a/src/python/txtai/database/base.py
+++ b/src/python/txtai/database/base.py
@@ -27,29 +27,8 @@ class Database:
config: database configuration
"""
- # Database configuration
- self.config = config
-
- # SQL parser
- self.sql = SQL(self)
-
- # Load objects encoder
- encoder = self.config.get("objects")
- self.encoder = EncoderFactory.create(encoder) if encoder else None
-
- # Transform columns
- columns = config.get("columns", {})
- self.text = columns.get("text", "text")
- self.object = columns.get("object", "object")
-
- # Custom functions and expressions
- self.functions, self.expressions = None, None
-
- # Load custom functions
- self.registerfunctions(self.config)
-
- # Load custom expressions
- self.registerexpressions(self.config)
+ # Initialize configuration
+ self.configure(config)
def load(self, path):
"""
@@ -82,13 +61,13 @@ class Database:
raise NotImplementedError
- def reindex(self, columns=None):
+ def reindex(self, config):
"""
Reindexes internal database content and streams results back. This method must renumber indexids
sequentially as deletes could have caused indexid gaps.
Args:
- columns: optional list of document columns used to rebuild data
+ config: new configuration
"""
raise NotImplementedError
@@ -235,6 +214,38 @@ class Database:
raise NotImplementedError
+ def configure(self, config):
+ """
+ Initialize configuration.
+
+ Args:
+ config: configuration
+ """
+
+ # Database configuration
+ self.config = config
+
+ # SQL parser
+ self.sql = SQL(self)
+
+ # Load objects encoder
+ encoder = self.config.get("objects")
+ self.encoder = EncoderFactory.create(encoder) if encoder else None
+
+ # Transform columns
+ columns = config.get("columns", {})
+ self.text = columns.get("text", "text")
+ self.object = columns.get("object", "object")
+
+ # Custom functions and expressions
+ self.functions, self.expressions = None, None
+
+ # Load custom functions
+ self.registerfunctions(self.config)
+
+ # Load custom expressions
+ self.registerexpressions(self.config)
+
def registerfunctions(self, config):
"""
Register custom functions. This method stores the function details for underlying
@@ -264,6 +275,7 @@ class Database:
# Store function details
functions.append((name, argcount, fn))
+ # pylint: disable=W0201
self.functions = functions
def registerexpressions(self, config):
@@ -283,6 +295,7 @@ class Database:
if name and expression:
expressions[name] = self.sql.snippet(expression)
+ # pylint: disable=W0201
self.expressions = expressions
def execute(self, function, *args):
diff --git a/src/python/txtai/database/rdbms.py b/src/python/txtai/database/rdbms.py
index f6dd5f90..30d6be3e 100644
--- a/src/python/txtai/database/rdbms.py
+++ b/src/python/txtai/database/rdbms.py
@@ -79,12 +79,13 @@ class RDBMS(Database):
self.cursor.execute(Statement.DELETE_OBJECTS)
self.cursor.execute(Statement.DELETE_SECTIONS)
- def reindex(self, columns=None):
+ def reindex(self, config):
if self.connection:
- # Resolve and build column strings if provided
- select = "text"
- if columns:
- select = "|| ' ' ||".join([self.resolve(c) for c in columns])
+ # Set new configuration
+ self.configure(config)
+
+ # Resolve text column
+ select = self.resolve(self.text)
# Initialize reindex operation
name = self.reindexstart()
@@ -94,11 +95,15 @@ class RDBMS(Database):
# Stream new results
self.cursor.execute(Statement.STREAM_SECTIONS % name)
- for uid, text, obj, tags in self.rows():
+ for uid, text, data, obj, tags in self.rows():
if not text and self.encoder and obj:
yield (uid, self.encoder.decode(obj), tags)
else:
- yield (uid, text, tags)
+ # Read JSON data, if provided
+ data = json.loads(data) if data and isinstance(data, str) else data
+
+ # Stream data if available, otherwise use section text
+ yield (uid, data if data else text, tags)
# Swap as new table
self.cursor.execute(Statement.DROP_SECTIONS)
diff --git a/src/python/txtai/database/schema/statement.py b/src/python/txtai/database/schema/statement.py
index b949c6ff..8111ddad 100644
--- a/src/python/txtai/database/schema/statement.py
+++ b/src/python/txtai/database/schema/statement.py
@@ -76,7 +76,11 @@ class Statement:
"INSERT INTO %s SELECT (select count(*) - 1 from sections s1 where s.indexid >= s1.indexid) 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 = "SELECT s.id, s.text, object, s.tags FROM %s s LEFT JOIN objects o ON s.id = o.id ORDER BY indexid"
+ STREAM_SECTIONS = (
+ "SELECT s.id, s.text, data, object, s.tags FROM %s s "
+ + "LEFT JOIN documents d ON s.id = d.id "
+ + "LEFT JOIN objects o ON s.id = o.id ORDER BY indexid"
+ )
DROP_SECTIONS = "DROP TABLE sections"
RENAME_SECTIONS = "ALTER TABLE %s RENAME TO sections"
diff --git a/src/python/txtai/embeddings/base.py b/src/python/txtai/embeddings/base.py
index 04b40fb3..01cf9d0d 100644
--- a/src/python/txtai/embeddings/base.py
+++ b/src/python/txtai/embeddings/base.py
@@ -260,18 +260,21 @@ class Embeddings:
return deletes
- def reindex(self, config, columns=None, function=None):
+ def reindex(self, config=None, function=None, **kwargs):
"""
Recreates the approximate nearest neighbor (ann) index using config. This method only works if document
content storage is enabled.
Args:
config: new config
- columns: optional list of document columns used to rebuild data
function: optional function to prepare content for indexing
+ kwargs: additional configuration as keyword args
"""
if self.database:
+ # Merge configuration into single dictionary
+ config = {**config, **kwargs} if config and kwargs else config if config else kwargs
+
# Keep content and objects parameters to ensure database is preserved
config["content"] = self.config["content"]
if "objects" in self.config:
@@ -286,9 +289,9 @@ class Embeddings:
# Reindex
if function:
- self.index(function(self.database.reindex(columns)), True)
+ self.index(function(self.database.reindex(self.config)), True)
else:
- self.index(self.database.reindex(columns), True)
+ self.index(self.database.reindex(self.config), True)
def transform(self, document):
"""
@@ -742,6 +745,9 @@ class Embeddings:
# Reset archive since this is a new index
self.archive = None
+ # Initialize ANN, will be created after index transformations complete
+ self.ann = None
+
# Create scoring only if term indexing is enabled
scoring = self.config.get("scoring")
if scoring and isinstance(scoring, dict) and self.config["scoring"].get("terms"):
diff --git a/test/python/testdatabase/testrdbms.py b/test/python/testdatabase/testrdbms.py
index f8f2e7be..f8af1584 100644
--- a/test/python/testdatabase/testrdbms.py
+++ b/test/python/testdatabase/testrdbms.py
@@ -550,7 +550,7 @@ class Common:
self.embeddings.delete(([0, 1]))
# Reindex
- self.embeddings.reindex({"path": "sentence-transformers/nli-mpnet-base-v2"}, ["text"])
+ self.embeddings.reindex({"path": "sentence-transformers/nli-mpnet-base-v2"})
# Search for best match
result = self.embeddings.search("feel good story", 1)[0]