def test_create_queryset_with_filters(): """ Create QuerySet with Multiple Filters """ # When create a query block t = QuerySet("foobar") # And I add a filter t.filter(Term("foo", "bar")) t.filter(Term("foobar", "foobar")) # Then I see the appropriate JSON results = { "query": { "filtered": { "query": {"match_all": {}}, "filter": { "and": [ { "term": { "foo": "bar" } }, { "term": { "foobar": "foobar" } } ] } } } } homogeneous(t._query, results)
def test_create_queryset_with_filters(): """ Create QuerySet with Multiple Filters """ # When create a query block t = QuerySet("foobar") # And I add a filter t.filter(Term("foo", "bar")) t.filter(Term("foobar", "foobar")) # Then I see the appropriate JSON results = { "query": { "filtered": { "query": {"match_all": {}}, "filter": { "and": [ { "term": { "foo": "bar" } }, { "term": { "foobar": "foobar" } } ] } } } } homogeneous(t._query, results)
def test_search_with_filter_and_scoring_and_sorting_and_fields(context): """ Search with match_all query, filter, scoring, sorting, and fields """ # When create a queryset t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": "baz", "scoring_field": 0, "sorting_field": 30}) add_document("foo", {"bar": "baz", "scoring_field": 1, "sorting_field": 20}) add_document("foo", {"bar": "baz", "scoring_field": 2, "sorting_field": 10}) add_document("foo", {"bar": "bazbaz", "scoring_field": 3, "sorting_field": 0}) # And I do a search t.filter(Term("bar", "baz")) score = ScriptScore("final_score = 0 + doc['scoring_field'].value;") t.score(score) sorting = Sort("sorting_field", order="desc") t.order_by(sorting) t.only(["bar"]) results = t[0:10] # Then I get a the expected results len(results).should.equal(3) results[0]['fields'].should.equal({"bar": ["baz"]}) results[1]['fields'].should.equal({"bar": ["baz"]}) results[2]['fields'].should.equal({"bar": ["baz"]})
def test_search_with_filter_and_scoring_and_sorting_and_fields(context): """ Search with match_all query, filter, scoring, sorting, and fields """ # When create a queryset t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": "baz", "scoring_field": 0, "sorting_field": 30}) add_document("foo", {"bar": "baz", "scoring_field": 1, "sorting_field": 20}) add_document("foo", {"bar": "baz", "scoring_field": 2, "sorting_field": 10}) add_document("foo", {"bar": "bazbaz", "scoring_field": 3, "sorting_field": 0}) # And I do a search t.filter(Term("bar", "baz")) score = ScriptScore("final_score = 0 + doc['scoring_field'].value;") t.score(score) sorting = Sort("sorting_field", order="desc") t.order_by(sorting) t.only(["bar"]) results = t[0:10] # Then I get a the expected results len(results).should.equal(3) results[0]['fields'].should.equal({"bar": ["baz"]}) results[1]['fields'].should.equal({"bar": ["baz"]}) results[2]['fields'].should.equal({"bar": ["baz"]})
def test_create_queryset_with_filters_and_scoring(): """ Create QuerySet with Scoring and Multiple Filters """ # When create a query block t = QuerySet("foobar") # And I add filtering t.filter(Term("foo", "bar")) # And I add scoring s = ScriptScore("foo = 0.0") t.score(s) # And I add a second filter t.filter(Term("foobar", "foobar")) # Then I see the appropriate JSON results = { "query": { "function_score": { "query": { "filtered": { "query": {"match_all": {}}, "filter": { "and": [ { "term": { "foo": "bar" } }, { "term": { "foobar": "foobar" } } ] } } }, "functions": [ { "script_score": { "script": "foo = 0.0" } } ], "boost_mode": "replace", "score_mode": "multiply" } } } homogeneous(t._query, results)
def test_create_queryset_with_filters_and_scoring(): """ Create QuerySet with Scoring and Multiple Filters """ # When create a query block t = QuerySet("foobar") # And I add filtering t.filter(Term("foo", "bar")) # And I add scoring s = ScriptScore("foo = 0.0") t.score(s) # And I add a second filter t.filter(Term("foobar", "foobar")) # Then I see the appropriate JSON results = { "query": { "function_score": { "query": { "filtered": { "query": {"match_all": {}}, "filter": { "and": [ { "term": { "foo": "bar" } }, { "term": { "foobar": "foobar" } } ] } } }, "functions": [ { "script_score": { "script": "foo = 0.0" } } ], "boost_mode": "replace", "score_mode": "multiply" } } } homogeneous(t._query, results)
def test_geo_distance_search_array(context): """ Search with geo distance filter with array """ # When create a queryset t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"location": {"lat": 1.1, "lon": 2.1}}) add_document("foo", {"location": {"lat": 40.1, "lon": 80.1}}) # And I filter for terms t.filter(GeoDistance([2.0, 1.0], "20mi")) results = t[0:10] # Then I get a the expected results len(results).should.equal(1)
def test_geo_distance_search_with_field_name(context): """ Search with geo distance filter with field_name """ # When create a queryset t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"foo_loc": {"lat": 1.1, "lon": 2.1}}) add_document("foo", {"foo_loc": {"lat": 40.1, "lon": 80.1}}) # And I filter for distance t.filter(GeoDistance({"lat": 1.0, "lon": 2.0}, "20mi", field_name="foo_loc")) results = t[0:10] # Then I get a the expected results len(results).should.equal(1)
def test_terms_search_with_execution(context): """ Search with terms filter with execution """ # When create a queryset t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"foo": ["foo", "bar"]}) add_document("foo", {"foo": ["foo", "baz"]}) # And I filter for terms t.filter(Terms("foo", ["foo", "bar"], execution="and")) results = t[0:10] # Then I get a the expected results len(results).should.equal(1) results[0]["_source"]["foo"].should.equal(["foo", "bar"])
def test_search_with_missing_existence_null_value(context): """ Search with missing via non-existence or a null value """ # When create a query block t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": 1}) add_document("foo", {"baz": 2}) add_document("foo", {"baz": 3, "bar": None}) # And I add filters t.filter(Missing("bar", existence=True, null_value=True)) results = t[0:10] # Then my results are filtered correctly len(results).should.equal(2)
def test_simple_search_with_filter(context): """ Search with filter """ # When create a queryset t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": "baz"}) add_document("foo", {"bar": "bazbaz"}) # And I do a filtered search t.filter(Term("bar", "baz")) results = t[0:10] # Then I get a the expected results len(results).should.equal(1) results[0]['_source'].should.equal({"bar": "baz"})
def test_search_with_filter_block(context): """ Search with Filter Block """ # 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", "foo": "foofoo"}) # And I do a filtered search f = Filter("or").filter(Term("bar", "baz")).filter(Term("foo", "foo")) t.filter(f) results = t[0:10] # Then I get the appropriate response len(results).should.equal(2)
def test_match_all_search(context): """ Search with match all filter """ # When create a queryset 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": "baz", "foo": "foofoo"}) add_document("foo", {"bar": "baz", "foo": "foofoofoo"}) # And I filter match_all t.filter(MatchAll()) results = t[0:10] # Then I get a the expected results len(results).should.equal(4)
def test_search_with_exists(context): """ Search with exists filter """ # When create a query block t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": 1}) add_document("foo", {"baz": 1}) # And I add an exists filter exists = Exists("baz") t.filter(exists) results = t[0:10] # Then my results only have that field len(results).should.equal(1) results[0]["_source"]["baz"].should.equal(1)
def test_search_with_missing(context): """ Search with missing """ # When create a query block t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": 1}) add_document("foo", {"baz": 2}) add_document("foo", {"bar": 3}) # And I add filters t.filter(Missing("bar")) results = t[0:10] # Then my results are filtered correctly len(results).should.equal(1) results[0]["_source"]["baz"].should.equal(2)
def test_terms_search(context): """ Search with terms filter """ # When create a queryset 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": "baz", "foo": "foofoo"}) add_document("foo", {"bar": "baz", "foo": "foofoofoo"}) # And I filter for terms t.filter(Terms("foo", ["foo", "foofoo"])) results = t[0:10] # Then I get a the expected results len(results).should.equal(3)
def test_create_queryset_with_scoring_and_filtering_from_object(): """ Create QuerySet with Scoring and Filter Object """ # When create a query block t = QuerySet("foobar") # And I add scoring s = ScriptScore("foo = 0.0") t.score(s) # And I add filtering f = Filter("and").filter(Term("foo", "bar")) t.filter(f) # Then I see the appropriate JSON results = { "query": { "function_score": { "query": { "filtered": { "query": {"match_all": {}}, "filter": { "and": [ { "term": { "foo": "bar" } } ] } } }, "script_score": { "script": "foo = 0.0" }, "boost_mode": "replace" } } } homogeneous(t._query, results)
def test_search_with_type(context): """ Search with type filter """ # When create a query block t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": 1}) add_document("foo", {"bar": 2}) add_document("foo", {"bar": 3}, doc_type="bar") # And I add a type filter _type = Type("bar") t.filter(_type) results = t[0:10] # Then my results only have that type len(results).should.equal(1) results[0]["_source"]["bar"].should.equal(3)
def test_search_with_gt_range(context): """ Search with gt range filter """ # When create a query block t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": 1}) add_document("foo", {"bar": 2}) add_document("foo", {"bar": 3}) # And I add a range filter _type = Range("bar", gt=2) t.filter(_type) results = t[0:10] # Then my results only have that type len(results).should.equal(1) results[0]["_source"]["bar"].should.equal(3)
def test_search_with_multiple_filters(context): """ Search with multiple filters """ # 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", "foo": "foofoo"}) # And I do a filtered search t.filter(Term("bar", "bazbaz")) t.filter(Term("foo", "foo")) results = t[0:10] # Then I get the appropriate response len(results).should.equal(1) results[0]['_source'].should.equal({"bar": "bazbaz", "foo": "foo"})
def test_search_with_filter_and_scoring(context): """ Search with match_all query, filter and scoring """ # When create a queryset t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": "baz", "scoring_field": 1}) add_document("foo", {"bar": "baz", "scoring_field": 2}) add_document("foo", {"bar": "bazbaz", "scoring_field": 3}) # And I do a search t.filter(Term("bar", "baz")) score = ScriptScore("final_score = 0 + doc['scoring_field'].value;") t.score(score) results = t[0:10] # Then I get a the expected results len(results).should.equal(2) results[0]['_source'].should.equal({"bar": "baz", "scoring_field": 2}) results[1]['_source'].should.equal({"bar": "baz", "scoring_field": 1})
def test_search_with_filter_and_scoring(context): """ Search with match_all query, filter and scoring """ # When create a queryset t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": "baz", "scoring_field": 1}) add_document("foo", {"bar": "baz", "scoring_field": 2}) add_document("foo", {"bar": "bazbaz", "scoring_field": 3}) # And I do a search t.filter(Term("bar", "baz")) score = ScriptScore("final_score = 0 + doc['scoring_field'].value;") t.score(score) results = t[0:10] # Then I get a the expected results len(results).should.equal(2) results[0]['_source'].should.equal({"bar": "baz", "scoring_field": 2}) results[1]['_source'].should.equal({"bar": "baz", "scoring_field": 1})
def test_search_as_queryset_with_filter(context): """ Search with match_all query and filter on a cloned queryset """ # When create a queryset t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": "baz"}) add_document("foo", {"bar": "bazbaz"}) # And I do a filter on my new object my_search = t.objects.filter(Term("bar", "baz")) # And a different filter on my old object t.filter(Term("bar", "bazbaz")) # And I do a search results = my_search[0:10] # Then I get a the expected results len(results).should.equal(1) results[0]['_source'].should.equal({"bar": "baz"})
def test_search_as_queryset_with_filter(context): """ Search with match_all query and filter on a cloned queryset """ # When create a queryset t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": "baz"}) add_document("foo", {"bar": "bazbaz"}) # And I do a filter on my new object my_search = t.objects.filter(Term("bar", "baz")) # And a different filter on my old object t.filter(Term("bar", "bazbaz")) # And I do a search results = my_search[0:10] # Then I get a the expected results len(results).should.equal(1) results[0]['_source'].should.equal({"bar": "baz"})
def test_copy_queryset_with_filters(): """ Copy Queryset object and ensure distinct filters """ # When create a queryset t = QuerySet("http://foobar:9200") # With filters t.filter(Term("foo", "bar")) # And I clone the queryset new_object = t.objects # And add new filters new_object.filter(Term("bar", "baz")) # Then the new object is not the same object as the queryset assert(new_object is not t) # And is not the same query object assert(new_object._query is not t._query) # But it is has the same properties heterogeneous(new_object._query, t._query)
def test_copy_queryset_with_filters(): """ Copy Queryset object and ensure distinct filters """ # When create a queryset t = QuerySet("http://foobar:9200") # With filters t.filter(Term("foo", "bar")) # And I clone the queryset new_object = t.objects # And add new filters new_object.filter(Term("bar", "baz")) # Then the new object is not the same object as the queryset assert (new_object is not t) # And is not the same query object assert (new_object._query is not t._query) # But it is has the same properties heterogeneous(new_object._query, t._query)