def test_search_histogram_aggregations_with_min_doc_count(context): """ Search with aggregations that have histograms with min_doc_count """ # When create a query block t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": 0, "foo": "baz"}) add_document("foo", {"bar": 0, "foo": "baz"}) add_document("foo", {"bar": 2}) add_document("foo", {"bar": 3, "foo": "bazbaz"}) add_document("foo", {"bar": 5, "foo": "foobar"}) add_document("foo", {"bar": 5}) add_document("foo", {"bar": 5, "foo": "foobaz"}) add_document("foo", {"bar": 9}) # When I do a histogram aggregation t.aggregate(aggregation=Aggregations("bar_buckets", "bar", "metric", histogram_interval=2, min_doc_count=2)) t[0:10] # I get the expected results t.aggregations().should.have.key("bar_buckets") t.aggregations()["bar_buckets"].should.have.key("buckets").being.equal([ {u'key': 4, u'doc_count': 3}, {u'key': 2, u'doc_count': 2}, {u'key': 0, u'doc_count': 2}])
def test_search_terms_aggregation_with_order_two(context): """ Search with terms aggregation ordered by count ascending """ # When create a queryset t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": "baz"}) add_document("foo", {"bar": "baz"}) add_document("foo", {"bar": "baz"}) add_document("foo", {"bar": "bazbaz"}) add_document("foo", {"bar": "bazbaz"}) add_document("foo", {"bar": "bazbar"}) # And I do an aggregated search t.aggregate(aggregation=Aggregations("foo_attrs", "bar", "terms", order_type="_count", order_dir="asc")) t[0:10] # Then I get a the expected results t.aggregations().should.have.key('foo_attrs') t.aggregations()['foo_attrs'].should.have.key("buckets").being.equal([ {u'key': u'bazbar', u'doc_count': 1}, {u'key': u'bazbaz', u'doc_count': 2}, {u'key': u'baz', u'doc_count': 3}])
def test_search_filtered_aggregations(context): """ Search with filtered aggregations """ # When create a query block t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": 0, "foo": "baz"}) add_document("foo", {"bar": 0, "foo": "baz"}) add_document("foo", {"bar": 2}) add_document("foo", {"bar": 3, "foo": "bazbaz"}) add_document("foo", {"bar": 5, "foo": "foobar"}) add_document("foo", {"bar": 5}) add_document("foo", {"bar": 5, "foo": "foobaz"}) add_document("foo", {"bar": 9}) # And I do a filtered f = Filter().filter(Range("bar", gte=5)) t.aggregate(aggregation=Aggregations("missing_foo", "foo", "missing", filter_val=f, filter_name="high_bar")) t[0:10] # Then I get the expected results t.aggregations().should.have.key("high_bar") t.aggregations()["high_bar"].should.have.key("missing_foo").being.equal( {u'doc_count': 2}) t.aggregations()["high_bar"]["doc_count"].should.equal(4)
def test_search_global_aggregations(context): """ Search with global aggregations """ # With a specific query # q = QueryBuilder(query_string=QueryString(query={"match": {"foo_attr": "yes"}})) # When create a query block t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": "baz", "foo_attr": "yes"}) add_document("foo", {"bar": "bazbaz", "foo_attr": "no"}) add_document("foo", {"bar": "bazbaz"}) # And I do a global t.aggregate(aggregation=Aggregations("foo_attrs", "bar", "terms", global_name="all_bar")) t[0:10] # I get the expected results t.aggregations().should.have.key("all_bar").being.equal({ u'foo_attrs': {u'buckets': [ {u'key': u'bazbaz', u'doc_count': 2}, {u'key': u'baz', u'doc_count': 1} ]}, u'doc_count': 3})
def test_search_nested_terms_aggregations_with_size(context): """ Search with nested terms aggregation and a specified size """ # When create a query block t = QuerySet("localhost", index="foo") # And there are nested records add_document("foo", {"child": [{"stuff": "yep", "bazbaz": "type0"}], "foo": "foo"}) add_document("foo", {"child": [{"stuff": "yep", "bazbaz": "type0"}], "foo": "foo"}) add_document("foo", {"child": [{"stuff": "nope", "bazbaz": "type1"}], "foo": "foofoo"}) add_document("foo", {"child": [{"stuff": "nope", "bazbaz": "type2"}], "foo": "foofoo"}) # And I do a nested t.aggregate(aggregation=Aggregations("baz_types", "bazbaz", "terms", nested_path="child", size=1)) t[0:10] # The I get the expected results t.aggregations().should.have.key("child").being.equal({ u'baz_types': { u'buckets': [{u'doc_count': 2, u'key': u'type0'}] }, u'doc_count': 4 })
def test_search_nested_aggregations(context): """ Search with nested aggregations """ # When create a query block t = QuerySet("localhost", index="foo") # And there are nested records add_document("foo", {"child": [{"stuff": "yep", "bazbaz": 10}], "foo": "foo"}) add_document("foo", {"child": [{"stuff": "nope", "bazbaz": 1}], "foo": "foofoo"}) # And I do a nested t.aggregate(aggregation=Aggregations("best_bazbaz", "bazbaz", "max", nested_path="child")) t[0:10] # The I get the expected results t.aggregations().should.have.key("child").being.equal({'best_bazbaz': {'value': 10.0}, 'doc_count': 2})
def test_search_terms_aggregation_with_size(context): """ Search with terms aggregation w/ specified size """ # When create a queryset t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": "baz"}) add_document("foo", {"bar": "baz"}) add_document("foo", {"bar": "bazbaz"}) add_document("foo", {"bar": "bazbar"}) # And I do an aggregated search t.aggregate(aggregation=Aggregations("foo_attrs", "bar", "terms", size=1)) t[0:10] # Then I get a the expected results t.aggregations().should.have.key('foo_attrs') t.aggregations()['foo_attrs'].should.have.key("buckets").being.equal([ {u'key': u'baz', u'doc_count': 2}])
def test_queryset_aggregations(): """ Fetch aggregation data from QuerySet with #aggregations """ # When I create a query block t = QuerySet("localhost", index="bar") # And I aggregate something a = Aggregations("agg_name", "field_name", "metric") t.aggregate(a) # And I have records response = { "took": 12, "hits": { "total": 1, "max_score": 10, "hits": [ {"_index": "bar", "_type": "baz", "_id": "1", "_score": 10, "_source": { "foo": "bar" }, "sort": [ 1395687078000 ]} ] }, "aggregations": { "agg_name": {"metric": {"field": "field_name"}} } } httpretty.register_uri(httpretty.GET, "http://localhost:9200/bar/_search", body=json.dumps(response), content_type="application/json") t[0:1] t.aggregations().should.have.key("agg_name").being.equal({"metric": {"field": "field_name"}})
def test_create_queryset_with_aggregation(): """ Create QuerySet with an Aggregation """ # When I create a query block t = QuerySet("foobar") # And I aggregate a = Aggregations("agg_name", "field_name", "metric") t.aggregate(a) results = { "query": { "match_all": {} }, "aggregations": { "agg_name": {"metric": {"field": "field_name"}} } } homogeneous(t._query, results) # And I can do it as many times as I want a1 = Aggregations("other_agg_name", "other_field_name", "terms", size=1) t.aggregate(a1) results = { "query": { "match_all": {} }, "aggregations": { "agg_name": {"metric": {"field": "field_name"}}, "other_agg_name": {"terms": { "field": "other_field_name", "order": {"_count": "desc"}, "min_doc_count": 1, "size": 1 }} } } homogeneous(t._query, results)
def test_search_range_aggregations(context): """ Search with aggregations for ranges """ # When create a query block t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": 1, "foo": "foo"}) add_document("foo", {"bar": 2, "foo": "foo"}) add_document("foo", {"bar": 2}) # When I do a ranged aggregation range_list = [0, 1, 2, 3] t.aggregate(aggregation=Aggregations("bar_types", "bar", "metric", range_list=range_list)) t[0:10] # I get the expected results t.aggregations().should.have.key("bar_ranges") t.aggregations()["bar_ranges"].should.have.key("buckets").being.equal([ {u'to': 0.0, u'doc_count': 0}, {u'to': 1.0, u'from': 0.0, u'doc_count': 0}, {u'to': 2.0, u'from': 1.0, u'doc_count': 1}, {u'to': 3.0, u'from': 2.0, u'doc_count': 2}, {u'from': 3.0, u'doc_count': 0}]) # And I should be able to name it if I want t.aggregate(aggregation=Aggregations("bar_types", "bar", "metric", range_list=range_list, range_name="my_unique_range")) t[0:10] # I get the expected results t.aggregations().should.have.key("my_unique_range") t.aggregations()["my_unique_range"].should.have.key("buckets").being.equal([ {u'to': 0.0, u'doc_count': 0}, {u'to': 1.0, u'from': 0.0, u'doc_count': 0}, {u'to': 2.0, u'from': 1.0, u'doc_count': 1}, {u'to': 3.0, u'from': 2.0, u'doc_count': 2}, {u'from': 3.0, u'doc_count': 0}])
def test_search_multi_aggregations(context): """ Search with multiple aggregations """ # When create a query block t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": "baz", "foo": "foo"}) add_document("foo", {"bar": "bazbaz", "foo": "foo"}) add_document("foo", {"bar": "bazbaz"}) # And I do an aggregated search on two dimensions t.aggregate(aggregation=Aggregations("bar_attrs", "bar", "terms")) t.aggregate(aggregation=Aggregations("missing_foo", "foo", "missing")) t[0:10] # Then I get a the expected results t.aggregations().should.have.key("missing_foo").being.equal({u'doc_count': 1}) t.aggregations().should.have.key("bar_attrs") t.aggregations()['bar_attrs'].should.have.key("buckets").being.equal([ {u'key': u'bazbaz', u'doc_count': 2}, {u'key': u'baz', u'doc_count': 1}])