@@ -345,6 +345,7 @@ def __init__(
345
345
limit : int = DEFAULT_PAGE_SIZE ,
346
346
page_size : int = DEFAULT_PAGE_SIZE ,
347
347
sort_fields : Optional [List [str ]] = None ,
348
+ nocontent : bool = False ,
348
349
):
349
350
if not has_redisearch (model .db ()):
350
351
raise RedisModelError (
@@ -358,6 +359,7 @@ def __init__(
358
359
self .offset = offset
359
360
self .limit = limit
360
361
self .page_size = page_size
362
+ self .nocontent = nocontent
361
363
362
364
if sort_fields :
363
365
self .sort_fields = self .validate_sort_fields (sort_fields )
@@ -377,6 +379,7 @@ def dict(self) -> Dict[str, Any]:
377
379
limit = self .limit ,
378
380
expressions = copy (self .expressions ),
379
381
sort_fields = copy (self .sort_fields ),
382
+ nocontent = self .nocontent ,
380
383
)
381
384
382
385
def copy (self , ** kwargs ):
@@ -716,18 +719,23 @@ def resolve_redisearch_query(cls, expression: ExpressionOrNegated) -> str:
716
719
717
720
return result
718
721
719
- async def execute (self , exhaust_results = True ):
722
+ async def execute (self , exhaust_results = True , return_raw_result = False ):
720
723
args = ["ft.search" , self .model .Meta .index_name , self .query , * self .pagination ]
721
724
if self .sort_fields :
722
725
args += self .resolve_redisearch_sort_fields ()
723
726
727
+ if self .nocontent :
728
+ args .append ("NOCONTENT" )
729
+
724
730
# Reset the cache if we're executing from offset 0.
725
731
if self .offset == 0 :
726
732
self ._model_cache .clear ()
727
733
728
734
# If the offset is greater than 0, we're paginating through a result set,
729
735
# so append the new results to results already in the cache.
730
736
raw_result = await self .model .db ().execute_command (* args )
737
+ if return_raw_result :
738
+ return raw_result
731
739
count = raw_result [0 ]
732
740
results = self .model .from_redis (raw_result )
733
741
self ._model_cache += results
@@ -759,6 +767,11 @@ async def first(self):
759
767
raise NotFoundError ()
760
768
return results [0 ]
761
769
770
+ async def count (self ):
771
+ query = self .copy (offset = 0 , limit = 0 , nocontent = True )
772
+ result = await query .execute (exhaust_results = True , return_raw_result = True )
773
+ return result [0 ]
774
+
762
775
async def all (self , batch_size = DEFAULT_PAGE_SIZE ):
763
776
if batch_size != self .page_size :
764
777
query = self .copy (page_size = batch_size , limit = batch_size )
@@ -1175,7 +1188,7 @@ def validate_primary_key(cls):
1175
1188
if primary_keys == 0 :
1176
1189
raise RedisModelError ("You must define a primary key for the model" )
1177
1190
elif primary_keys == 2 :
1178
- cls .__fields__ .pop ('pk' )
1191
+ cls .__fields__ .pop ("pk" )
1179
1192
elif primary_keys > 2 :
1180
1193
raise RedisModelError ("You must define only one primary key for a model" )
1181
1194
0 commit comments