|
11 | 11 | import six
|
12 | 12 |
|
13 | 13 | from redisearch import *
|
| 14 | +import redisearch.aggregation as aggregations |
| 15 | +import redisearch.reducers as reducers |
14 | 16 |
|
15 | 17 | WILL_PLAY_TEXT = os.path.abspath(os.path.dirname(__file__)) + '/will_play_text.csv.bz2'
|
16 | 18 |
|
@@ -171,6 +173,33 @@ def getCleanClient(self, name):
|
171 | 173 | pass
|
172 | 174 |
|
173 | 175 | 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) |
174 | 203 |
|
175 | 204 | def testPayloads(self):
|
176 | 205 |
|
@@ -658,6 +687,72 @@ def testGet(self):
|
658 | 687 | self.assertEqual([['f1', 'some valid content dd2', 'f2', 'this is sample text ff2']], client.get('doc2'))
|
659 | 688 | 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'))
|
660 | 689 |
|
| 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 | + |
661 | 756 |
|
662 | 757 | if __name__ == '__main__':
|
663 | 758 |
|
|
0 commit comments