def test_avg_aggregation(self): agg = Avg("avg") agg.set_field("price") query = Query() query.add_aggregation(agg) results = self._index.search(query).aggregations["avg"] self.assertEqual((5 + 8 + 1 + 3) / 4.0, results['value'])
def test_ip_range_aggregation(self): agg = IpRange("ip", "address") agg.add_range(from_value="192.168.1.101").add_range(to_value="192.168.1.200").add_mask_range("192.168.1.0/24") query = Query() query.add_aggregation(agg) results = self._index.search(query).aggregations["ip"] for bucket in results["buckets"]: if "from" in bucket and "to" in bucket: # the CIDR mask self.assertEqual(3, bucket["doc_count"]) else: self.assertEqual(2, bucket["doc_count"])
def test_ip_range_aggregation(self): agg = IpRange("ip", "address") agg.add_range(from_value="192.168.1.101").add_range( to_value="192.168.1.200").add_mask_range("192.168.1.0/24") query = Query() query.add_aggregation(agg) results = self._index.search(query).aggregations['ip'] for bucket in results['buckets']: if 'from' in bucket and 'to' in bucket: #the CIDR mask self.assertEqual(3, bucket['doc_count']) else: self.assertEqual(2, bucket['doc_count'])
def test_max_aggregation(self): agg = Max("min_price") agg.set_field("price") query = Query() query.add_aggregation(agg) results = self._index.get_doc_type("test").search(query).aggregations["min_price"] self.assertAlmostEqual(8, results["value"]) # test using a script agg.set_script(Script("_value * conversion_rate", {"conversion_rate": 1.2})) query = Query() query.add_aggregation(agg) results = self._index.get_doc_type("test").search(query).aggregations["min_price"] self.assertEqual(8 * 1.2, results["value"])