Example #1
0
    def test_date_histogram_aggregation(self):
        agg = DateHistogram("hist", "created", "1h")

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations['hist']

        self.assertEqual(3, len(results['buckets']))
Example #2
0
    def test_date_histogram_aggregation(self):
        agg = DateHistogram("hist", "created", "1h")

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations['hist']

        self.assertEqual(3, len(results['buckets']))
Example #3
0
    def test_value_count_aggregation(self):
        agg = ValueCount("count", "price")

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations['count']

        self.assertEqual(5, results['value'])
Example #4
0
    def test_missing_aggregation(self):
        agg = Missing("missing", "color")

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations['missing']

        self.assertEqual(1, results['doc_count'])
Example #5
0
    def test_value_count_aggregation(self):
        agg = ValueCount("count", "price")

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations['count']

        self.assertEqual(5, results['value'])
Example #6
0
    def test_filter_aggregation(self):
        agg = Filter("filter").set_filter(Term("color", "blue")).add_aggregation(Avg("price").set_field("price"))

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations['filter']['price']['value']

        self.assertEqual((5 + 8) / 2.0, results)
Example #7
0
    def test_sum_aggregation(self):
        agg = Sum("sum")
        agg.set_field("price")

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations["sum"]
        self.assertEqual(5 + 8 + 1 + 3, results['value'])
Example #8
0
    def test_missing_aggregation(self):
        agg = Missing("missing", "color")

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations['missing']

        self.assertEqual(1, results['doc_count'])
Example #9
0
    def test_nested_aggregation(self):
        agg = Nested("resellers", "resellers")
        agg.add_aggregation(Min("min_price").set_field("price"))

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations['resellers']

        self.assertEqual(4.98, results['min_price']['value'])
Example #10
0
    def test_terms_aggregation(self):
        agg = Terms("terms").set_field("color")
        query = Query()
        query.add_aggregation(agg)

        results = self._index.search(query).aggregations['terms']

        self.assertEqual(2, results['buckets'][0]['doc_count'])
        self.assertEqual("blue", results['buckets'][0]['key'])
Example #11
0
    def test_geo_distance_aggregation(self):
        agg = GeoDistance("geo", "location", {'lat': 32.804654, 'lon': -117.242594})
        agg.add_range(to_value=100).set_unit("mi")

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations['geo']['buckets']

        self.assertEqual(2, results[0]['doc_count'])
Example #12
0
    def test_range_aggregation(self):
        agg = Range("range")
        agg.set_field("price")
        agg.add_range(from_value=1.5, to_value=5)

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations['range']['buckets']
        self.assertEqual(2, results[0]['doc_count'])
Example #13
0
    def test_terms_aggregation(self):
        agg = Terms("terms").set_field("color")
        query = Query()
        query.add_aggregation(agg)

        results = self._index.search(query).aggregations['terms']

        self.assertEqual(2, results['buckets'][0]['doc_count'])
        self.assertEqual("blue", results['buckets'][0]['key'])
Example #14
0
    def test_nested_aggregation(self):
        agg = Nested("resellers", "resellers")
        agg.add_aggregation(Min("min_price").set_field("price"))

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations['resellers']

        self.assertEqual(4.98, results['min_price']['value'])
Example #15
0
    def test_range_aggregation(self):
        agg = Range("range")
        agg.set_field("price")
        agg.add_range(from_value=1.5, to_value=5)

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations['range']['buckets']
        self.assertEqual(2, results[0]['doc_count'])
Example #16
0
    def test_geohashgrid_aggregation(self):
        agg = GeohashGrid("hash", "location")
        agg.set_precision(3)

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations['hash']

        self.assertEqual(2, results['buckets'][0]['doc_count'])
        self.assertEqual(1, results['buckets'][1]['doc_count'])
Example #17
0
    def test_filter_aggregation(self):
        agg = Filter("filter").set_filter(Term(
            "color", "blue")).add_aggregation(Avg("price").set_field("price"))

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(
            query).aggregations['filter']['price']['value']

        self.assertEqual((5 + 8) / 2.0, results)
Example #18
0
    def test_geohashgrid_aggregation(self):
        agg = GeohashGrid("hash", "location")
        agg.set_precision(3)

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations['hash']

        self.assertEqual(2, results['buckets'][0]['doc_count'])
        self.assertEqual(1, results['buckets'][1]['doc_count'])
Example #19
0
    def test_histogram_aggregation(self):
        agg = Histogram("hist", "price", 10)
        agg.set_minimum_document_count(0)  # should return empty buckets

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations["hist"]["buckets"]

        self.assertEqual(5, len(results))
        self.assertEqual(30, results[3]["key"])
        self.assertEqual(2, results[3]["doc_count"])
Example #20
0
    def test_histogram_aggregation(self):
        agg = Histogram("hist", "price", 10)
        agg.set_minimum_document_count(0)  # should return empty buckets

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations['hist']['buckets']

        self.assertEqual(5, len(results))
        self.assertEqual(30, results[3]['key'])
        self.assertEqual(2, results[3]['doc_count'])
Example #21
0
    def test_top_hits_aggregation(self):
        agg = Terms("terms").set_field("price")
        agg.add_aggregation(TopHits("hits").set_size(5))
        query = Query()
        query.add_aggregation(agg)

        results = self._index.search(query).aggregations['terms']["buckets"]

        self.assertEqual(2, len(results))
        self.assertEqual(5, len(results[0]["hits"]["hits"]["hits"]))
        for hit in results[0]['hits']['hits']['hits']:
            self.assertEqual(5, hit["_source"]["price"])
Example #22
0
    def test_stats_aggregation(self):
        agg = Stats("stats")
        agg.set_field("price")

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations['stats']

        self.assertEqual(4, results['count'])
        self.assertEqual(1, results['min'])
        self.assertEqual(8, results['max'])
        self.assertEqual((5 + 8 + 1 + 3) / 4.0, results['avg'])
        self.assertEqual((5 + 8 + 1 + 3), results['sum'])
Example #23
0
    def test_stats_aggregation(self):
        agg = Stats("stats")
        agg.set_field("price")

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations["stats"]

        self.assertEqual(4, results["count"])
        self.assertEqual(1, results["min"])
        self.assertEqual(8, results["max"])
        self.assertEqual((5 + 8 + 1 + 3) / 4.0, results["avg"])
        self.assertEqual((5 + 8 + 1 + 3), results["sum"])
Example #24
0
    def test_extended_stats_aggregation(self):
        agg = ExtendedStats("stats")
        agg.set_field("price")

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations['stats']

        self.assertEqual(4, results['count'])
        self.assertEqual(1, results['min'])
        self.assertEqual(8, results['max'])
        self.assertEqual((5 + 8 + 1 + 3) / 4.0, results['avg'])
        self.assertEqual((5 + 8 + 1 + 3), results['sum'])
        self.assertIn('sum_of_squares', results)
Example #25
0
    def test_date_range_aggregation(self):
        agg = DateRange("date")
        agg.set_field("created")
        agg.add_range(from_value=1390958535000).add_range(to_value=1390958535000)

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations["date"]

        for bucket in results["buckets"]:
            if "to" in bucket:
                self.assertEqual(1, bucket["doc_count"])
            elif "from" in bucket:
                self.assertEqual(2, bucket["doc_count"])
Example #26
0
    def test_date_range_aggregation(self):
        agg = DateRange("date")
        agg.set_field("created")
        agg.add_range(from_value=1390958535000).add_range(
            to_value=1390958535000)

        query = Query()
        query.add_aggregation(agg)
        results = self._index.search(query).aggregations['date']

        for bucket in results['buckets']:
            if 'to' in bucket:
                self.assertEqual(1, bucket['doc_count'])
            elif 'from' in bucket:
                self.assertEqual(2, bucket['doc_count'])