@@ -176,30 +176,31 @@ def getCleanClient(self, name):
176176
177177 def testAddHash (self ):
178178 conn = self .redis ()
179+
180+ with conn as r :
181+ # Creating a client with a given index name
182+ client = Client ('idx' , port = conn .port )
179183
180- # Creating a client with a given index name
181- client = Client ('idx' , port = conn .port )
182-
183- client .redis .flushdb ()
184- # Creating the index definition and schema
185- client .create_index ((TextField ('title' ,
186- weight = 5.0 ), TextField ('body' )))
187- redis_client = redis .Redis (port = conn .port )
188- redis_client .hset (
189- 'doc1' ,
190- mapping = {
191- 'title' : 'RediSearch' ,
192- 'body' : 'Redisearch impements a search engine on top of redis'
193- })
194- # Indexing the hash
195- client .add_document_hash ('doc1' )
184+ client .redis .flushdb ()
185+ # Creating the index definition and schema
186+ client .create_index ((TextField ('title' ,
187+ weight = 5.0 ), TextField ('body' )))
188+
189+ client .redis .hset (
190+ 'doc1' ,
191+ mapping = {
192+ 'title' : 'RediSearch' ,
193+ 'body' : 'Redisearch impements a search engine on top of redis'
194+ })
195+ # Indexing the hash
196+ client .add_document_hash ('doc1' )
196197
197- # Searching with complext parameters:
198- q = Query ("search engine" ).verbatim ().no_content ().paging (0 , 5 )
198+ # Searching with complext parameters:
199+ q = Query ("search engine" ).verbatim ().no_content ().paging (0 , 5 )
199200
200- res = client .search (q )
201+ res = client .search (q )
201202
202- self .assertTrue ('doc1' , res .docs [0 ].id )
203+ self .assertEqual ('doc1' , res .docs [0 ].id )
203204
204205 def testPayloads (self ):
205206
@@ -689,69 +690,71 @@ def testGet(self):
689690
690691 def testAggregations (self ):
691692 conn = self .redis ()
692- client = Client ('myIndex' , port = conn .port )
693- client .redis .flushdb ()
694-
695- # Creating the index definition and schema
696- client .create_index ((NumericField ('random_num' ), TextField ('title' ),
697- TextField ('body' ), TextField ('parent' )))
698-
699- # Indexing a document
700- client .add_document (
701- 'search' ,
702- title = 'RediSearch' ,
703- body = 'Redisearch impements a search engine on top of redis' ,
704- parent = 'redis' ,
705- random_num = 10 )
706- client .add_document (
707- 'ai' ,
708- title = 'RedisAI' ,
709- body =
710- 'RedisAI executes Deep Learning/Machine Learning models and managing their data.' ,
711- parent = 'redis' ,
712- random_num = 3 )
713- client .add_document (
714- 'json' ,
715- title = 'RedisJson' ,
716- body =
717- 'RedisJSON implements ECMA-404 The JSON Data Interchange Standard as a native data type.' ,
718- parent = 'redis' ,
719- random_num = 8 )
720-
721- req = aggregations .AggregateRequest ('redis' ).group_by (
722- "@parent" ,
723- reducers .count (),
724- reducers .count_distinct ('@title' ),
725- reducers .count_distinctish ('@title' ),
726- reducers .sum ("@random_num" ),
727- reducers .min ("@random_num" ),
728- reducers .max ("@random_num" ),
729- reducers .avg ("@random_num" ),
730- reducers .stddev ("random_num" ),
731- reducers .quantile ("@random_num" , 0.5 ),
732- reducers .tolist ("@title" ),
733- reducers .first_value ("@title" ),
734- reducers .random_sample ("@title" , 2 ),
735- )
736-
737- res = client .aggregate (req )
738-
739- res = res .rows [0 ]
740-
741- self .assertEqual (len (res ), 26 )
742- self .assertEqual (b'redis' , res [1 ])
743- self .assertEqual (b'3' , res [3 ])
744- self .assertEqual (b'3' , res [5 ])
745- self .assertEqual (b'3' , res [7 ])
746- self .assertEqual (b'21' , res [9 ])
747- self .assertEqual (b'3' , res [11 ])
748- self .assertEqual (b'10' , res [13 ])
749- self .assertEqual (b'7' , res [15 ])
750- self .assertEqual (b'3.60555127546' , res [17 ])
751- self .assertEqual (b'10' , res [19 ])
752- self .assertEqual ([b'RediSearch' , b'RedisAI' , b'RedisJson' ], res [21 ])
753- self .assertEqual (b'RediSearch' , res [23 ])
754- self .assertEqual (2 , len (res [25 ]))
693+
694+ with conn as r :
695+ client = Client ('myIndex' , port = conn .port )
696+ client .redis .flushdb ()
697+
698+ # Creating the index definition and schema
699+ client .create_index ((NumericField ('random_num' ), TextField ('title' ),
700+ TextField ('body' ), TextField ('parent' )))
701+
702+ # Indexing a document
703+ client .add_document (
704+ 'search' ,
705+ title = 'RediSearch' ,
706+ body = 'Redisearch impements a search engine on top of redis' ,
707+ parent = 'redis' ,
708+ random_num = 10 )
709+ client .add_document (
710+ 'ai' ,
711+ title = 'RedisAI' ,
712+ body =
713+ 'RedisAI executes Deep Learning/Machine Learning models and managing their data.' ,
714+ parent = 'redis' ,
715+ random_num = 3 )
716+ client .add_document (
717+ 'json' ,
718+ title = 'RedisJson' ,
719+ body =
720+ 'RedisJSON implements ECMA-404 The JSON Data Interchange Standard as a native data type.' ,
721+ parent = 'redis' ,
722+ random_num = 8 )
723+
724+ req = aggregations .AggregateRequest ('redis' ).group_by (
725+ "@parent" ,
726+ reducers .count (),
727+ reducers .count_distinct ('@title' ),
728+ reducers .count_distinctish ('@title' ),
729+ reducers .sum ("@random_num" ),
730+ reducers .min ("@random_num" ),
731+ reducers .max ("@random_num" ),
732+ reducers .avg ("@random_num" ),
733+ reducers .stddev ("random_num" ),
734+ reducers .quantile ("@random_num" , 0.5 ),
735+ reducers .tolist ("@title" ),
736+ reducers .first_value ("@title" ),
737+ reducers .random_sample ("@title" , 2 ),
738+ )
739+
740+ res = client .aggregate (req )
741+
742+ res = res .rows [0 ]
743+
744+ self .assertEqual (len (res ), 26 )
745+ self .assertEqual (b'redis' , res [1 ])
746+ self .assertEqual (b'3' , res [3 ])
747+ self .assertEqual (b'3' , res [5 ])
748+ self .assertEqual (b'3' , res [7 ])
749+ self .assertEqual (b'21' , res [9 ])
750+ self .assertEqual (b'3' , res [11 ])
751+ self .assertEqual (b'10' , res [13 ])
752+ self .assertEqual (b'7' , res [15 ])
753+ self .assertEqual (b'3.60555127546' , res [17 ])
754+ self .assertEqual (b'10' , res [19 ])
755+ self .assertEqual ([b'RediSearch' , b'RedisAI' , b'RedisJson' ], res [21 ])
756+ self .assertEqual (b'RediSearch' , res [23 ])
757+ self .assertEqual (2 , len (res [25 ]))
755758
756759
757760if __name__ == '__main__' :
0 commit comments