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

Update Search API return result #68

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
2 changes: 1 addition & 1 deletion elasticmock/fake_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ def search(self, index=None, doc_type=None, body=None, params=None, headers=None

result = {
'hits': {
'total': len(matches),
'total': {'value': len(matches), 'relation': 'eq'},
'max_score': 1.0
},
'_shards': {
Expand Down
4 changes: 2 additions & 2 deletions tests/fake_elasticsearch/test_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ def test_should_not_raise_exception_when_delete_nonindexed_document_if_ignored_l
def test_should_delete_indexed_document(self):
doc_indexed = self.es.index(index=INDEX_NAME, doc_type=DOC_TYPE, body=BODY)
search = self.es.search(index=INDEX_NAME)
self.assertEqual(1, search.get('hits').get('total'))
self.assertEqual(1, search.get('hits').get('total').get('value'))

doc_id = doc_indexed.get('_id')
doc_deleted = self.es.delete(index=INDEX_NAME, doc_type=DOC_TYPE, id=doc_id)
search = self.es.search(index=INDEX_NAME)
self.assertEqual(0, search.get('hits').get('total'))
self.assertEqual(0, search.get('hits').get('total').get('value'))

expected_doc_deleted = {
'found': True,
Expand Down
4 changes: 2 additions & 2 deletions tests/fake_elasticsearch/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ def test_doc_type_can_be_list(self):
self.es.index(index=INDEX_NAME, doc_type=doc_type, body={})

result = self.es.search(doc_type=[doc_types[0]])
self.assertEqual(count_per_doc_type, result.get('hits').get('total'))
self.assertEqual(count_per_doc_type, result.get('hits').get('total').get('value'))

result = self.es.search(doc_type=doc_types[:2])
self.assertEqual(count_per_doc_type * 2, result.get('hits').get('total'))
self.assertEqual(count_per_doc_type * 2, result.get('hits').get('total').get('value'))

def test_update_existing_doc(self):
data = self.es.index(index=INDEX_NAME, doc_type=DOC_TYPE, body=BODY)
Expand Down
2 changes: 1 addition & 1 deletion tests/fake_elasticsearch/test_scroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ def __assert_scroll(self, result, expected_scroll_hits):

self.assertNotEqual(None, result.get('_scroll_id', None))
self.assertEqual(expected_scroll_hits, len(hits.get('hits')))
self.assertEqual(100, hits.get('total'))
self.assertEqual(100, hits.get('total').get('value'))
40 changes: 20 additions & 20 deletions tests/fake_elasticsearch/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_should_raise_notfounderror_when_search_for_unexistent_index(self):

def test_should_return_hits_hits_even_when_no_result(self):
search = self.es.search()
self.assertEqual(0, search.get('hits').get('total'))
self.assertEqual(0, search.get('hits').get('total').get('value'))
self.assertListEqual([], search.get('hits').get('hits'))

def test_should_return_skipped_shards(self):
Expand All @@ -28,23 +28,23 @@ def test_should_return_all_documents(self):
self.es.index(index='index_{0}'.format(i), doc_type=DOC_TYPE, body={'data': 'test_{0}'.format(i)})

search = self.es.search()
self.assertEqual(index_quantity, search.get('hits').get('total'))
self.assertEqual(index_quantity, search.get('hits').get('total').get('value'))

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

search = self.es.search(body={'query': {'match_all': {}}})
self.assertEqual(index_quantity, search.get('hits').get('total'))
self.assertEqual(index_quantity, search.get('hits').get('total').get('value'))

def test_should_return_only_indexed_documents_on_index(self):
index_quantity = 2
for i in range(0, index_quantity):
self.es.index(index=INDEX_NAME, doc_type=DOC_TYPE, body={'data': 'test_{0}'.format(i)})

search = self.es.search(index=INDEX_NAME)
self.assertEqual(index_quantity, search.get('hits').get('total'))
self.assertEqual(index_quantity, search.get('hits').get('total').get('value'))

def test_should_return_only_indexed_documents_on_index_with_doc_type(self):
index_quantity = 2
Expand All @@ -53,15 +53,15 @@ def test_should_return_only_indexed_documents_on_index_with_doc_type(self):
self.es.index(index=INDEX_NAME, doc_type='another-Doctype', body={'data': 'test'})

search = self.es.search(index=INDEX_NAME, doc_type=DOC_TYPE)
self.assertEqual(index_quantity, search.get('hits').get('total'))
self.assertEqual(index_quantity, search.get('hits').get('total').get('value'))

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'))
self.assertEqual(2, result.get('hits').get('total').get('value'))

def test_usage_of_aggregations(self):
self.es.index(index='index', doc_type='document', body={'genre': 'rock'})
Expand All @@ -78,20 +78,20 @@ def test_search_with_scroll_param(self):
result = self.es.search(index='groups', params={'scroll': '1m', 'size': 30})
self.assertNotEqual(None, result.get('_scroll_id', None))
self.assertEqual(30, len(result.get('hits').get('hits')))
self.assertEqual(100, result.get('hits').get('total'))
self.assertEqual(100, result.get('hits').get('total').get('value'))

def test_search_with_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': {'match': {'data': 'TEST'}}})
self.assertEqual(response['hits']['total'], 10)
self.assertEqual(response['hits']['total']['value'], 10)
hits = response['hits']['hits']
self.assertEqual(len(hits), 10)

response = self.es.search(index='index_for_search', doc_type=DOC_TYPE, body={'query': {'match': {'data': '3'}}})
self.assertEqual(response['hits']['total'], 1)
self.assertEqual(response['hits']['total']['value'], 1)
hits = response['hits']['hits']
self.assertEqual(len(hits), 1)
self.assertEqual(hits[0]['_source'], {'data': 'test_3'})
Expand All @@ -100,7 +100,7 @@ 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]})
response = self.es.search(index='index_for_search', doc_type=DOC_TYPE, body={'query': {'match': {'data': 1}}})
self.assertEqual(response['hits']['total'], 1)
self.assertEqual(response['hits']['total']['value'], 1)
hits = response['hits']['hits']
self.assertEqual(len(hits), 1)
self.assertEqual(hits[0]['_source'], {'data': [1, 11, 13]})
Expand All @@ -110,7 +110,7 @@ def test_search_with_match_query_in_string_list(self):
self.es.index(index='index_for_search', doc_type=DOC_TYPE, body={'data': [str(i), 'two', 'three']})

response = self.es.search(index='index_for_search', doc_type=DOC_TYPE, body={'query': {'match': {'data': '1'}}})
self.assertEqual(response['hits']['total'], 1)
self.assertEqual(response['hits']['total']['value'], 1)
hits = response['hits']['hits']
self.assertEqual(len(hits), 1)
self.assertEqual(hits[0]['_source'], {'data': ['1', 'two', 'three']})
Expand All @@ -121,12 +121,12 @@ def test_search_with_term_query(self):

response = self.es.search(index='index_for_search', doc_type=DOC_TYPE,
body={'query': {'term': {'data': 'TEST'}}})
self.assertEqual(response['hits']['total'], 0)
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': {'term': {'data': '3'}}})
self.assertEqual(response['hits']['total'], 1)
self.assertEqual(response['hits']['total']['value'], 1)
hits = response['hits']['hits']
self.assertEqual(len(hits), 1)
self.assertEqual(hits[0]['_source'], {'data': 'test_3'})
Expand All @@ -137,7 +137,7 @@ def test_search_with_bool_query(self):

response = self.es.search(index='index_for_search', doc_type=DOC_TYPE,
body={'query': {'bool': {'filter': [{'term': {'id': 1}}]}}})
self.assertEqual(response['hits']['total'], 1)
self.assertEqual(response['hits']['total']['value'], 1)
hits = response['hits']['hits']
self.assertEqual(len(hits), 1)

Expand All @@ -147,7 +147,7 @@ def test_search_with_terms_query(self):

response = self.es.search(index='index_for_search', doc_type=DOC_TYPE,
body={'query': {'terms': {'id': [1, 2, 3]}}})
self.assertEqual(response['hits']['total'], 3)
self.assertEqual(response['hits']['total']['value'], 3)
hits = response['hits']['hits']
self.assertEqual(len(hits), 3)

Expand All @@ -159,7 +159,7 @@ def test_query_on_nested_data(self):
for term, value, i in [('data.x', 1, 1), ('data.y', 'yes', 0)]:
response = self.es.search(index='index_for_search', doc_type=DOC_TYPE,
body={'query': {'term': {term: value}}})
self.assertEqual(1, response['hits']['total'])
self.assertEqual(1, response['hits']['total']['value'])
doc = response['hits']['hits'][0]['_source']
self.assertEqual(i, doc['id'])

Expand All @@ -185,7 +185,7 @@ def test_search_with_bool_query_and_multi_match(self):
}
response = self.es.search(index='index_for_search', doc_type=DOC_TYPE,
body=search_body)
self.assertEqual(response['hits']['total'], 10)
self.assertEqual(response['hits']['total']['value'], 10)
hits = response['hits']['hits']
self.assertEqual(len(hits), 10)

Expand Down Expand Up @@ -221,10 +221,10 @@ def test_msearch(self):

result = self.es.msearch(index='index_for_search', body=body)
response1, response2 = result['responses']
self.assertEqual(response1['hits']['total'], 10)
self.assertEqual(response1['hits']['total']['value'], 10)
hits1 = response1['hits']['hits']
self.assertEqual(len(hits1), 10)
self.assertEqual(response2['hits']['total'], 10)
self.assertEqual(response2['hits']['total']['value'], 10)
hits2 = response2['hits']['hits']
self.assertEqual(len(hits2), 10)

Expand Down Expand Up @@ -302,7 +302,7 @@ def test_search_with_range_query(self, _, query_range, expected_ids):
body={'query': {'range': query_range}},
)

self.assertEqual(len(expected_ids), response['hits']['total'])
self.assertEqual(len(expected_ids), response['hits']['total']['value'])
hits = response['hits']['hits']
self.assertEqual(set(expected_ids), set(hit['_source']['id'] for hit in hits))

Expand Down