示例#1
0
 def test_normalize_buckets(self):
     my_agg = Aggs(sample.EXPECTED_AGG_QUERY, mapping=MAPPING)
     response = Aggregations(
         data=sample.ES_AGG_RESPONSE,
         aggs=my_agg,
         index=None,
         client=None,
         query=None,
     ).serialize_as_normalized()
     self.assertEqual(ordered(response),
                      ordered(sample.EXPECTED_NORMALIZED_RESPONSE))
示例#2
0
def test_search_query_combines_query():
    s = Search()

    s2 = s.query("match", f=42)
    assert s2._query.to_dict() == Query(Match(f=42)).to_dict()
    assert s._query.to_dict() is None

    s3 = s2.query("match", f=43)
    assert s2._query.to_dict() == Query(Match(f=42)).to_dict()
    assert ordered(s3._query.to_dict()) == ordered(
        Query(Bool(must=[Match(f=42), Match(f=43)])).to_dict())
示例#3
0
    def test_search_query_combines_query(self):
        s = Search()

        s2 = s.query("match", f=42)
        self.assertEqual(s2._query.to_dict(), Query(Match(f=42)).to_dict())
        self.assertEqual(s._query.to_dict(), None)

        s3 = s2.query("match", f=43)
        self.assertEqual(s2._query.to_dict(), Query(Match(f=42)).to_dict())
        self.assertEqual(
            ordered(s3._query.to_dict()),
            ordered(Query(Bool(must=[Match(f=42), Match(f=43)])).to_dict()),
        )
示例#4
0
    def test_filters(self):
        es_raw_response = {
            "buckets": {
                "my_first_filter": {"doc_count": 1},
                "my_second_filter": {"doc_count": 2},
            }
        }
        # test extract_buckets
        buckets_iterator = Filters("name", None).extract_buckets(es_raw_response)
        self.assertTrue(hasattr(buckets_iterator, "__iter__"))
        buckets = list(buckets_iterator)
        self.assertEqual(
            buckets,
            [
                # key -> bucket
                ("my_first_filter", {"doc_count": 1}),
                ("my_second_filter", {"doc_count": 2}),
            ],
        )

        # test extract bucket value
        self.assertEqual(
            Filters("name", None).extract_bucket_value({"doc_count": 1}), 1
        )
        self.assertEqual(
            Filters("name", None).extract_bucket_value({"doc_count": 2}), 2
        )

        # test get_filter
        filters_agg = Filters(
            name="some_agg",
            filters={
                "first_bucket": {"term": {"some_path": 1}},
                "second_bucket": {"term": {"some_path": 2}},
            },
            other_bucket=True,
            other_bucket_key="neither_one_nor_two",
        )
        self.assertEqual(
            filters_agg.get_filter("first_bucket"), {"term": {"some_path": 1}}
        )
        expected_others_filter = {
            "bool": {
                "must_not": [
                    {
                        "bool": {
                            "should": [
                                {"term": {"some_path": {"value": 1}}},
                                {"term": {"some_path": {"value": 2}}},
                            ]
                        }
                    }
                ]
            }
        }
        self.assertEqual(
            ordered(filters_agg.get_filter("_other_")), ordered(expected_others_filter)
        )
        self.assertEqual(
            ordered(filters_agg.get_filter("neither_one_nor_two")),
            ordered(expected_others_filter),
        )

        self.assertEqual(
            ordered(filters_agg.to_dict()),
            ordered(
                {
                    "filters": {
                        "filters": {
                            "first_bucket": {"term": {"some_path": 1}},
                            "second_bucket": {"term": {"some_path": 2}},
                        },
                        "other_bucket": True,
                        "other_bucket_key": "neither_one_nor_two",
                    }
                }
            ),
        )
示例#5
0
    def test_complex_example(self):
        s = (Search().query(
            "match", title="python").must_not("match", title="ruby").should(
                Query("term", category="meetup"),
                Query("term", category="conference")).post_filter(
                    "terms", tags=["prague", "czech"]).script_fields(
                        more_attendees="doc['attendees'].value + 42").aggs(
                            "per_country", "terms", field="country").aggs(
                                "avg_attendees", "avg",
                                field="attendees").bool(
                                    minimum_should_match=2).highlight_options(
                                        order="score").highlight(
                                            "title", "body", fragment_size=50))

        self.assertEqual(
            ordered({
                "aggs": {
                    "per_country": {
                        "aggs": {
                            "avg_attendees": {
                                "avg": {
                                    "field": "attendees"
                                }
                            }
                        },
                        "terms": {
                            "field": "country"
                        },
                    }
                },
                "highlight": {
                    "fields": {
                        "body": {
                            "fragment_size": 50
                        },
                        "title": {
                            "fragment_size": 50
                        },
                    },
                    "order": "score",
                },
                "post_filter": {
                    "terms": {
                        "tags": ["prague", "czech"]
                    }
                },
                "query": {
                    "bool": {
                        "minimum_should_match":
                        2,
                        "must": [{
                            "match": {
                                "title": {
                                    "query": "python"
                                }
                            }
                        }],
                        "must_not": [{
                            "match": {
                                "title": {
                                    "query": "ruby"
                                }
                            }
                        }],
                        "should": [
                            {
                                "term": {
                                    "category": {
                                        "value": "conference"
                                    }
                                }
                            },
                            {
                                "term": {
                                    "category": {
                                        "value": "meetup"
                                    }
                                }
                            },
                        ],
                    }
                },
                "script_fields": {
                    "more_attendees": {
                        "script": "doc['attendees'].value + 42"
                    }
                },
            }),
            ordered(s.to_dict()),
        )
示例#6
0
 def test_normalize_buckets(self):
     my_agg = Aggs(sample.EXPECTED_AGG_QUERY, mappings=MAPPINGS)
     response = Aggregations(data=sample.ES_AGG_RESPONSE,
                             _search=Search().aggs(my_agg)).to_normalized()
     self.assertEqual(ordered(response),
                      ordered(sample.EXPECTED_NORMALIZED_RESPONSE))