From 7fd6bd35fd4565356bb0200dd497814aa1e6e170 Mon Sep 17 00:00:00 2001 From: algolia-bot Date: Wed, 26 Jun 2024 11:02:55 +0000 Subject: [PATCH] docs: add saveObjects, deleteObjects and partialUpdateObjects to helpers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/algolia/api-clients-automation/pull/3256 Co-authored-by: Clément Vannicatte --- algoliasearch/search/client.py | 45 +++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/algoliasearch/search/client.py b/algoliasearch/search/client.py index ef1a8af13..5a3feb1b4 100644 --- a/algoliasearch/search/client.py +++ b/algoliasearch/search/client.py @@ -443,11 +443,54 @@ def create_temporary_name(self, index_name: str) -> str: """ return "{}_tmp_{}".format(index_name, randint(1000000, 9999999)) + async def save_objects( + self, + index_name: str, + objects: List[Dict[str, Any]], + ) -> List[BatchResponse]: + """ + Helper: Saves the given array of objects in the given index. The `chunked_batch` helper is used under the hood, which creates a `batch` requests with at most 1000 objects in it. + """ + return await self.chunked_batch( + index_name=index_name, objects=objects, action=Action.ADDOBJECT + ) + + async def delete_objects( + self, + index_name: str, + object_ids: List[str], + ) -> List[BatchResponse]: + """ + Helper: Deletes every records for the given objectIDs. The `chunked_batch` helper is used under the hood, which creates a `batch` requests with at most 1000 objectIDs in it. + """ + return await self.chunked_batch( + index_name=index_name, + objects=[{"objectID": id} for id in object_ids], + action=Action.DELETEOBJECT, + ) + + async def partial_update_objects( + self, + index_name: str, + objects: List[Dict[str, Any]], + create_if_not_exists: Optional[bool] = False, + ) -> List[BatchResponse]: + """ + Helper: Replaces object content of all the given objects according to their respective `objectID` field. The `chunked_batch` helper is used under the hood, which creates a `batch` requests with at most 1000 objects in it. + """ + return await self.chunked_batch( + index_name=index_name, + objects=objects, + action=Action.PARTIALUPDATEOBJECT + and create_if_not_exists + or Action.PARTIALUPDATEOBJECTNOCREATE, + ) + async def chunked_batch( self, index_name: str, objects: List[Dict[str, Any]], - action: Action = "addObject", + action: Action = Action.ADDOBJECT, wait_for_tasks: bool = False, batch_size: int = 1000, request_options: Optional[Union[dict, RequestOptions]] = None,