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_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_scoring(): """ 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_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 ss = ScriptScore("foo = 1.0") t.score(ss) # Then I see the appropriate JSON results = { "query": { "function_score": { "query": {"match_all": {}}, "script_score": { "script": "foo = 1.0" }, "boost_mode": "replace" } } } homogeneous(t._query, results)
def test_add_agg_histogram_with_order(): """ Create an aggregations block w/ histogram intervals and order type/direction """ # Whan add an agg block w/ interval t = Aggregations("agg_name", "field_name", "metric", histogram_interval=20, order_type="_count", order_dir="asc") # Then I see correct json results = { "agg_name": { "histogram": { "field": "field_name", "interval": 20, "order": { "_count": "asc" }, "min_doc_count": 1 } } } homogeneous(t, 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_add_agg_global(): """ Create an aggregations block that is global """ # When add a global agg block t = Aggregations("agg_name", "field_name", "metric", global_name="global_agg") # Then I see correct json results = { "global_agg": { "global": {}, "aggregations": { "agg_name": { "metric": { "field": "field_name" } } } } } homogeneous(t, results)
def test_add_agg_range(): """ Create an aggregations block for a range """ # When add an agg block w/ range range_list = [1, 2, 3] t = Aggregations("agg_name", "field_name", "metric", range_list=range_list, range_name="my_ranges") # Then I see the correct json results = { "my_ranges": { "range": { "field": "field_name", "ranges": [{ "to": 1 }, { "from": 1, "to": 2 }, { "from": 2, "to": 3 }, { "from": 3 }] } } } homogeneous(t, results) # Also works without a given range_name t = Aggregations("agg_name", "field_name", "metric", range_list=range_list) # Then I see the correct json results = { "field_name_ranges": { "range": { "field": "field_name", "ranges": [{ "to": 1 }, { "from": 1, "to": 2 }, { "from": 2, "to": 3 }, { "from": 3 }] } } } homogeneous(t, results)
def test_add_agg_nested_with_min_doc_count(): """ Create nested aggregations block specifying min_doc_count """ # When add a nested_path with terms agg block w/ size t = Aggregations("agg_name", "field_name", "terms", min_doc_count=10, nested_path="nested_doc") # The I see correct json results = { "nested_doc": { "nested": { "path": "nested_doc" }, "aggregations": { "agg_name": { "terms": { "field": "nested_doc.field_name", "order": { "_count": "desc" }, "min_doc_count": 10, "size": 0 } } } } } homogeneous(t, results)
def test_add_agg_filtered(): """ Create an aggregations block with filter """ # With a filter filter_value = {"filter_type": {"other_field": {"comparison": "value"}}} # When add a filtered agg block t = Aggregations("agg_name", "field_name", "metric", filter_val=filter_value, filter_name="filter_on_other") # Then I see correct json results = { "filter_on_other": { "filter": filter_value, "aggregations": { "agg_name": { "metric": { "field": "field_name" } } } } } homogeneous(t, results)
def test_add_agg_with_order(): """ Create aggregations block specifying order type and direction """ # When add a terms agg block w/ size t = Aggregations("agg_name", "field_name", "terms", order_type="_term", order_dir="asc") # Then I see correct json results = { "agg_name": { "terms": { "field": "field_name", "order": { "_term": "asc" }, "min_doc_count": 1, "size": 0 } } } homogeneous(t, results)
def test_add_agg_nested(): """ Create nested aggregations block """ # When add a nested_path with agg block t = Aggregations("agg_name", "field_name", "metric", nested_path="nested_doc") # The I see correct json results = { "nested_doc": { "nested": { "path": "nested_doc" }, "aggregations": { "agg_name": { "metric": { "field": "nested_doc.field_name" } }, } } } homogeneous(t, results)
def test_add_agg_histogram_with_min_doc_count(): """ Create an aggregations block w/ histogram intervals and min_doc_count """ # Whan add an agg block w/ interval t = Aggregations("agg_name", "field_name", "metric", histogram_interval=20, min_doc_count=10) # Then I see correct json results = { "agg_name": { "histogram": { "field": "field_name", "interval": 20, "order": { "_key": "desc" }, "min_doc_count": 10 } } } homogeneous(t, 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_add_match_all(): """ Create Match All Block """ # When add a match all filter t = MatchAll() # Then I see the appropriate JSON results = {"match_all": {}} homogeneous(t, results)
def test_add_score_with_params_and_lang(): """ Create Score Block with Params and Language """ # When add a Score field t = ScriptScore("foo", params={"bar": "baz"}, lang="mvel") # Then I see the appropriate JSON results = {"script": "foo", "params": {"bar": "baz"}, "lang": "mvel"} homogeneous(t, results)
def test_exists(): """ Create Exists Block """ # When add an Exists Block t = Exists("foo") # Then I see the appropriate JSON results = {"exists": {"field": "foo"}} homogeneous(t, results)
def test_add_type(): """ Create Type Block """ # When add a Type Block t = Type("foo") # Then I see the appropriate JSON results = {"type": {"value": "foo"}} homogeneous(t, results)
def test_add_score(): """ Create Score Block """ # When add a Score field t = ScriptScore("foo") # Then I see the appropriate JSON results = {"script": "foo"} homogeneous(t, results)
def test_filter_with_or(): """ Create OR Filter """ # When create a filter block t = Filter("or") # Then I see the appropriate JSON results = {"or": []} homogeneous(t, results)
def test_add_agg(): """ Create aggregations block """ # When add an agg block t = Aggregations("agg_name", "field_name", "metric") # Then I see correct json results = {"agg_name": {"metric": {"field": "field_name"}}} homogeneous(t, results)
def test_create_filter(): """ Create Default Filter """ # When create a filter block t = Filter() # Then I see the appropriate JSON results = {"and": []} homogeneous(t, results)
def test_filter_with_and(): """ Create AND Filter """ # When create a filter block t = Filter("and") # Then I see the appropriate JSON results = {"and": []} homogeneous(t, results)
def test_add_score_with_params(): """ Create Score Block with Params """ # When add a Score field t = ScriptScore("foo", params={"bar": "baz"}) # Then I see the appropriate JSON results = {"script": "foo", "params": {"bar": "baz"}} homogeneous(t, results)
def test_add_score_with_lang(): """ Create Score Block with Language """ # When add a Score field t = ScriptScore("foo", lang="mvel") # Then I see the appropriate JSON results = {"script": "foo", "lang": "mvel"} homogeneous(t, results)
def test_create_bool(): """ Create Bool Block """ # When I create a Bool block t = Bool() # Then I see the appropriate JSON results = { "bool": {} } homogeneous(t, results)
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_add_match_all(): """ Create Match All Block """ # When add a match all filter t = MatchAll() # Then I see the appropriate JSON results = { "match_all": {} } homogeneous(t, results)
def test_add_score(): """ Create Score Block """ # When add a Score field t = ScriptScore("foo") # Then I see the appropriate JSON results = { "script": "foo" } homogeneous(t, results)
def test_add_filter(): """ Create Filter with Block """ # When I create a filter t = Filter() # And add a block t.filter(Term("foo", "bar")) # Then I see the appropriate JSON results = {"and": [{"term": {"foo": "bar"}}]} homogeneous(t, results)
def test_add_score_with_lang(): """ Create Score Block with Language """ # When add a Score field t = ScriptScore("foo", lang="mvel") # Then I see the appropriate JSON results = { "script": "foo", "lang": "mvel" } homogeneous(t, results)
def test_add_sort_desc(): """ Create Sort Block Descending """ # When add a Sort block t = Sort("foo", order="dsc") # Then I see the appropriate JSON results = { "foo": { "order": "dsc" } } homogeneous(t, results)
def test_add_sort(): """ Create Sort Block """ # When add a Sort block t = Sort("foo") # Then I see the appropriate JSON results = { "foo": { "order": "asc" } } homogeneous(t, results)
def test_add_type(): """ Create Type Block """ # When add a Type Block t = Type("foo") # Then I see the appropriate JSON results = { "type": { "value": "foo" } } homogeneous(t, results)
def test_add_terms(): """ Create Terms Block """ # When add a Terms field t = Terms("foo", ["bar", "baz"]) # Then I see the appropriate JSON results = { "terms": { "foo": ["bar", "baz"] } } homogeneous(t, results)
def test_missing(): """ Create Missing Block """ # When add a Missing Block t = Missing("foo") # Then I see the appropriate JSON results = { "missing": { "field": "foo" } } homogeneous(t, results)
def test_add_agg(): """ Create aggregations block """ # When add an agg block t = Aggregations("agg_name", "field_name", "metric") # Then I see correct json results = { "agg_name": { "metric": {"field": "field_name"} } } homogeneous(t, results)
def test_add_term(): """ Create Term Block """ # When add a Term field t = Term("foo", "bar") # Then I see the appropriate JSON results = { "term": { "foo": "bar" } } homogeneous(t, results)
def test_add_score_with_params(): """ Create Score Block with Params """ # When add a Score field t = ScriptScore("foo", params={"bar": "baz"}) # Then I see the appropriate JSON results = { "script": "foo", "params": { "bar": "baz" } } homogeneous(t, results)
def test_add_sort_with_mode(): """ Create Sort Block with Mode """ # When add a Sort block t = Sort("foo", mode="bar") # Then I see the appropriate JSON results = { "foo": { "order": "asc", "mode": "bar" } } homogeneous(t, results)
def test_missing_existence(): """ Create Missing Block with Existence """ # When add a Missing Block t = Missing("foo", existence=True) # Then I see the appropriate JSON results = { "missing": { "field": "foo", "existence": True } } homogeneous(t, results)
def test_missing_null_value(): """ Create Missing Block with Null Value """ # When add a Missing Block t = Missing("foo", null_value=True) # Then I see the appropriate JSON results = { "missing": { "field": "foo", "null_value": True } } homogeneous(t, results)
def test_add_sort_with_value(): """ Create Sort Block with Value """ # When add a Sort block t = Sort("location", location=[1.23, 3.45]) # Then I see the appropriate JSON results = { "_geo_distance": { "location": [1.23, 3.45], "order": "asc" } } homogeneous(t, results)
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_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_add_gte_range(): """ Create Greater Than Or Equal Range Block """ # When add a Range Block t = Range("foo", gte="bar") # Then I see the appropriate JSON results = { "range": { "foo": { "gte": "bar", } } } homogeneous(t, results)
def test_add_terms_with_execution(): """ Create Terms With Execution """ # When add a Terms field t = Terms("foo", ["bar", "baz"], execution="and") # Then I see the appropriate JSON results = { "terms": { "foo": ["bar", "baz"], "execution": "and" } } homogeneous(t, 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)