Skip to content

Commit

Permalink
Merge pull request #25 from jacquerie/require-that-all-fields-of-a-ne…
Browse files Browse the repository at this point in the history
…sted-query-contain-a-value

core: require a value per path in a nested query
  • Loading branch information
jacquerie authored Oct 24, 2017
2 parents f58ca29 + 0952db9 commit c16230a
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 73 deletions.
5 changes: 1 addition & 4 deletions inspire_matcher/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,17 +103,14 @@ def _compile_nested(query, record):
for path, search_path in zip(paths, search_paths):
value = get_value(record, path)
if not value:
continue
return

result['query']['nested']['query']['bool']['must'].append({
'match': {
search_path: value,
},
})

if not result['query']['nested']['query']['bool']['must']:
return

return result


Expand Down
70 changes: 1 addition & 69 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,85 +257,17 @@ def test_compile_nested():
assert expected == result


def test_compile_nested_skips_non_existing_paths():
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.journal_issue',
'reference.publication_info.artid',
'reference.publication_info.page_start',
'reference.publication_info.page_end',
],
'search_paths': [
'publication_info.journal_title',
'publication_info.journal_volume',
'publication_info.journal_issue',
'publication_info.artid',
'publication_info.page_start',
'publication_info.page_end',
],
}
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_does_not_generate_an_empty_query():
query = {
'paths': [
'reference.publication_info.journal_title',
'reference.publication_info.journal_volume',
'reference.publication_info.journal_issue',
'reference.publication_info.artid',
'reference.publication_info.page_start',
'reference.publication_info.page_end',
],
'search_paths': [
'publication_info.journal_title',
'publication_info.journal_volume',
'publication_info.journal_issue',
'publication_info.artid',
'publication_info.page_start',
'publication_info.page_end',
],
}
reference = {
Expand Down

0 comments on commit c16230a

Please sign in to comment.