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']))
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'])
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'])
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)
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'])
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'])
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'])
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'])
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'])
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'])
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)
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"])
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'])
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"])
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'])
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"])
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)
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"])
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'])