Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Merge branch 'postatum-99443364_wrong_fields_resp' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jstoiko committed Jul 23, 2015
2 parents 1184663 + e4a6471 commit f9af8de
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
11 changes: 4 additions & 7 deletions nefertari/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ def process_fields_param(fields):
* Fields list is split if needed
* '_type' field is added, if not present, so the actual value is
displayed instead of 'None'
* '_source=False' is returned as well, so document source is not
loaded from ES. This is done because source is not used when
'fields' param is provided
"""
if not fields:
return fields
Expand All @@ -122,8 +119,8 @@ def process_fields_param(fields):
if '_type' not in fields:
fields.append('_type')
return {
'fields': fields,
'_source': False,
'_source_include': fields,
'_source': True,
}


Expand Down Expand Up @@ -427,7 +424,7 @@ def get_by_ids(self, ids, **params):

for _d in data['docs']:
try:
_d = _d['fields'] if fields else _d['_source']
_d = _d['_source']
except KeyError:
msg = "ES: '%s(%s)' resource not found" % (
_d['_type'], _d['_id'])
Expand Down Expand Up @@ -577,7 +574,7 @@ def get_collection(self, **params):
return documents

for da in data['hits']['hits']:
_d = da['fields'] if fields else da['_source']
_d = da['_source']
_d['_score'] = da['_score']
documents.append(dict2obj(_d))

Expand Down
21 changes: 10 additions & 11 deletions tests/test_elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ def test_process_fields_param_no_fields(self):

def test_process_fields_param_string(self):
assert es.process_fields_param('foo,bar') == {
'fields': ['foo', 'bar', '_type'],
'_source': False
'_source_include': ['foo', 'bar', '_type'],
'_source': True
}

def test_process_fields_param_list(self):
assert es.process_fields_param(['foo', 'bar']) == {
'fields': ['foo', 'bar', '_type'],
'_source': False
'_source_include': ['foo', 'bar', '_type'],
'_source': True
}

@patch('nefertari.elasticsearch.ES')
Expand Down Expand Up @@ -434,17 +434,16 @@ def test_get_by_ids_fields(self, mock_mget):
'_type': 'foo',
'_id': 1,
'_source': {'_id': 1, '_type': 'Story', 'name': 'bar'},
'fields': {'name': 'bar'}
}]
}
docs = obj.get_by_ids(documents, _limit=1, _fields=['name'])
mock_mget.assert_called_once_with(
body={'docs': [{'_index': 'foondex', '_type': 'story', '_id': 1}]},
fields=['name', '_type'], _source=False
_source_include=['name', '_type'], _source=True
)
assert len(docs) == 1
assert not hasattr(docs[0], '_id')
assert not hasattr(docs[0], '_type')
assert hasattr(docs[0], '_id')
assert hasattr(docs[0], '_type')
assert docs[0].name == 'bar'
assert docs._nefertari_meta['total'] == 1
assert docs._nefertari_meta['start'] == 0
Expand Down Expand Up @@ -644,16 +643,16 @@ def test_get_collection_fields(self, mock_search):
obj = es.ES('Foo', 'foondex')
mock_search.return_value = {
'hits': {
'hits': [{'fields': {'foo': 'bar', 'id': 1}, '_score': 2}],
'hits': [{'_source': {'foo': 'bar', 'id': 1}, '_score': 2}],
'total': 4,
},
'took': 2.8,
}
docs = obj.get_collection(
fields=['foo'], body={'foo': 'bar'}, from_=0)
mock_search.assert_called_once_with(
body={'foo': 'bar'}, fields=['foo', '_type'], from_=0,
_source=False)
body={'foo': 'bar'}, _source_include=['foo', '_type'],
from_=0, _source=True)
assert len(docs) == 1
assert docs[0].id == 1
assert docs[0]._score == 2
Expand Down

0 comments on commit f9af8de

Please sign in to comment.