Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Add count method to database, closes #517

davidmezzettiaaa5a7227354
diff --git a/src/python/txtai/database/base.py b/src/python/txtai/database/base.py
index 576e12ee..f7ef14d3 100644
--- a/src/python/txtai/database/base.py
+++ b/src/python/txtai/database/base.py
@@ -124,6 +124,16 @@ class Database:
raise NotImplementedError
+ def count(self):
+ """
+ Retrieves the count of this database instance.
+
+ Returns:
+ total database count
+ """
+
+ raise NotImplementedError
+
def search(self, query, similarity=None, limit=None):
"""
Runs a search against the database. Supports the following methods:
@@ -161,6 +171,7 @@ class Database:
token = f"{Token.SIMILAR_TOKEN}{x}"
if where and token in where:
where = where.replace(token, self.embed(similarity, x))
+
elif similarity:
# Not a SQL query, load similarity results, if any
where = self.embed(similarity, 0)
diff --git a/src/python/txtai/database/filedb.py b/src/python/txtai/database/filedb.py
index dcc45d5b..7f4fa636 100644
--- a/src/python/txtai/database/filedb.py
+++ b/src/python/txtai/database/filedb.py
@@ -88,6 +88,7 @@ class FileDB(Database):
# Queries
SELECT_IDS = "SELECT indexid, id FROM sections WHERE id in (SELECT id FROM batch)"
+ COUNT_IDS = "SELECT count(indexid) FROM sections"
# Partial sql clauses
TABLE_CLAUSE = (
@@ -230,6 +231,10 @@ class FileDB(Database):
# Format and return results
return self.cursor.fetchall()
+ def count(self):
+ self.cursor.execute(FileDB.COUNT_IDS)
+ return self.cursor.fetchone()[0]
+
def resolve(self, name, alias=None):
# Standard column names
sections = ["indexid", "id", "tags", "entry"]