def test_params_being_passed_to_search(mock_client): s = search.Search(using='mock') s = s.params(routing='42') s.execute() mock_client.search.assert_called_once_with( doc_type=[], index=None, body={'query': {'match_all': {}}}, routing='42' )
def test_cache_can_be_ignored(mock_client): s = search.Search(using='mock') r = object() s._response = r s.execute(ignore_cache=True) mock_client.search.assert_called_once_with( doc_type=[], index=None, body={'query': {'match_all': {}}}, )
def test_search_query_combines_query(): s = search.Search() s2 = s.query('match', f=42) assert s2.query._proxied == query.Match(f=42) assert s.query._proxied == query.MatchAll() s3 = s2.query('match', f=43) assert s2.query._proxied == query.Match(f=42) assert s3.query._proxied == query.Bool( must=[query.Match(f=42), query.Match(f=43)])
def test_suggest(): s = search.Search() s = s.suggest('my_suggestion', 'pyhton', term={'field': 'title'}) assert { 'suggest': { 'my_suggestion': { 'term': {'field': 'title'}, 'text': 'pyhton' } } } == s.to_dict()
def test_filter_can_be_overriden(): s = search.Search().filter('term', tag='python') s.filter = ~F(s.filter) assert { "query": { "filtered": { "query": {"match_all": {}}, "filter": {"bool": {"must_not": [{"term": {"tag": "python"}}]}} } } } == s.to_dict()
def test_search_to_dict(): s = search.Search() assert {} == s.to_dict() s = s.query("match", f=42) assert {"query": {"match": {"f": 42}}} == s.to_dict() assert {"query": {"match": {"f": 42}}, "size": 10} == s.to_dict(size=10) s.aggs.bucket("per_tag", "terms", field="f").metric("max_score", "max", field="score") d = { "aggs": { "per_tag": { "terms": { "field": "f" }, "aggs": { "max_score": { "max": { "field": "score" } } }, } }, "query": { "match": { "f": 42 } }, } assert d == s.to_dict() s = search.Search(extra={"size": 5}) assert {"size": 5} == s.to_dict() s = s.extra(from_=42) assert {"size": 5, "from": 42} == s.to_dict()
def test_source_on_clone(): assert { '_source': { 'includes': ['foo.bar.*'], 'excludes': ['foo.one'] }, 'query': { 'bool': { 'filter': [{'term': {'title': 'python'}}], } } } == search.Search().source(includes=['foo.bar.*']).\ source(excludes=['foo.one']).\ filter('term', title='python').to_dict()\ assert {'_source': False, 'query': { 'bool': { 'filter': [{'term': {'title': 'python'}}], } }} == search.Search().source( False).filter('term', title='python').to_dict()
def test_query_can_be_wrapped(): s = search.Search().query('match', title='python') s.query = Q('function_score', query=s.query, field_value_factor={'field': 'rating'}) assert { 'query': { 'function_score': { 'functions': [{'field_value_factor': {'field': 'rating'}}], 'query': {'match': {'title': 'python'}} } } }== s.to_dict()
def test_search_to_dict(): s = search.Search() assert {"query": {"match_all": {}}} == s.to_dict() s = s.query('match', f=42) assert {"query": {"match": {'f': 42}}} == s.to_dict() assert {"query": {"match": {'f': 42}}, "size": 10} == s.to_dict(size=10) s.aggs.bucket('per_tag', 'terms', field='f').metric('max_score', 'max', field='score') d = { 'aggs': { 'per_tag': { 'terms': { 'field': 'f' }, 'aggs': { 'max_score': { 'max': { 'field': 'score' } } } } }, 'query': { 'match': { 'f': 42 } } } assert d == s.to_dict() s = search.Search(extra={"size": 5}) assert {"query": {"match_all": {}}, "size": 5} == s.to_dict() s = s.extra(from_=42) assert {"query": {"match_all": {}}, "size": 5, "from": 42} == s.to_dict()
def test_query_can_be_wrapped(): s = search.Search().query("match", title="python") s.query = Q("function_score", query=s.query, field_value_factor={"field": "rating"}) assert { "query": { "function_score": { "functions": [{"field_value_factor": {"field": "rating"}}], "query": {"match": {"title": "python"}}, } } } == s.to_dict()
def test_update_from_dict(): s = search.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_params_being_passed_to_search(dummy_response): client = Mock() client.search.return_value = dummy_response s = search.Search(client) s = s.params(routing='42') s.execute() client.search.assert_called_once_with(doc_type=None, index=None, body={'query': { 'match_all': {} }}, routing='42')
def test_fields_on_clone(): assert { 'query': { 'bool': { 'filter': [{ 'term': { 'title': 'python' } }], } }, 'fields': ['title'] } == search.Search().fields(['title']).filter('term', title='python').to_dict()
def test_suggest(): s = search.Search() s = s.suggest("my_suggestion", "pyhton", term={"field": "title"}) assert { "suggest": { "my_suggestion": { "term": { "field": "title" }, "text": "pyhton" } } } == s.to_dict()
def test_complex_example(): s = search.Search() s = ( s.query("match", title="python") .query(~Q("match", title="ruby")) .filter(Q("term", category="meetup") | Q("term", category="conference")) .post_filter("terms", tags=["prague", "czech"]) .script_fields(more_attendees="doc['attendees'].value + 42") ) s.aggs.bucket("per_country", "terms", field="country").metric( "avg_attendees", "avg", field="attendees" ) s.query.minimum_should_match = 2 s = s.highlight_options(order="score").highlight("title", "body", fragment_size=50) assert { "query": { "bool": { "filter": [ { "bool": { "should": [ {"term": {"category": "meetup"}}, {"term": {"category": "conference"}}, ] } } ], "must": [{"match": {"title": "python"}}], "must_not": [{"match": {"title": "ruby"}}], "minimum_should_match": 2, } }, "post_filter": {"terms": {"tags": ["prague", "czech"]}}, "aggs": { "per_country": { "terms": {"field": "country"}, "aggs": {"avg_attendees": {"avg": {"field": "attendees"}}}, } }, "highlight": { "order": "score", "fields": {"title": {"fragment_size": 50}, "body": {"fragment_size": 50}}, }, "script_fields": {"more_attendees": {"script": "doc['attendees'].value + 42"}}, } == s.to_dict()
def test_aggs_get_copied_on_change(): s = search.Search().query("match_all") s.aggs.bucket("per_tag", "terms", field="f").metric("max_score", "max", field="score") s2 = s.query("match_all") s2.aggs.bucket("per_month", "date_histogram", field="date", interval="month") s3 = s2.query("match_all") s3.aggs["per_month"].metric("max_score", "max", field="score") s4 = s3._clone() s4.aggs.metric("max_score", "max", field="score") d = { "query": { "match_all": {} }, "aggs": { "per_tag": { "terms": { "field": "f" }, "aggs": { "max_score": { "max": { "field": "score" } } }, } }, } assert d == s.to_dict() d["aggs"]["per_month"] = { "date_histogram": { "field": "date", "interval": "month" } } assert d == s2.to_dict() d["aggs"]["per_month"]["aggs"] = {"max_score": {"max": {"field": "score"}}} assert d == s3.to_dict() d["aggs"]["max_score"] = {"max": {"field": "score"}} assert d == s4.to_dict()
def test_aggs_get_copied_on_change(): s = search.Search() s.aggs.bucket('per_tag', 'terms', field='f').metric('max_score', 'max', field='score') s2 = s.query('match_all') s2.aggs.bucket('per_month', 'date_histogram', field='date', interval='month') s3 = s2.query('match_all') s3.aggs['per_month'].metric('max_score', 'max', field='score') s4 = s3._clone() s4.aggs.metric('max_score', 'max', field='score') d = { 'query': { 'match_all': {} }, 'aggs': { 'per_tag': { 'terms': { 'field': 'f' }, 'aggs': { 'max_score': { 'max': { 'field': 'score' } } } } } } assert d == s.to_dict() d['aggs']['per_month'] = { "date_histogram": { 'field': 'date', 'interval': 'month' } } assert d == s2.to_dict() d['aggs']['per_month']['aggs'] = {"max_score": {"max": {"field": 'score'}}} assert d == s3.to_dict() d['aggs']['max_score'] = {"max": {"field": 'score'}} assert d == s4.to_dict()
def mock_perform_es_search(): return response.Response( search.Search(), { "hits": { "hits": [ { "_index": "records-hep", "_type": "hep", "_id": "elasticsearch", "_score": 12.0, "_source": { "title": "Higgs Discovery" }, }, { "_index": "records-hep", "_type": "hep", "_id": "42", "_score": 11.123, "_source": { "title": "Another Higgs Discovery" }, }, { "_index": "records-hep", "_type": "hep", "_id": "47", "_score": 1, "_source": { "title": "And another Higgs Discovery" }, }, { "_index": "records-hep", "_type": "hep", "_id": "53", "_score": 16.0, }, ], "max_score": 12.0, "total": 10 }, "timed_out": False, "took": 123 })
def test_fields_on_clone(): assert { 'query': { 'filtered': { 'filter': { 'term': { 'title': 'python' } }, 'query': { 'match_all': {} } } }, 'fields': ['title'] } == search.Search().fields('title').filter('term', title='python').to_dict()
def test_complex_example(): s = search.Search() s = s.query('match', title='python') \ .query(~Q('match', title='ruby')) \ .filter(F('term', category='meetup') | F('term', category='conference')) \ .post_filter('terms', tags=['prague', 'czech']) s.aggs.bucket('per_country', 'terms', field='country')\ .metric('avg_attendees', 'avg', field='attendees') s.query.minimum_should_match = 2 assert { 'query': { 'filtered': { 'filter': { 'bool': { 'should': [ {'term': {'category': 'meetup'}}, {'term': {'category': 'conference'}} ] } }, 'query': { 'bool': { 'must': [ {'match': {'title': 'python'}}], 'must_not': [{'match': {'title': 'ruby'}}], 'minimum_should_match': 2 } } } }, 'post_filter': { 'terms': {'tags': ['prague', 'czech']} }, 'aggs': { 'per_country': { 'terms': {'field': 'country'}, 'aggs': { 'avg_attendees': {'avg': {'field': 'attendees'}} } } } } == s.to_dict()
def test_aggs_allow_two_metric(): s = search.Search() s.aggs.metric("a", "max", field="a").metric("b", "max", field="b") assert s.to_dict() == { "aggs": { "a": { "max": { "field": "a" } }, "b": { "max": { "field": "b" } } } }
def test_exclude(): s = search.Search() s = s.exclude("match", title="python") assert { "query": { "bool": { "filter": [{ "bool": { "must_not": [{ "match": { "title": "python" } }] } }] } } } == s.to_dict()
def test_exclude(): s = search.Search() s = s.exclude('match', title='python') assert { 'query': { 'bool': { 'filter': [{ 'bool': { 'must_not': [{ 'match': { 'title': 'python' } }] } }] } } } == s.to_dict()
def test_aggs_allow_two_metric(): s = search.Search() s.aggs.metric('a', 'max', field='a').metric('b', 'max', field='b') assert s.to_dict() == { 'aggs': { 'a': { 'max': { 'field': 'a' } }, 'b': { 'max': { 'field': 'b' } } } }
def mock_perform_es_search_onerecord(): return response.Response( search.Search(), { "hits": { "hits": [{ "_index": "records-hep", "_type": "hep", "_id": "42", "_score": 11.123, "_source": { "control_number": 1410174 } }], "max_score": 11.123, "total": 1 }, "timed_out": False, "took": 123 })
def test_partial_fields_on_clone(): assert { 'query': { 'bool': { 'filter': [ { 'term': { 'title': 'python' } }, ] } }, 'partial_fields': { 'foo': { 'include': ['foo.bar.*'], 'exclude': ['foo.one'] } } } == search.Search().partial_fields(foo={ 'include': ['foo.bar.*'], 'exclude': ['foo.one'] }).filter('term', title='python').to_dict()
def test_slice(): s = search.Search() assert { 'query': { 'match_all': {} }, 'from': 3, 'size': 7 } == s[3:10].to_dict() assert { 'query': { 'match_all': {} }, 'from': 0, 'size': 5 } == s[:5].to_dict() assert { 'query': { 'match_all': {} }, 'from': 3, 'size': 10 } == s[3:].to_dict()
def test_execute_uses_cache(): s = search.Search() r = object() s._response = r assert r is s.execute()
def test_search_starts_with_empty_query(): s = search.Search() assert s.query._proxied == query.MatchAll()
def test_cache_isnt_cloned(): s = search.Search() s._response = object() assert not hasattr(s._clone(), '_response')