def test_create_queryset_with_multiple_sorting(): """ Create QuerySet with Multiple Sorting """ # When create a query block t = QuerySet("foobar") # And I add sorting s = Sort("_id", order="asc") t.order_by(s) ss = Sort("_id", order="desc") t.order_by(ss) # Then I see the appropriate JSON results = { "sort": [ { "_id": { "order": "asc" } }, { "_id": { "order": "desc" } } ], "query": { "match_all": {} } } 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_iterator(context): """ Search using an iterator """ # When create a queryset t = QuerySet("localhost", index="foo") # And set iterator fetching to a small size t._per_request = 2 # And there are records add_document("foo", {"bar": 0}) add_document("foo", {"bar": 1}) add_document("foo", {"bar": 2}) add_document("foo", {"bar": 3}) add_document("foo", {"bar": 4}) # And I do a filter on my new object # And a different filter on my old object t.order_by(Sort("bar", order="asc")) # Then I get the expected results for (counter, result) in enumerate(t): result['_source'].should.equal({"bar": counter}) len(t).should.equal(5) t.count().should.equal(5) t.max_score().should_not.be(None)
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_post_query_actions(context): """ Search with match_all query with post query actions """ # When create a queryset 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 have a post query action global my_global_var my_global_var = 1 def action(self, results, start, stop): global my_global_var my_global_var += 1 t.post_query_actions(action) # And I do a search t.order_by(Sort("bar", order="asc")) results = t[0:10] # Then I get a the expected results len(results).should.equal(3) results[0]["_source"]["bar"].should.equal(1) results[1]["_source"]["bar"].should.equal(2) results[2]["_source"]["bar"].should.equal(3) my_global_var.should.equal(2)
def test_create_queryset_with_scoring_min_score_track_score(): """ Create QuerySet with Scoring, Minimum Score and Track Scores """ # When create a query block t = QuerySet("foobar") # And I add scoring s = ScriptScore("foo = 0.0") t.score(s, min_score=0, track_scores=True) # Then I see the appropriate JSON results = { "min_score": 0, "track_scores": True, "query": { "function_score": { "functions": [ { "script_score": { "script": "foo = 0.0" } } ], "query": {"match_all": {}}, "boost_mode": "replace", "score_mode": "multiply" } } } homogeneous(t._query, results)
def test_create_queryset_with_scoring(): """ Create QuerySet with Scoring """ # When create a query block t = QuerySet("foobar") # And I add scoring s = ScriptScore("foo = 0.0") t.score(s) # Then I see the appropriate JSON results = { "query": { "function_score": { "functions": [ { "script_score": { "script": "foo = 0.0" } } ], "query": {"match_all": {}}, "boost_mode": "replace", "score_mode": "multiply" } } } homogeneous(t._query, results)
def test_queryset_iteration_with_no_results(): """ Fetch results with QuerySet via __iter__ with no results """ # When I create a query block t = QuerySet("foobar", index="bar") wrapper = lambda y: list(map(lambda x: x['_id'], y)) t.wrappers(wrapper) # And I have no records response = { "took": 12, "hits": { "total": 0, "max_score": 0, "hits": [] } } httpretty.register_uri(httpretty.GET, "http://foobar:9200/bar/_search", body=json.dumps(response), content_type="application/json") results = [] for result in t: results.append(result) len(results).should.equal(0) t.count().should.equal(0)
def test_queryset_max_score(): """ Get QuerySet Max Score """ # When I create a query block t = QuerySet("foobar") # Then I get an appropriate max score t.max_score().should.equal(None)
def test_queryset_count(): """ Get QuerySet Count """ # When I create a query block t = QuerySet("foobar") # Then I get an appropriate Count t.count().should.equal(None)
def test_queryset_getitem_with_post_query_action(): """ Fetch from QuerySet with __getitem__ and post query action """ # When I create a query block t = QuerySet("localhost", index="bar") # And I have a post query action global my_global_var my_global_var = 1 def action(self, results, start, stop): global my_global_var my_global_var += 1 t.post_query_actions(action) # And I have records response = { "took": 12, "timed_out": False, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 10, "hits": [ { "_index": "bar", "_type": "baz", "_id": "1", "_score": 10, "_source": { "foo": "bar" }, "sort": [ 1395687078000 ] } ] } } httpretty.register_uri(httpretty.GET, "http://localhost:9200/bar/_search", body=json.dumps(response), content_type="application/json") results = t[0:1] len(results).should.equal(1) t.count().should.equal(1) # Then I see the correct results results[0]['_id'].should.equal('1') my_global_var.should.equal(2)
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_host_dict(): """ Create a queryset with a host given as a dict """ # When create a queryset connection_info = {"host": "localhost", "port": 8080} t = QuerySet(connection_info, index="bar") # And I have records good_response = { "took": 1, "hits": { "total": 1, "max_score": 1, "hits": [{ "_index": "bar", "_type": "baz", "_id": "1", "_score": 10, "_source": { "foo": "bar" }, "sort": [1395687078000] }] } } bad_response = { "took": 1, "hits": { "total": 0, "max_score": None, "hits": [] } } httpretty.register_uri(httpretty.GET, "http://localhost:9200/bar/_search", body=json.dumps(bad_response), content_type="application/json") httpretty.register_uri(httpretty.GET, "http://localhost:8080/bar/_search", body=json.dumps(good_response), content_type="application/json") # When I run a query results = t[0:1] # Then I see the response. len(results).should.equal(1) results[0]["_source"]["foo"].should.equal("bar")
def test_create_queryset(): """ Create Default QuerySet """ # When create a queryset t = QuerySet("foobar") # Then I see the appropriate JSON results = { "query": {"match_all": {}} } homogeneous(t._query, results)
def test_simple_search(context): """ Search with match_all query """ # When create a queryset t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": "baz"}) # And I do a search results = t[0:1] # Then I get a the expected results len(results).should.equal(1) results[0]['_source'].should.equal({"bar": "baz"})
def test_copy_queryset(): """ Copy Queryset object when used as Model """ # When create a queryset t = QuerySet("http://foobar:9200") new_object = t.objects # 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 homogeneous(new_object._query, t._query)
def test_string_search(context): """ Search with string query """ # When create a queryset t = QuerySet("localhost", query="cheese", index="foo") # And there are records add_document("foo", {"bar": "banana"}) add_document("foo", {"bar": "cheese"}) # And I do a search results = t[0:10] # Then I get a the expected results len(results).should.equal(1) results[0]['_source'].should.equal({"bar": "cheese"})
def test_create_queryset_with_only_block(): """ Create QuerySet with Only block """ # When create a query block t = QuerySet("foobar") # And I add an 'only' block t.only("_id") # Then I see the appropriate JSON results = { "query": {"match_all": {}}, "fields": ["_id"] } homogeneous(t._query, results)
def test_queryset_string(): """ Create QuerySet with String query """ # When I create a query block t = QuerySet("foobar", query="foo") # Then I see the appropriate JSON results = { "query": { "query_string": { "query": "foo" } } } homogeneous(t._query, results)
def test_create_queryset_with_multiple_scoring(): """ Create QuerySet with Multiple Scoring """ # When create a query block t = QuerySet("foobar") # And I add scoring s = ScriptScore("foo = 0.0") t.score(s) # And I add more scoring boost = { "boost_factor": "3", "filter": Term("foo", "bar") } t.score(boost) # Then I see the appropriate JSON results = { "query": { "function_score": { "query": {"match_all": {}}, "functions": [ { "script_score": { "script": "foo = 0.0" } }, { "boost_factor": "3", "filter": { "term": { "foo": "bar" } } } ], "boost_mode": "replace", "score_mode": "multiply" } } } homogeneous(t._query, results)
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_create_queryset_with_query_string(): """ Create QuerySet with QueryString """ # When create a queryset q = QueryString("foo") t = QuerySet("foobar", query=q) # Then I see the appropriate JSON results = { "query": { "query_string": { "query": "foo" } } } homogeneous(t._query, results)
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_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_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_queryset_iteration(): """ Fetch results with QuerySet via __iter__ """ # When I create a query block t = QuerySet("foobar", index="bar") wrapper = lambda y: list(map(lambda x: x['_id'], y)) t.wrappers(wrapper) # And I have a record response = { "took": 12, "hits": { "total": 1, "max_score": 10, "hits": [ { "_index": "bar", "_type": "baz", "_id": "1", "_score": 10, "_source": { "foo": "bar" }, "sort": [ 1395687078000 ] } ] } } httpretty.register_uri(httpretty.GET, "http://foobar:9200/bar/_search", body=json.dumps(response), content_type="application/json") results = [] for result in t: results.append(result) len(results).should.equal(1) len(t).should.equal(1) t.count().should.equal(1)
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_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_queryset_getitem_multiple(): """ Fetch from QuerySet with __getitem__ multiple times """ # When I create a query block t = QuerySet("localhost", index="bar") wrapper = lambda y: list(map(lambda x: x['_id'], y)) t.wrappers(wrapper) # And I have a record response = { "took": 12, "hits": { "total": 1, "max_score": 10, "hits": [ { "_index": "bar", "_type": "baz", "_id": "1", "_score": 10, "_source": { "foo": "bar" }, "sort": [ 1395687078000 ] } ] } } httpretty.register_uri(httpretty.GET, "http://localhost:9200/bar/_search", body=json.dumps(response), content_type="application/json") results = t[0:1] len(results).should.equal(1) t.count().should.equal(1) results = t[0:1] len(results).should.equal(1) t.count().should.equal(1)