Skip to content

Commit 0c0447c

Browse files
committed
Adds missing attributesToRetrieve option into search_index.get_objects
1 parent 1796e52 commit 0c0447c

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

algoliasearch/search_index.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,21 @@ def get_object(self, object_id, request_options=None):
126126
def get_objects(self, object_ids, request_options=None):
127127
# type: (Iterator[str], Optional[Union[dict, RequestOptions]]) -> dict
128128

129+
if request_options is None or isinstance(request_options, dict):
130+
request_options = RequestOptions.create(
131+
self._config,
132+
request_options
133+
)
134+
129135
requests = []
130136
for object_id in object_ids:
131137
request = {'indexName': self._name, 'objectID': str(object_id)}
138+
139+
if 'attributesToRetrieve' in request_options.data:
140+
request['attributesToRetrieve'] = request_options.data.pop(
141+
'attributesToRetrieve'
142+
)
143+
132144
requests.append(request)
133145

134146
return self._transporter.read(

tests/unit/test_search_index.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import mock
44

55
from algoliasearch.exceptions import MissingObjectIdException
6+
from algoliasearch.http.request_options import RequestOptions
67
from algoliasearch.responses import Response
78
from algoliasearch.search_index import SearchIndex
89
from algoliasearch.configs import SearchConfig
@@ -98,6 +99,45 @@ def test_partial_update_objects(self):
9899
None,
99100
)
100101

102+
def test_get_objects(self):
103+
request_options = RequestOptions.create(self.config)
104+
105+
requests = [{
106+
'indexName': 'index-name',
107+
'objectID': 'foo_id'
108+
}]
109+
110+
self.index.get_objects(['foo_id'], request_options)
111+
112+
self.transporter.read.assert_called_once_with(
113+
'POST',
114+
'1/indexes/*/objects',
115+
{'requests': requests}, # asserts version 2 it's used.
116+
request_options
117+
)
118+
119+
def test_get_objects_with_attributes_to_retreive(self):
120+
request_options = RequestOptions.create(self.config, {
121+
'attributesToRetrieve': ['firstname', 'lastname']
122+
})
123+
124+
requests = [{
125+
'indexName': 'index-name',
126+
'objectID': 'foo_id',
127+
'attributesToRetrieve': ['firstname', 'lastname']
128+
}]
129+
130+
self.index.get_objects(['foo_id'], request_options)
131+
132+
self.transporter.read.assert_called_once_with(
133+
'POST',
134+
'1/indexes/*/objects',
135+
{'requests': requests}, # asserts version 2 it's used.
136+
request_options
137+
)
138+
139+
self.assertNotIn('attributesToRetrieve', request_options.data)
140+
101141
def test_get_settings(self):
102142
self.transporter.read.return_value = {
103143
'attributesToIndex': ['attr1', 'attr2'],

0 commit comments

Comments
 (0)