Exemplo n.º 1
0
    def testGroup(self):
        # Check the group class on its own
        self.assertRaises(ValueError, a.Group, [], [])
        self.assertRaises(ValueError, a.Group, ['foo'], [])

        # Zero fields, single reducer
        g = a.Group([], r.count())
        ret = g.build_args()
        self.assertEqual(['GROUPBY', '0', 'REDUCE', 'COUNT', '0'], ret)

        # Single field, single reducer
        g = a.Group('foo', r.count())
        ret = g.build_args()
        self.assertEqual(['GROUPBY', '1', 'foo', 'REDUCE', 'COUNT', '0'], ret)

        # Multiple fields, single reducer
        g = a.Group(['foo', 'bar'], r.count())
        self.assertEqual(
            ['GROUPBY', '2', 'foo', 'bar', 'REDUCE', 'COUNT', '0'],
            g.build_args())

        # Multiple fields, multiple reducers
        g = a.Group(['foo', 'bar'], [r.count(), r.count_distinct('@fld1')])
        self.assertEqual([
            'GROUPBY', '2', 'foo', 'bar', 'REDUCE', 'COUNT', '0', 'REDUCE',
            'COUNT_DISTINCT', '1', '@fld1'
        ], g.build_args())
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
    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]))