Example #1
0
    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'])
Example #2
0
    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"])
Example #3
0
    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'])
Example #4
0
    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"])
Example #5
0
    def test_indices_filter(self):
        indices_filter = Indices(BoolNot(Term("color", "blue")),
                                 [self._index1.name])
        indices_filter.set_no_match_filter(BoolNot(Term("color", "yellow")))
        query = Query().set_filter(indices_filter)

        # search over the alias
        index = self._get_client().get_index("indices_filter")
        results = index.search(query)

        # ensure that the proper docs have been filtered out for each index
        self.assertEqual(5, len(results))
        for result in results.results:
            data = result.data
            color = data['color']
            if result.get_index() == self._index1.name:
                self.assertNotEqual("blue", color)
            else:
                self.assertNotEqual("yellow", color)