diff --git a/elasticmock/fake_elasticsearch.py b/elasticmock/fake_elasticsearch.py index dab9b74..6a72948 100644 --- a/elasticmock/fake_elasticsearch.py +++ b/elasticmock/fake_elasticsearch.py @@ -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() diff --git a/tests/fake_elasticsearch/test_search.py b/tests/fake_elasticsearch/test_search.py index c154add..a6b6ec1 100644 --- a/tests/fake_elasticsearch/test_search.py +++ b/tests/fake_elasticsearch/test_search.py @@ -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'}) @@ -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]})