Skip to content

Commit

Permalink
Search and count accept list of index names as 'index' params
Browse files Browse the repository at this point in the history
  • Loading branch information
Florent Rivoire committed Aug 30, 2017
1 parent a887d3f commit 1f4aa78
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
29 changes: 21 additions & 8 deletions elasticmock/fake_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,7 @@ def get_source(self, index, doc_type, id, params=None):
'suggest_size', 'suggest_text', 'terminate_after', 'timeout',
'track_scores', 'version')
def count(self, index=None, doc_type=None, body=None, params=None):
if index is not None and index not in self.__documents_dict:
raise NotFoundError(404, 'IndexMissingException[[{0}] missing]'.format(index))

searchable_indexes = [index] if index is not None else self.__documents_dict.keys()
searchable_indexes = self._normalize_index_to_list(index)

i = 0
for searchable_index in searchable_indexes:
Expand Down Expand Up @@ -149,10 +146,7 @@ def count(self, index=None, doc_type=None, body=None, params=None):
'suggest_size', 'suggest_text', 'terminate_after', 'timeout',
'track_scores', 'version')
def search(self, index=None, doc_type=None, body=None, params=None):
if index is not None and index not in self.__documents_dict:
raise NotFoundError(404, 'IndexMissingException[[{0}] missing]'.format(index))

searchable_indexes = [index] if index is not None else self.__documents_dict.keys()
searchable_indexes = self._normalize_index_to_list(index)

matches = []
for searchable_index in searchable_indexes:
Expand Down Expand Up @@ -235,3 +229,22 @@ def suggest(self, body, index=None, params=None):
}
]
return result_dict

def _normalize_index_to_list(self, index):
# Ensure to have a list of index
if index is None:
searchable_indexes = self.__documents_dict.keys()
elif isinstance(index, str) or isinstance(index, unicode):
searchable_indexes = [index]
elif isinstance(index, list):
searchable_indexes = index
else:
# Is it the correct exception to use ?
raise ValueError("Invalid param 'index'")

# Check index(es) exists
for searchable_index in searchable_indexes:
if searchable_index not in self.__documents_dict:
raise NotFoundError(404, 'IndexMissingException[[{0}] missing]'.format(searchable_index))

return searchable_indexes
16 changes: 16 additions & 0 deletions tests/test_elasticmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,22 @@ def test_should_return_suggestions(self):
],
}, suggestion)

def test_should_search_in_multiple_indexes(self):
self.es.index(index='groups', doc_type='groups', body={'budget': 1000})
self.es.index(index='users', doc_type='users', body={'name': 'toto'})
self.es.index(index='pcs', doc_type='pcs', body={'model': 'macbook'})

result = self.es.search(index=['users', 'pcs'])
self.assertEqual(2, result.get('hits').get('total'))

def test_should_count_in_multiple_indexes(self):
self.es.index(index='groups', doc_type='groups', body={'budget': 1000})
self.es.index(index='users', doc_type='users', body={'name': 'toto'})
self.es.index(index='pcs', doc_type='pcs', body={'model': 'macbook'})

result = self.es.count(index=['users', 'pcs'])
self.assertEqual(2, result.get('count'))


if __name__ == '__main__':
unittest.main()

0 comments on commit 1f4aa78

Please sign in to comment.