def test_sort(): s = Search() s = s.sort("fielda", "-fieldb") assert ["fielda", {"fieldb": {"order": "desc"}}] == s._sort assert {"sort": ["fielda", {"fieldb": {"order": "desc"}}]} == s.to_dict() s = s.sort() assert [] == s._sort assert Search().to_dict() == s.to_dict()
def test_expand__to_dot_is_respected(self): s = Search().query("match", a__b=42, _expand__to_dot=False) self.assertEqual({"query": { "match": { "a__b": { "query": 42 } } }}, s.to_dict())
def test_update_from_dict(self): s = Search() s.update_from_dict({"indices_boost": [{"important-documents": 2}]}) s.update_from_dict({"_source": ["id", "name"]}) assert { "indices_boost": [{ "important-documents": 2 }], "_source": ["id", "name"], } == s.to_dict()
def test_suggest(self): s = Search() s = s.suggest("my_suggestion", "pyhton", term={"field": "title"}) assert { "suggest": { "my_suggestion": { "term": { "field": "title" }, "text": "pyhton" } } } == s.to_dict()
def test_aggs_allow_two_metric(): s = Search() s = s.aggs({"a": Max(field="a"), "b": Max(field="b")}) assert s.to_dict() == { "aggs": { "a": { "max": { "field": "a" } }, "b": { "max": { "field": "b" } } } }
def test_search_to_dict(self): s = Search() assert {} == s.to_dict() s = s.query("match", f=42) assert {"query": {"match": {"f": {"query": 42}}}} == s.to_dict() assert { "query": { "match": { "f": { "query": 42 } } }, "size": 10 } == s.to_dict(size=10) s = s.aggs("per_tag", "terms", field="f").aggs("max_score", "max", field="score") d = { "aggs": { "per_tag": { "terms": { "field": "f" }, "aggs": { "max_score": { "max": { "field": "score" } } }, } }, "query": { "match": { "f": { "query": 42 } } }, } self.assertEqual(d, s.to_dict()) s = Search().params(size=5) assert {"size": 5} == s.to_dict() s = s.params(from_=42) assert {"size": 5, "from": 42} == s.to_dict()
def test_exclude(): s = Search() s = s.exclude("match", title="python") assert { "query": { "bool": { "filter": [{ "bool": { "must_not": [{ "match": { "title": { "query": "python" } } }] } }] } } } == s.to_dict()
def test_aggs_allow_two_metric(self): s = Search() s = s.aggs([Aggs("a", "max", field="a"), Aggs("b", "max", field="b")]) self.assertEqual( s.to_dict(), { "aggs": { "a": { "max": { "field": "a" } }, "b": { "max": { "field": "b" } } } }, )
def test_exclude(self): s = Search() s = s.exclude("match", title="python") self.assertEqual( { "query": { "bool": { "filter": [{ "bool": { "must_not": [{ "match": { "title": { "query": "python" } } }] } }] } } }, s.to_dict(), )
def test_expand__to_dot_is_respected(): s = Search().query("match", a__b=42, _expand__to_dot=False) assert {"query": {"match": {"a__b": {"query": 42}}}} == s.to_dict()