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_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_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_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_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_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_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_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_search_with_scoring_min_score_and_track_scores(context): """ Search with match_all query and scoring with min_score and track_scores """ # 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": "baz", "scoring_field": 3}) # And I do a search score = ScriptScore("final_score = 0 + doc['scoring_field'].value;") t.score(score, min_score=2, track_scores=True) 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": 3}) results[1]['_source'].should.equal({"bar": "baz", "scoring_field": 2})
def test_search_with_scoring_and_lang(context): """ Search with custom scoring and language """ # 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 scoring with a language score = ScriptScore("s = 0 + doc['bar'].value", lang="mvel") t.score(score) results = t[0:10] # Then my results are scored correctly len(results).should.equal(3) results[0]["_source"]["bar"].should.equal(3) results[1]["_source"]["bar"].should.equal(2) results[2]["_source"]["bar"].should.equal(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_multiple_scoring(context): """ Search with custom scoring and params """ # When create a query block t = QuerySet("localhost", index="foo") # And there are records add_document("foo", {"bar": 1, "baz": 4}) add_document("foo", {"bar": 1}) # And I add scoring with params score = ScriptScore("s = custom_param + doc['bar'].value", params={"custom_param": 1}) t.score(score) boost = {"boost_factor": "10", "filter": Exists("baz")} t.score(boost) results = t[0:10] # Then my results are scored correctly len(results).should.equal(2) results[0]["_source"]["baz"].should.equal(4) ("baz" in results[1]["_source"].keys()).should.be.false