Exemplo n.º 1
0
def test_buckets_to_dict():
    a = aggs.Terms(field="tags")
    a.bucket("per_author", "terms", field="author.raw")

    assert {
        "terms": {
            "field": "tags"
        },
        "aggs": {
            "per_author": {
                "terms": {
                    "field": "author.raw"
                }
            }
        },
    } == a.to_dict()

    a = aggs.Terms(field="tags")
    a.metric("max_score", "max", field="score")

    assert {
        "terms": {
            "field": "tags"
        },
        "aggs": {
            "max_score": {
                "max": {
                    "field": "score"
                }
            }
        },
    } == a.to_dict()
Exemplo n.º 2
0
def test_buckets_to_dict():
    a = aggs.Terms(field='tags')
    a.bucket('per_author', 'terms', field='author.raw')

    assert {
        'terms': {
            'field': 'tags'
        },
        'aggs': {
            'per_author': {
                'terms': {
                    'field': 'author.raw'
                }
            }
        },
    } == a.to_dict()

    a = aggs.Terms(field='tags')
    a.metric('max_score', 'max', field='score')

    assert {
        'terms': {
            'field': 'tags'
        },
        'aggs': {
            'max_score': {
                'max': {
                    'field': 'score'
                }
            }
        },
    } == a.to_dict()
Exemplo n.º 3
0
def test_metric_inside_buckets():
    a = aggs.Terms(field="tags")
    b = a.metric("max_score", "max", field="score")

    # returns bucket so it's chainable
    assert a is b
    assert a.aggs["max_score"] == aggs.Max(field="score")
Exemplo n.º 4
0
def test_buckets_are_nestable():
    a = aggs.Terms(field="tags")
    b = a.bucket("per_author", "terms", field="author.raw")

    assert isinstance(b, aggs.Terms)
    assert b._params == {"field": "author.raw"}
    assert a.aggs == {"per_author": b}
Exemplo n.º 5
0
def test_buckets_are_nestable():
    a = aggs.Terms(field='tags')
    b = a.bucket('per_author', 'terms', field='author.raw')

    assert isinstance(b, aggs.Terms)
    assert b._params == {'field': 'author.raw'}
    assert a.aggs == {'per_author': b}
Exemplo n.º 6
0
def test_aggregate_inside_buckets():
    a = aggs.Terms('per_tag', field='tags')
    b = a.aggregate('max_score', 'max', field='score')

    # returns bucket so it's chainable
    assert a is b
    assert a.aggs['max_score'] == aggs.Max('max_score', field='score')
Exemplo n.º 7
0
def test_metric_inside_buckets():
    a = aggs.Terms(field='tags')
    b = a.metric('max_score', 'max', field='score')

    # returns bucket so it's chainable
    assert a is b
    assert a.aggs['max_score'] == aggs.Max(field='score')
Exemplo n.º 8
0
def test_filter_aggregation_as_nested_agg():
    a = aggs.Terms(field="tags")
    a.bucket("filtered", "filter", query.Q("term", f=42))

    assert {
        "terms": {"field": "tags"},
        "aggs": {"filtered": {"filter": {"term": {"f": 42}}}},
    } == a.to_dict()
Exemplo n.º 9
0
 def _add_agg_terms(cls, search: Search, size: int,
                    field: Sequence[str]) -> Search:
     search = copy(search)
     a = search.aggs
     for i in range(len(field) - 1):
         a = a.bucket(field[i].replace(".", "__"),
                      aggs.Nested(path=".".join(field[:i + 1])))
     a.bucket(field[-1].replace(".", "__"),
              aggs.Terms(field=".".join(field), size=size))
     return search
Exemplo n.º 10
0
def test_filter_aggregation_as_nested_agg():
    a = aggs.Terms(field='tags')
    a.bucket('filtered', 'filter', query.Q('term', f=42))

    assert {
        'terms': {
            'field': 'tags'
        },
        'aggs': {
            'filtered': {
                'filter': {
                    'term': {
                        'f': 42
                    }
                },
            }
        }
    } == a.to_dict()
Exemplo n.º 11
0
 def add_agg_text_tokens_date_histogram_terms(
     self,
     search: Search,
     calendar_interval: str,
     size: int,
     include: object,
 ) -> Search:
     search = copy(search)
     a = search.aggs.bucket(
         self._date_field.replace(".", "__"),
         aggs.DateHistogram(field=self._date_field,
                            calendar_interval=calendar_interval),
     )
     if size:
         a.bucket(
             self._text_tokens_field.replace(".", "__"),
             aggs.Terms(
                 field=self._text_tokens_field,
                 size=size,
                 include=include,
             ),
         )
     return search
Exemplo n.º 12
0
def test_buckets_equals_counts_subaggs():
    a = aggs.Terms(field="tags")
    a.bucket("per_author", "terms", field="author.raw")
    b = aggs.Terms(field="tags")

    assert a != b
Exemplo n.º 13
0
def test_nested_buckets_are_settable_as_getitem():
    a = aggs.Terms(field='tags')
    b = a['per_author'] = aggs.A('terms', field='author.raw')

    assert a.aggs['per_author'] is b
Exemplo n.º 14
0
 def count(self, search):
     search.aggs.bucket(self._top_bucket_name, aggs.Nested(path=self.name)) \
         .bucket(self._terms_bucket_name,
                 aggs.Terms(field='{}.id'.format(self.name))) \
         .bucket(self._top_hit_bucket_name, aggs.TopHits(size=1, _source={'includes': [self.name]}))
Exemplo n.º 15
0
def test_buckets_equals_counts_subaggs():
    a = aggs.Terms(field='tags')
    a.bucket('per_author', 'terms', field='author.raw')
    b = aggs.Terms(field='tags')

    assert a != b
Exemplo n.º 16
0
def test_nested_buckets_are_reachable_as_getitem():
    a = aggs.Terms(field="tags")
    b = a.bucket("per_author", "terms", field="author.raw")

    assert a["per_author"] is not b
    assert a["per_author"] == b
Exemplo n.º 17
0
def test_nested_buckets_are_settable_as_getitem():
    a = aggs.Terms(field="tags")
    b = a["per_author"] = aggs.A("terms", field="author.raw")

    assert a.aggs["per_author"] is b
Exemplo n.º 18
0
def test_nested_buckets_are_reachable_as_getitem():
    a = aggs.Terms(field='tags')
    b = a.bucket('per_author', 'terms', field='author.raw')

    assert a['per_author'] is not b
    assert a['per_author'] == b