def test_reducers(self): self.assertEqual((), r.count().args) self.assertEqual(('f1', ), r.sum('f1').args) self.assertEqual(('f1', ), r.min('f1').args) self.assertEqual(('f1', ), r.max('f1').args) self.assertEqual(('f1', ), r.avg('f1').args) self.assertEqual(('f1', ), r.tolist('f1').args) self.assertEqual(('f1', ), r.count_distinct('f1').args) self.assertEqual(('f1', ), r.count_distinctish('f1').args) self.assertEqual(('f1', '0.95'), r.quantile('f1', 0.95).args) self.assertEqual(('f1', ), r.stddev('f1').args) self.assertEqual(('f1', ), r.first_value('f1').args) self.assertEqual(('f1', 'BY', 'f2', 'ASC'), r.first_value('f1', a.Asc('f2')).args) self.assertEqual(('f1', 'BY', 'f1', 'ASC'), r.first_value('f1', a.Asc).args) self.assertEqual(('f1', '50'), r.random_sample('f1', 50).args)
def testAggregations(self): conn = self.redis() with conn as r: client = Client('myIndex', port=conn.port) client.redis.flushdb() # Creating the index definition and schema client.create_index( (NumericField('random_num'), TextField('title'), TextField('body'), TextField('parent'))) # Indexing a document client.add_document( 'search', title='RediSearch', body='Redisearch impements a search engine on top of redis', parent='redis', random_num=10) client.add_document( 'ai', title='RedisAI', body= 'RedisAI executes Deep Learning/Machine Learning models and managing their data.', parent='redis', random_num=3) client.add_document( 'json', title='RedisJson', body= 'RedisJSON implements ECMA-404 The JSON Data Interchange Standard as a native data type.', parent='redis', random_num=8) req = aggregations.AggregateRequest('redis').group_by( "@parent", reducers.count(), reducers.count_distinct('@title'), reducers.count_distinctish('@title'), reducers.sum("@random_num"), reducers.min("@random_num"), reducers.max("@random_num"), reducers.avg("@random_num"), reducers.stddev("random_num"), reducers.quantile("@random_num", 0.5), reducers.tolist("@title"), reducers.first_value("@title"), reducers.random_sample("@title", 2), ) res = client.aggregate(req) res = res.rows[0] self.assertEqual(len(res), 26) self.assertEqual(b'redis', res[1]) self.assertEqual(b'3', res[3]) self.assertEqual(b'3', res[5]) self.assertEqual(b'3', res[7]) self.assertEqual(b'21', res[9]) self.assertEqual(b'3', res[11]) self.assertEqual(b'10', res[13]) self.assertEqual(b'7', res[15]) self.assertEqual(b'3.60555127546', res[17]) self.assertEqual(b'10', res[19]) self.assertEqual([b'RediSearch', b'RedisAI', b'RedisJson'], res[21]) self.assertEqual(b'RediSearch', res[23]) self.assertEqual(2, len(res[25]))