def test_compile_nested_raises_when_paths_and_search_paths_dont_have_the_same_length( ): query = { 'paths': [ 'foo', 'bar', ], 'search_paths': ['baz'], 'type': 'nested', } with pytest.raises(ValueError) as excinfo: _compile_nested(query, None) assert 'same length' in str(excinfo.value)
def test_compile_nested_raises_when_search_paths_dont_share_a_common_path(): query = { 'paths': [ 'foo.bar', 'foo.baz', ], 'search_paths': [ 'bar', 'baz', ], 'type': 'nested', } with pytest.raises(ValueError) as excinfo: _compile_nested(query, None) assert 'common path' in str(excinfo.value)
def test_compile_nested_requires_all_paths_to_contain_a_value_in_order_to_generate_a_query( ): query = { 'paths': [ 'reference.publication_info.journal_title', 'reference.publication_info.journal_volume', 'reference.publication_info.artid', ], 'search_paths': [ 'publication_info.journal_title', 'publication_info.journal_volume', 'publication_info.artid', ], } reference = { 'reference': { 'publication_info': { 'label': '23', 'misc': [ 'Strai~burger, C., this Conference', ], }, }, } assert _compile_nested(query, reference) is None
def test_nested_query_uses_correct_path_if_only_one_search_path_provided(): query = { "type": "nested", "paths": ["full_name"], "search_paths": ["authors.full_name"], } author_data = {"full_name": "John Smith"} expected = { "query": { "nested": { "path": "authors", "query": { "bool": { "must": [{ "match": { "authors.full_name": "John Smith" } }] } }, } } } result = _compile_nested(query, author_data) assert result == expected
def test_nested_query_with_and_operator(): query = { "type": "nested", "paths": ["full_name"], "search_paths": ["authors.full_name"], "operator": "AND" } author_data = {"full_name": "John Smith"} expected = { "query": { "nested": { "path": "authors", "query": { "bool": { "must": [ { "match": { "authors.full_name": { 'query': "John Smith", "operator": "AND", } } } ] } }, } } } result = _compile_nested(query, author_data) assert result == expected
def test_compile_nested(): query = { 'paths': [ 'reference.publication_info.journal_title', 'reference.publication_info.journal_volume', 'reference.publication_info.artid', ], 'search_paths': [ 'publication_info.journal_title', 'publication_info.journal_volume', 'publication_info.artid', ], 'type': 'nested', } reference = { 'reference': { 'publication_info': { 'journal_title': 'Phys.Rev.', 'journal_volume': 'D94', 'artid': '124054', }, }, } expected = { 'query': { 'nested': { 'path': 'publication_info', 'query': { 'bool': { 'must': [ { 'match': { 'publication_info.journal_title': 'Phys.Rev.', }, }, { 'match': { 'publication_info.journal_volume': 'D94', }, }, { 'match': { 'publication_info.artid': '124054', }, }, ], }, }, }, }, } result = _compile_nested(query, reference) assert expected == result
def test_compile_nested_with_inner_hits(): query = { 'paths': [ 'first_name', 'last_name', ], 'search_paths': [ 'authors.first_name', 'authors.last_name', ], 'type': 'nested', 'inner_hits': {"_source": ["authors.full_name"]} } author_data = { "first_name": "Name", "last_name": "Test" } expected = { 'query': { 'nested': { 'path': 'authors', 'query': { 'bool': { 'must': [ { 'match': { 'authors.first_name': { 'query': 'Name', 'operator': 'OR' } }, }, { 'match': { 'authors.last_name': { 'query': 'Test', 'operator': 'OR' } }, }, ], }, }, "inner_hits": { "_source": [ "authors.full_name" ] } }, }, } result = _compile_nested(query, author_data) assert expected == result