def test_A_fails_with_incorrect_dict(): correct_d = { 'terms': { 'field': 'tags' }, 'aggs': { 'per_author': { 'terms': { 'field': 'author.raw' } } }, } with raises(Exception): aggs.A(correct_d, field='f') d = correct_d.copy() del d['terms'] with raises(Exception): aggs.A(d) d = correct_d.copy() d['xx'] = {} with raises(Exception): aggs.A(d)
def test_A_fails_with_incorrect_dict(): correct_d = { "terms": { "field": "tags" }, "aggs": { "per_author": { "terms": { "field": "author.raw" } } }, } with raises(Exception): aggs.A(correct_d, field="f") d = correct_d.copy() del d["terms"] with raises(Exception): aggs.A(d) d = correct_d.copy() d["xx"] = {} with raises(Exception): aggs.A(d)
def test_A_from_dict(): d = { 'per_tag': { 'terms': { 'field': 'tags' }, 'aggs': { 'per_author': { 'terms': { 'field': 'author.raw' } } }, } } a = aggs.A(d) assert isinstance(a, aggs.Terms) assert a._params == { 'field': 'tags', 'aggs': { 'per_author': aggs.A('per_author', 'terms', field='author.raw') } } assert a._name == 'per_tag' assert a['per_author'] == aggs.A('per_author', 'terms', field='author.raw') assert a.aggs.per_author == aggs.A('per_author', 'terms', field='author.raw')
def test_meta_from_dict(): max_score = aggs.Max(field="score") a = aggs.A( "terms", field="tags", aggs={"max_score": max_score}, meta={"some": "metadata"} ) assert aggs.A(a.to_dict()) == a
def test_meta_from_dict(): max_score = aggs.Max(field='score') a = aggs.A('terms', field='tags', aggs={'max_score': max_score}, meta={'some': 'metadata'}) assert aggs.A(a.to_dict()) == a
def test_A_fails_with_agg_and_name_or_params(): a = aggs.A('per_tag', 'terms', field='tags') with raises(Exception): aggs.A(a, 'name') with raises(Exception): aggs.A(a, field='score')
def test_A_from_dict(): d = { "terms": {"field": "tags"}, "aggs": {"per_author": {"terms": {"field": "author.raw"}}}, } a = aggs.A(d) assert isinstance(a, aggs.Terms) assert a._params == { "field": "tags", "aggs": {"per_author": aggs.A("terms", field="author.raw")}, } assert a["per_author"] == aggs.A("terms", field="author.raw") assert a.aggs.per_author == aggs.A("terms", field="author.raw")
def test_filters_correctly_identifies_the_hash(): a = aggs.A( "filters", filters={ "group_a": { "term": { "group": "a" } }, "group_b": { "term": { "group": "b" } }, }, ) assert { "filters": { "filters": { "group_a": { "term": { "group": "a" } }, "group_b": { "term": { "group": "b" } }, } } } == a.to_dict() assert a.filters.group_a == query.Q("term", group="a")
def test_A_handles_nested_aggs_properly(): max_score = aggs.Max('max_score', field='score') a = aggs.A('per_tag', 'terms', field='tags', aggs={'max_score': max_score}) assert isinstance(a, aggs.Terms) assert a._params == {'field': 'tags', 'aggs': {'max_score': max_score}} assert a._name == 'per_tag'
def test_filters_correctly_identifies_the_hash(): a = aggs.A('filters', filters={ 'group_a': { 'term': { 'group': 'a' } }, 'group_b': { 'term': { 'group': 'b' } } }) assert { 'filters': { 'filters': { 'group_a': { 'term': { 'group': 'a' } }, 'group_b': { 'term': { 'group': 'b' } } } } } == a.to_dict() assert a.filters.group_a == filter.F('term', group='a')
def test_meta(): max_score = aggs.Max(field="score") a = aggs.A( "terms", field="tags", aggs={"max_score": max_score}, meta={"some": "metadata"} ) assert { "terms": {"field": "tags"}, "aggs": {"max_score": {"max": {"field": "score"}}}, "meta": {"some": "metadata"}, } == a.to_dict()
def test_meta(): max_score = aggs.Max(field='score') a = aggs.A('terms', field='tags', aggs={'max_score': max_score}, meta={'some': 'metadata'}) assert { 'terms': { 'field': 'tags' }, 'aggs': { 'max_score': { 'max': { 'field': 'score' } } }, 'meta': { 'some': 'metadata' } } == a.to_dict()
def filter_queryset(self, request, queryset, view): daily_seed = int(datetime.datetime.now().strftime('%Y%m%d')) # Map over the game categories to create a function score query for one # and dump it into a Bool should. game_query = query.Q( 'function_score', filter=es_filter.Bool( should=[es_filter.Term(tags=cat) for cat in GAME_CATEGORIES]), # Consistently random based on the day. functions=[SF('random_score', seed=daily_seed)], ) # Buckets by tag. Run a size=1 TopHits aggregation to only select one # game from each tag. Results will have to be pulled out of # S.execute().aggregations rather than S.execute().hits. top_hits = aggs.TopHits(size=1) a = aggs.A('terms', field='tags', aggs={'first_game': top_hits}) queryset = queryset.query(game_query)[0:4] queryset.aggs.bucket('top_hits', a) # Not chainable. return queryset
def test_A_passes_aggs_through(): a = aggs.A("terms", field="tags") assert aggs.A(a) is a
def test_A_handles_nested_aggs_properly(): max_score = aggs.Max(field="score") a = aggs.A("terms", field="tags", aggs={"max_score": max_score}) assert isinstance(a, aggs.Terms) assert a._params == {"field": "tags", "aggs": {"max_score": max_score}}
def test_A_creates_proper_agg(): a = aggs.A("terms", field="tags") assert isinstance(a, aggs.Terms) assert a._params == {"field": "tags"}
def test_A_fails_without_agg_type(): with raises(Exception): aggs.A('name', field='f')
def test_filter_can_be_instantiated_using_positional_args(): a = aggs.Filter(query.Q("term", f=42)) assert {"filter": {"term": {"f": 42}}} == a.to_dict() assert a == aggs.A("filter", query.Q("term", f=42))
def test_repr(): max_score = aggs.Max(field="score") a = aggs.A("terms", field="tags", aggs={"max_score": max_score}) assert "Terms(aggs={'max_score': Max(field='score')}, field='tags')" == repr( a)
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
def test_A_fails_with_agg_and_params(): a = aggs.A("terms", field="tags") with raises(Exception): aggs.A(a, field="score")
def test_A_fails_with_agg_and_params(): a = aggs.A('terms', field='tags') with raises(Exception): aggs.A(a, field='score')
def test_repr(): max_score = aggs.Max(field='score') a = aggs.A('terms', field='tags', aggs={'max_score': max_score}) assert "Terms(aggs={'max_score': Max(field='score')}, field='tags')" == repr( a)
def test_A_passes_aggs_through(): a = aggs.A('terms', field='tags') assert aggs.A(a) is a
def test_A_creates_proper_agg(): a = aggs.A('terms', field='tags') assert isinstance(a, aggs.Terms) assert a._params == {'field': 'tags'}
def test_filter_can_be_instantiated_using_positional_args(): a = aggs.Filter(filter.F('term', f=42)) assert {'filter': {'term': {'f': 42}}} == a.to_dict() assert a == aggs.A('filter', filter.F('term', f=42))
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