Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Should to bool Query #67

Merged
merged 1 commit into from
Mar 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions elasticmock/fake_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def _evaluate_for_query_type(self, document):
return self._evaluate_for_compound_query_type(document)
elif self.type == QueryType.MUST:
return self._evaluate_for_compound_query_type(document)
elif self.type == QueryType.SHOULD:
return self._evaluate_for_should_query_type(document)
elif self.type == QueryType.MULTI_MATCH:
return self._evaluate_for_multi_match_query_type(document)
else:
Expand Down Expand Up @@ -206,6 +208,18 @@ def _evaluate_for_compound_query_type(self, document):

return return_val

def _evaluate_for_should_query_type(self, document):
return_val = False
for sub_condition in self.condition:
for sub_condition_key in sub_condition:
return_val = FakeQueryCondition(
QueryType.get_query_type(sub_condition_key),
sub_condition[sub_condition_key]
).evaluate(document)
if return_val:
return True
return return_val

def _evaluate_for_multi_match_query_type(self, document):
return self._evaluate_for_fields(document)

Expand Down
21 changes: 21 additions & 0 deletions tests/fake_elasticsearch/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,27 @@ def test_search_with_bool_query_and_multi_match(self):
hits = response['hits']['hits']
self.assertEqual(len(hits), 10)

def test_search_bool_should_match_query(self):
for i in range(0, 10):
self.es.index(index='index_for_search', doc_type=DOC_TYPE, body={'data': 'test_{0}'.format(i)})

response = self.es.search(index='index_for_search', doc_type=DOC_TYPE,
body={
'query': {
'bool': {
'should': [
{'match': {'data': 'test_0'}},
{'match': {'data': 'test_1'}},
{'match': {'data': 'test_2'}},
]
}
}
})
self.assertEqual(response['hits']['total'], 3)
hits = response['hits']['hits']
self.assertEqual(len(hits), 3)
self.assertEqual(hits[0]['_source'], {'data': 'test_0'})

def test_msearch(self):
for i in range(0, 10):
self.es.index(index='index_for_search1', doc_type=DOC_TYPE, body={
Expand Down