Skip to content

Commit f445524

Browse files
Merge pull request #98 from algolia/feat/no_create
Add a no_create option to partial object
2 parents 24ae439 + 2dc904c commit f445524

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

algoliasearch/index.py

Lines changed: 22 additions & 12 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)
@@ -162,26 +162,32 @@ def get_objects(self, object_ids):
162162
def partialUpdateObject(self, partial_object):
163163
return self.partial_update_object(partial_object)
164164

165-
def partial_update_object(self, partial_object):
165+
def partial_update_object(self, partial_object, no_create=False):
166166
"""
167167
Update partially an object (only update attributes passed in argument).
168168
169169
@param partial_object contains the object attributes to override, the
170170
object must contains an objectID attribute
171+
@param no_create specifies whether or not a missing object must be
172+
created
171173
"""
172174
path = '/%s/partial' % safe(partial_object['objectID'])
175+
if no_create:
176+
path += '?createIfNotExists=false'
173177
return self._req(False, path, 'POST', data=partial_object)
174178

175179
@deprecated
176180
def partialUpdateObjects(self, objects):
177181
return self.partial_update_objects(objects)
178182

179-
def partial_update_objects(self, objects):
183+
def partial_update_objects(self, objects, no_create=False):
180184
"""
181185
Partially Override the content of several objects.
182186
183187
@param objects contains an array of objects to update (each object
184188
must contains a objectID attribute)
189+
@param no_create specifies whether or not a missing object must be
190+
created
185191
"""
186192
requests = []
187193
for obj in objects:
@@ -190,7 +196,7 @@ def partial_update_objects(self, objects):
190196
'objectID': obj['objectID'],
191197
'body': obj
192198
})
193-
return self.batch(requests)
199+
return self.batch(requests, no_create=no_create)
194200

195201
@deprecated
196202
def saveObject(self, obj):
@@ -487,7 +493,7 @@ def search_disjunctive_faceting(self, query, disjunctive_facets,
487493
for i in range(1, len(answers['results'])):
488494
for facet in answers['results'][i]['facets']:
489495
aggregated_answer['disjunctiveFacets'][facet] = (
490-
answers['results'][i]['facets'][facet])
496+
answers['results'][i]['facets'][facet])
491497

492498
if facet not in disjunctive_refinements:
493499
continue
@@ -876,11 +882,15 @@ def update_user_key(self, key, obj, validity=None,
876882
path = '/keys/%s' % key
877883
return self._req(False, path, 'PUT', data=obj)
878884

879-
def batch(self, requests):
885+
def batch(self, requests, no_create=False):
880886
"""Send a batch requests."""
881887
if isinstance(requests, (list, tuple)):
882888
requests = {'requests': requests}
883889

890+
path = '/batch'
891+
if no_create:
892+
path += '?createIfNotExists=false'
893+
884894
return self._req(False, '/batch', 'POST', data=requests)
885895

886896
def _req(self, is_search, path, meth, params=None, data=None):

tests/test_index.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
from algoliasearch.client import MAX_API_KEY_LENGTH
1717
from algoliasearch.helpers import AlgoliaException
1818

19-
from .helpers import safe_index_name
20-
from .helpers import get_api_client
21-
from .helpers import FakeData
19+
from tests.helpers import safe_index_name
20+
from tests.helpers import get_api_client
21+
from tests.helpers import FakeData
2222

2323

2424
class IndexTest(unittest.TestCase):
@@ -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)