Skip to content

Commit

Permalink
support shorthand wildcard/prefix queries
Browse files Browse the repository at this point in the history
  • Loading branch information
santiagoaguiar committed Aug 22, 2022
1 parent ec754b5 commit 75cee9e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion elasticmock/fake_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ def _evaluate_for_multi_match_query_type(self, document):
return self._evaluate_for_fields(document)

def _compare_value_for_field(self, doc_source, field, value, ignore_case, is_wildcard=False, is_prefix=False):
if is_wildcard or is_prefix:
if (is_wildcard or is_prefix) and type(value) == type({}):
value = value['value']
if ignore_case and isinstance(value, str):
value = value.lower()
Expand Down
32 changes: 32 additions & 0 deletions tests/fake_elasticsearch/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ def test_search_with_wildcard_query(self):
hits = response['hits']['hits']
self.assertEqual(len(hits), 3)

def test_search_with_wildcard_query_shorthand(self):
self.es.index(index='index_for_search', doc_type=DOC_TYPE, body={'data': 'test_20221010'})
self.es.index(index='index_for_search', doc_type=DOC_TYPE, body={'data': 'test_20221011'})
self.es.index(index='index_for_search', doc_type=DOC_TYPE, body={'data': 'test_20221012'})
response = self.es.search(index='index_for_search', doc_type=DOC_TYPE,
body={'query': {'wildcard': {'data': 'test_1*'}}})
self.assertEqual(response['hits']['total']['value'], 0)
hits = response['hits']['hits']
self.assertEqual(len(hits), 0)

response = self.es.search(index='index_for_search', doc_type=DOC_TYPE,
body={'query': {'wildcard': {'data':'test_*'}}})
self.assertEqual(response['hits']['total']['value'], 3)
hits = response['hits']['hits']
self.assertEqual(len(hits), 3)

def test_search_with_prefix_query(self):
self.es.index(index='index_for_search', doc_type=DOC_TYPE, body={'data': 'test_20221010'})
self.es.index(index='index_for_search', doc_type=DOC_TYPE, body={'data': 'test_20221011'})
Expand All @@ -128,6 +144,22 @@ def test_search_with_prefix_query(self):
hits = response['hits']['hits']
self.assertEqual(len(hits), 3)

def test_search_with_prefix_query_shorthand(self):
self.es.index(index='index_for_search', doc_type=DOC_TYPE, body={'data': 'test_20221010'})
self.es.index(index='index_for_search', doc_type=DOC_TYPE, body={'data': 'test_20221011'})
self.es.index(index='index_for_search', doc_type=DOC_TYPE, body={'data': 'test_20221012'})
response = self.es.search(index='index_for_search', doc_type=DOC_TYPE,
body={'query': {'prefix': {'data': 'test_1'}}}}
self.assertEqual(response['hits']['total']['value'], 0)
hits = response['hits']['hits']
self.assertEqual(len(hits), 0)

response = self.es.search(index='index_for_search', doc_type=DOC_TYPE,
body={'query': {'prefix': {'data': 'test_2'}}})
self.assertEqual(response['hits']['total']['value'], 3)
hits = response['hits']['hits']
self.assertEqual(len(hits), 3)

def test_search_with_match_query_in_int_list(self):
for i in range(0, 10):
self.es.index(index='index_for_search', doc_type=DOC_TYPE, body={'data': [i, 11, 13]})
Expand Down

0 comments on commit 75cee9e

Please sign in to comment.