Skip to content

Commit c162651

Browse files
committed
added tests for addhash and aggregations
1 parent 67b1655 commit c162651

File tree

2 files changed

+99
-2
lines changed

2 files changed

+99
-2
lines changed

redisearch/reducers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class avg(FieldOnlyReducer):
5656
def __init__(self, field):
5757
super(avg, self).__init__(field)
5858

59+
5960
class tolist(FieldOnlyReducer):
6061
"""
6162
Returns all the matched properties in a list
@@ -65,6 +66,7 @@ class tolist(FieldOnlyReducer):
6566
def __init__(self, field):
6667
super(tolist, self).__init__(field)
6768

69+
6870
class count_distinct(FieldOnlyReducer):
6971
"""
7072
Calculate the number of distinct values contained in all the results in
@@ -82,7 +84,7 @@ class count_distinctish(FieldOnlyReducer):
8284
group for the given field. This uses a faster algorithm than
8385
`count_distinct` but is less accurate
8486
"""
85-
name = 'COUNT_DISTINCTISH'
87+
NAME = 'COUNT_DISTINCTISH'
8688

8789

8890
class quantile(Reducer):
@@ -101,7 +103,7 @@ class stddev(FieldOnlyReducer):
101103
"""
102104
Return the standard deviation for the values within the group
103105
"""
104-
name = 'STDDEV'
106+
NAME = 'STDDEV'
105107

106108
def __init__(self, field):
107109
super(stddev, self).__init__(field)

test/test.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import six
1212

1313
from redisearch import *
14+
import redisearch.aggregation as aggregations
15+
import redisearch.reducers as reducers
1416

1517
WILL_PLAY_TEXT = os.path.abspath(os.path.dirname(__file__)) + '/will_play_text.csv.bz2'
1618

@@ -171,6 +173,33 @@ def getCleanClient(self, name):
171173
pass
172174

173175
return client
176+
177+
def testAddHash(self):
178+
conn = self.redis()
179+
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')
196+
197+
# Searching with complext parameters:
198+
q = Query("search engine").verbatim().no_content().paging(0, 5)
199+
200+
res = client.search(q)
201+
202+
self.assertTrue('doc1', res.docs[0].id)
174203

175204
def testPayloads(self):
176205

@@ -658,6 +687,72 @@ def testGet(self):
658687
self.assertEqual([['f1', 'some valid content dd2', 'f2', 'this is sample text ff2']], client.get('doc2'))
659688
self.assertEqual([['f1', 'some valid content dd1', 'f2', 'this is sample text ff1'], ['f1', 'some valid content dd2', 'f2', 'this is sample text ff2']], client.get('doc1', 'doc2'))
660689

690+
def testAggregations(self):
691+
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]))
755+
661756

662757
if __name__ == '__main__':
663758

0 commit comments

Comments
 (0)