Skip to content

Commit b0fc158

Browse files
PLNechLeoErcolanelli
authored andcommitted
index: add attributes_to_retrieve to get_objects
1 parent 4cf8844 commit b0fc158

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

algoliasearch/index.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ def __init__(self, client, index_name):
8282
self.index_name = index_name
8383
self._request_path = '/1/indexes/%s' % safe(self.index_name)
8484

85-
8685
@deprecated
8786
def addObject(self, content, object_id=None):
8887
return self.add_object(content, object_id)
@@ -141,19 +140,20 @@ def get_object(self, object_id, attributes_to_retrieve=None):
141140
def getObjects(self, object_ids):
142141
return self.get_objects(object_ids)
143142

144-
def get_objects(self, object_ids):
143+
def get_objects(self, object_ids, attributes_to_retrieve=None):
145144
"""
146145
Get several objects from this index.
147146
148147
@param object_ids the array of unique identifier of objects to retrieve
148+
@param attributes_to_retrieve (optional) if set, contains the list
149+
of attributes to retrieve as a string separated by a comma
149150
"""
150-
151151
requests = []
152152
for object_id in object_ids:
153-
requests.append({
154-
'indexName': self.index_name,
155-
'objectID': object_id
156-
})
153+
request = {'indexName': self.index_name, 'objectID': object_id}
154+
if attributes_to_retrieve is not None:
155+
request['attributesToRetrieve'] = ",".join(attributes_to_retrieve)
156+
requests.append(request)
157157
data = {'requests': requests}
158158
path = '/1/indexes/*/objects' # Use client._req()
159159
return self.client._req(True, path, 'POST', data=data)
@@ -487,7 +487,7 @@ def search_disjunctive_faceting(self, query, disjunctive_facets,
487487
for i in range(1, len(answers['results'])):
488488
for facet in answers['results'][i]['facets']:
489489
aggregated_answer['disjunctiveFacets'][facet] = (
490-
answers['results'][i]['facets'][facet])
490+
answers['results'][i]['facets'][facet])
491491

492492
if facet not in disjunctive_refinements:
493493
continue

tests/test_index.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,15 @@ def test_get_objects(self):
212212
self.assertDictContainsSubset(self.objs[0], res['results'][1])
213213
self.assertDictContainsSubset(self.objs[2], res['results'][2])
214214

215+
def test_get_objects_with_attributes_to_retrieve(self):
216+
res = self.index.get_objects(self.objectIDs[1:3], attributes_to_retrieve=['name', 'email'])
217+
for obj, obj_res in zip(self.objs[1:3], res['results']):
218+
self.assertEqual(obj['name'], obj_res['name'])
219+
self.assertEqual(obj['email'], obj_res['email'])
220+
self.assertNotIn('phone', obj_res)
221+
self.assertNotIn('city', obj_res)
222+
self.assertNotIn('country', obj_res)
223+
215224
def test_browse(self):
216225
res = self.index.browse(page=0, hits_per_page=2)
217226
self.assertEqual(res['page'], 0)

0 commit comments

Comments
 (0)