Beispiel #1
0
    def testAggRequest(self):
        req = a.AggregateRequest()
        self.assertEqual(['*'], req.build_args())

        # Test with group_by
        req = a.AggregateRequest().group_by('@foo', r.count())
        self.assertEqual(['*', 'GROUPBY', '1', '@foo', 'REDUCE', 'COUNT', '0'],
                         req.build_args())

        # Test with limit
        req = a.AggregateRequest().\
            group_by('@foo', r.count()).\
            sort_by('@foo')
        self.assertEqual([
            '*', 'GROUPBY', '1', '@foo', 'REDUCE', 'COUNT', '0', 'SORTBY', '1',
            '@foo'
        ], req.build_args())

        # Test with sort_by
        req = a.AggregateRequest().group_by('@foo', r.count()).sort_by('@date')
        # print req.build_args()
        self.assertEqual([
            '*', 'GROUPBY', '1', '@foo', 'REDUCE', 'COUNT', '0', 'SORTBY', '1',
            '@date'
        ], req.build_args())

        req = a.AggregateRequest().group_by('@foo',
                                            r.count()).sort_by(a.Desc('@date'))
        # print req.build_args()
        self.assertEqual([
            '*', 'GROUPBY', '1', '@foo', 'REDUCE', 'COUNT', '0', 'SORTBY', '2',
            '@date', 'DESC'
        ], req.build_args())

        req = a.AggregateRequest().group_by('@foo', r.count()).sort_by(
            a.Desc('@date'), a.Asc('@time'))
        # print req.build_args()
        self.assertEqual([
            '*', 'GROUPBY', '1', '@foo', 'REDUCE', 'COUNT', '0', 'SORTBY', '4',
            '@date', 'DESC', '@time', 'ASC'
        ], req.build_args())

        req = a.AggregateRequest().group_by('@foo',
                                            r.count()).sort_by(a.Desc('@date'),
                                                               a.Asc('@time'),
                                                               max=10)
        self.assertEqual([
            '*', 'GROUPBY', '1', '@foo', 'REDUCE', 'COUNT', '0', 'SORTBY', '4',
            '@date', 'DESC', '@time', 'ASC', 'MAX', '10'
        ], req.build_args())
Beispiel #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)
def get_movie_group_by(field):
    req = aggregations.AggregateRequest("*").group_by(
        "@" + field,
        reducers.count().alias("nb_of_movies")).sort_by(
            aggregations.Asc("@" + field)).limit(0, 1000)

    res = g.movieIdx.aggregate(req)

    reslist = []
    for row in res.rows:
        item = {
            row[0].decode("utf-8"): row[1].decode("utf-8"),
            row[2].decode("utf-8"): int(row[3].decode("utf-8"))
        }
        reslist.append(item)

    dictResult = {
        "totalResults": len(res.rows),
        "rows": reslist,
    }

    return dictResult
Beispiel #4
0
    def testAggRequest(self):
        req = a.AggregateRequest()
        self.assertEqual(['*'], req.build_args())

        # Test with group_by
        req = a.AggregateRequest().group_by('@foo', r.count())
        self.assertEqual(['*', 'GROUPBY', '1', '@foo', 'REDUCE', 'COUNT', '0'],
                         req.build_args())

        # Test with group_by and alias on reducer
        req = a.AggregateRequest().group_by('@foo',
                                            r.count().alias('foo_count'))
        self.assertEqual([
            '*', 'GROUPBY', '1', '@foo', 'REDUCE', 'COUNT', '0', 'AS',
            'foo_count'
        ], req.build_args())

        # Test with limit
        req = a.AggregateRequest(). \
            group_by('@foo', r.count()). \
            sort_by('@foo')
        self.assertEqual([
            '*', 'GROUPBY', '1', '@foo', 'REDUCE', 'COUNT', '0', 'SORTBY', '1',
            '@foo'
        ], req.build_args())

        # Test with apply
        req = a.AggregateRequest(). \
            apply(foo="@bar / 2"). \
            group_by('@foo', r.count())

        self.assertEqual([
            '*', 'APPLY', '@bar / 2', 'AS', 'foo', 'GROUPBY', '1', '@foo',
            'REDUCE', 'COUNT', '0'
        ], req.build_args())

        # Test with filter
        req = a.AggregateRequest().group_by('@foo',
                                            r.count()).filter("@foo=='bar'")
        self.assertEqual([
            '*', 'GROUPBY', '1', '@foo', 'REDUCE', 'COUNT', '0', 'FILTER',
            "@foo=='bar'"
        ], req.build_args())

        # Test with filter on different state of the pipeline
        req = a.AggregateRequest().filter("@foo=='bar'").group_by(
            '@foo', r.count())
        self.assertEqual([
            '*', 'FILTER', "@foo=='bar'", 'GROUPBY', '1', '@foo', 'REDUCE',
            'COUNT', '0'
        ], req.build_args())

        # Test with filter on different state of the pipeline
        req = a.AggregateRequest().filter(["@foo=='bar'", "@foo2=='bar2'"
                                           ]).group_by('@foo', r.count())
        self.assertEqual([
            '*', 'FILTER', "@foo=='bar'", 'FILTER', "@foo2=='bar2'", 'GROUPBY',
            '1', '@foo', 'REDUCE', 'COUNT', '0'
        ], req.build_args())

        # Test with sort_by
        req = a.AggregateRequest().group_by('@foo', r.count()).sort_by('@date')
        # print req.build_args()
        self.assertEqual([
            '*', 'GROUPBY', '1', '@foo', 'REDUCE', 'COUNT', '0', 'SORTBY', '1',
            '@date'
        ], req.build_args())

        req = a.AggregateRequest().group_by('@foo',
                                            r.count()).sort_by(a.Desc('@date'))
        # print req.build_args()
        self.assertEqual([
            '*', 'GROUPBY', '1', '@foo', 'REDUCE', 'COUNT', '0', 'SORTBY', '2',
            '@date', 'DESC'
        ], req.build_args())

        req = a.AggregateRequest().group_by('@foo', r.count()).sort_by(
            a.Desc('@date'), a.Asc('@time'))
        # print req.build_args()
        self.assertEqual([
            '*', 'GROUPBY', '1', '@foo', 'REDUCE', 'COUNT', '0', 'SORTBY', '4',
            '@date', 'DESC', '@time', 'ASC'
        ], req.build_args())

        req = a.AggregateRequest().group_by('@foo',
                                            r.count()).sort_by(a.Desc('@date'),
                                                               a.Asc('@time'),
                                                               max=10)
        self.assertEqual([
            '*', 'GROUPBY', '1', '@foo', 'REDUCE', 'COUNT', '0', 'SORTBY', '4',
            '@date', 'DESC', '@time', 'ASC', 'MAX', '10'
        ], req.build_args())