Skip to content

Commit ea171a8

Browse files
committed
count method
1 parent ee3fd30 commit ea171a8

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

aredis_om/model/model.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,25 @@ async def all(self, batch_size=10):
760760
return await query.execute()
761761
return await self.execute()
762762

763+
async def count(self):
764+
args = ["FT.AGGREGATE", self.model.Meta.index_name, "*"]
765+
if self.expression:
766+
args += ["FILTER", *self.query]
767+
args += [
768+
"APPLY",
769+
"matched_terms()",
770+
"AS",
771+
"countable",
772+
"GROUPBY",
773+
"1",
774+
"@countable",
775+
"REDUCE",
776+
"COUNT",
777+
"0",
778+
]
779+
raw_result = await self.model.db().execute_command(*args)
780+
return sum([count for [count, _] in raw_result])
781+
763782
def sort_by(self, *fields: str):
764783
if not fields:
765784
return self

0 commit comments

Comments
 (0)