Summary
The current FindQuery.count() implementation uses FT.SEARCH with LIMIT 0 0 and nocontent=True:
async def count(self):
query = self.copy(offset=0, limit=0, nocontent=True)
result = await query.execute(exhaust_results=True, return_raw_result=True)
return result[0]
This works, but FT.AGGREGATE with a count reducer may be a better approach.
Why FT.AGGREGATE might be better
- Purpose-built for aggregations -
FT.AGGREGATE is designed for operations like counting, grouping, etc.
- More flexible - Could support
count() with grouping (e.g., count by category)
- Potentially more efficient - Aggregation pipeline is optimized for this use case
Example with FT.AGGREGATE
FT.AGGREGATE idx * GROUPBY 0 REDUCE COUNT 0 AS total
This returns just the count without any document data.
Tasks
Summary
The current
FindQuery.count()implementation usesFT.SEARCHwithLIMIT 0 0andnocontent=True:This works, but
FT.AGGREGATEwith a count reducer may be a better approach.Why FT.AGGREGATE might be better
FT.AGGREGATEis designed for operations like counting, grouping, etc.count()with grouping (e.g., count by category)Example with FT.AGGREGATE
This returns just the count without any document data.
Tasks
FT.SEARCH LIMIT 0 0vsFT.AGGREGATEfor countFT.AGGREGATEprovides any performance benefitsModel.find().count(group_by="category"))