Skip to content

Commit

Permalink
feat(clients): expose waitForTasks to batch helpers [skip-bc] (#4030)…
Browse files Browse the repository at this point in the history
… (generated) [skip ci]

Co-authored-by: Clément Vannicatte <[email protected]>
  • Loading branch information
algolia-bot and shortcuts committed Oct 28, 2024
1 parent 7f5fe6b commit 8456571
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6516,7 +6516,23 @@ public <T> List<BatchResponse> saveObjects(String indexName, Iterable<T> objects
* the transporter requestOptions. (optional)
*/
public <T> List<BatchResponse> saveObjects(String indexName, Iterable<T> objects, RequestOptions requestOptions) {
return chunkedBatch(indexName, objects, Action.ADD_OBJECT, false, 1000, requestOptions);
return saveObjects(indexName, objects, false, requestOptions);
}

/**
* Helper: Saves the given array of objects in the given index. The `chunkedBatch` helper is used
* under the hood, which creates a `batch` requests with at most 1000 objects in it.
*
* @param indexName The `indexName` to replace `objects` in.
* @param objects The array of `objects` to store in the given Algolia `indexName`.
* @param waitForTasks - Whether or not we should wait until every `batch` tasks has been
* processed, this operation may slow the total execution time of this method but is more
* reliable.
* @param requestOptions The requestOptions to send along with the query, they will be merged with
* the transporter requestOptions. (optional)
*/
public <T> List<BatchResponse> saveObjects(String indexName, Iterable<T> objects, boolean waitForTasks, RequestOptions requestOptions) {
return chunkedBatch(indexName, objects, Action.ADD_OBJECT, waitForTasks, 1000, requestOptions);
}

/**
Expand All @@ -6527,7 +6543,7 @@ public <T> List<BatchResponse> saveObjects(String indexName, Iterable<T> objects
* @param objectIDs The array of `objectIDs` to delete from the `indexName`.
*/
public List<BatchResponse> deleteObjects(String indexName, List<String> objectIDs) {
return deleteObjects(indexName, objectIDs, null);
return deleteObjects(indexName, objectIDs, false, null);
}

/**
Expand All @@ -6540,6 +6556,22 @@ public List<BatchResponse> deleteObjects(String indexName, List<String> objectID
* the transporter requestOptions. (optional)
*/
public List<BatchResponse> deleteObjects(String indexName, List<String> objectIDs, RequestOptions requestOptions) {
return deleteObjects(indexName, objectIDs, false, null);
}

/**
* Helper: Deletes every records for the given objectIDs. The `chunkedBatch` helper is used under
* the hood, which creates a `batch` requests with at most 1000 objectIDs in it.
*
* @param indexName The `indexName` to delete `objectIDs` from.
* @param objectIDs The array of `objectIDs` to delete from the `indexName`.
* @param waitForTasks - Whether or not we should wait until every `batch` tasks has been
* processed, this operation may slow the total execution time of this method but is more
* reliable.
* @param requestOptions The requestOptions to send along with the query, they will be merged with
* the transporter requestOptions. (optional)
*/
public List<BatchResponse> deleteObjects(String indexName, List<String> objectIDs, boolean waitForTasks, RequestOptions requestOptions) {
List<Map<String, String>> objects = new ArrayList<>();

for (String id : objectIDs) {
Expand All @@ -6548,7 +6580,7 @@ public List<BatchResponse> deleteObjects(String indexName, List<String> objectID
objects.add(obj);
}

return chunkedBatch(indexName, objects, Action.DELETE_OBJECT, false, 1000, requestOptions);
return chunkedBatch(indexName, objects, Action.DELETE_OBJECT, waitForTasks, 1000, requestOptions);
}

/**
Expand All @@ -6562,7 +6594,29 @@ public List<BatchResponse> deleteObjects(String indexName, List<String> objectID
* will fail.
*/
public <T> List<BatchResponse> partialUpdateObjects(String indexName, Iterable<T> objects, boolean createIfNotExists) {
return partialUpdateObjects(indexName, objects, createIfNotExists, null);
return partialUpdateObjects(indexName, objects, createIfNotExists, false, null);
}

/**
* Helper: Replaces object content of all the given objects according to their respective
* `objectID` field. The `chunkedBatch` helper is used under the hood, which creates a `batch`
* requests with at most 1000 objects in it.
*
* @param indexName The `indexName` to update `objects` in.
* @param objects The array of `objects` to update in the given Algolia `indexName`.
* @param createIfNotExists To be provided if non-existing objects are passed, otherwise, the call
* will fail.
* @param waitForTasks - Whether or not we should wait until every `batch` tasks has been
* processed, this operation may slow the total execution time of this method but is more
* reliable.
*/
public <T> List<BatchResponse> partialUpdateObjects(
String indexName,
Iterable<T> objects,
boolean createIfNotExists,
boolean waitForTasks
) {
return partialUpdateObjects(indexName, objects, createIfNotExists, waitForTasks, null);
}

/**
Expand All @@ -6574,20 +6628,24 @@ public <T> List<BatchResponse> partialUpdateObjects(String indexName, Iterable<T
* @param objects The array of `objects` to update in the given Algolia `indexName`.
* @param createIfNotExists To be provided if non-existing objects are passed, otherwise, the call
* will fail.
* @param waitForTasks - Whether or not we should wait until every `batch` tasks has been
* processed, this operation may slow the total execution time of this method but is more
* reliable.
* @param requestOptions The requestOptions to send along with the query, they will be merged with
* the transporter requestOptions. (optional)
*/
public <T> List<BatchResponse> partialUpdateObjects(
String indexName,
Iterable<T> objects,
boolean createIfNotExists,
boolean waitForTasks,
RequestOptions requestOptions
) {
return chunkedBatch(
indexName,
objects,
createIfNotExists ? Action.PARTIAL_UPDATE_OBJECT : Action.PARTIAL_UPDATE_OBJECT_NO_CREATE,
false,
waitForTasks,
1000,
requestOptions
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -824,21 +824,21 @@ export type SearchClientNodeHelpers = {
getSecuredApiKeyRemainingValidity: (opts: GetSecuredApiKeyRemainingValidityOptions) => number;
};

export type DeleteObjectsOptions = Pick<ChunkedBatchOptions, 'indexName'> & {
export type DeleteObjectsOptions = Pick<ChunkedBatchOptions, 'indexName' | 'waitForTasks'> & {
/**
* The objectIDs to delete.
*/
objectIDs: string[];
};

export type PartialUpdateObjectsOptions = Pick<ChunkedBatchOptions, 'indexName' | 'objects'> & {
export type PartialUpdateObjectsOptions = Pick<ChunkedBatchOptions, 'indexName' | 'objects' | 'waitForTasks'> & {
/**
*To be provided if non-existing objects are passed, otherwise, the call will fail.
*/
createIfNotExists?: boolean;
};

export type SaveObjectsOptions = Pick<ChunkedBatchOptions, 'indexName' | 'objects'>;
export type SaveObjectsOptions = Pick<ChunkedBatchOptions, 'indexName' | 'objects' | 'waitForTasks'>;

export type ChunkedBatchOptions = ReplaceAllObjectsOptions & {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -550,13 +550,14 @@ export function createSearchClient({
* @param saveObjects - The `saveObjects` object.
* @param saveObjects.indexName - The `indexName` to save `objects` in.
* @param saveObjects.objects - The array of `objects` to store in the given Algolia `indexName`.
* @param saveObjects.waitForTasks - Whether or not we should wait until every `batch` tasks has been processed, this operation may slow the total execution time of this method but is more reliable.
* @param requestOptions - The requestOptions to send along with the query, they will be forwarded to the `batch` method and merged with the transporter requestOptions.
*/
async saveObjects(
{ indexName, objects }: SaveObjectsOptions,
{ indexName, objects, waitForTasks }: SaveObjectsOptions,
requestOptions?: RequestOptions,
): Promise<BatchResponse[]> {
return await this.chunkedBatch({ indexName, objects, action: 'addObject' }, requestOptions);
return await this.chunkedBatch({ indexName, objects, action: 'addObject', waitForTasks }, requestOptions);
},

/**
Expand All @@ -566,17 +567,19 @@ export function createSearchClient({
* @param deleteObjects - The `deleteObjects` object.
* @param deleteObjects.indexName - The `indexName` to delete `objectIDs` from.
* @param deleteObjects.objectIDs - The objectIDs to delete.
* @param deleteObjects.waitForTasks - Whether or not we should wait until every `batch` tasks has been processed, this operation may slow the total execution time of this method but is more reliable.
* @param requestOptions - The requestOptions to send along with the query, they will be forwarded to the `batch` method and merged with the transporter requestOptions.
*/
async deleteObjects(
{ indexName, objectIDs }: DeleteObjectsOptions,
{ indexName, objectIDs, waitForTasks }: DeleteObjectsOptions,
requestOptions?: RequestOptions,
): Promise<BatchResponse[]> {
return await this.chunkedBatch(
{
indexName,
objects: objectIDs.map((objectID) => ({ objectID })),
action: 'deleteObject',
waitForTasks,
},
requestOptions,
);
Expand All @@ -590,17 +593,19 @@ export function createSearchClient({
* @param partialUpdateObjects.indexName - The `indexName` to update `objects` in.
* @param partialUpdateObjects.objects - The array of `objects` to update in the given Algolia `indexName`.
* @param partialUpdateObjects.createIfNotExists - To be provided if non-existing objects are passed, otherwise, the call will fail..
* @param partialUpdateObjects.waitForTasks - Whether or not we should wait until every `batch` tasks has been processed, this operation may slow the total execution time of this method but is more reliable.
* @param requestOptions - The requestOptions to send along with the query, they will be forwarded to the `getTask` method and merged with the transporter requestOptions.
*/
async partialUpdateObjects(
{ indexName, objects, createIfNotExists }: PartialUpdateObjectsOptions,
{ indexName, objects, createIfNotExists, waitForTasks }: PartialUpdateObjectsOptions,
requestOptions?: RequestOptions,
): Promise<BatchResponse[]> {
return await this.chunkedBatch(
{
indexName,
objects,
action: createIfNotExists ? 'partialUpdateObject' : 'partialUpdateObjectNoCreate',
waitForTasks,
},
requestOptions,
);
Expand Down
12 changes: 12 additions & 0 deletions clients/algoliasearch-client-python/algoliasearch/search/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,7 @@ async def save_objects(
self,
index_name: str,
objects: List[Dict[str, Any]],
wait_for_tasks: bool = False,
request_options: Optional[Union[dict, RequestOptions]] = None,
) -> List[BatchResponse]:
"""
Expand All @@ -500,13 +501,15 @@ async def save_objects(
index_name=index_name,
objects=objects,
action=Action.ADDOBJECT,
wait_for_tasks=wait_for_tasks,
request_options=request_options,
)

async def delete_objects(
self,
index_name: str,
object_ids: List[str],
wait_for_tasks: bool = False,
request_options: Optional[Union[dict, RequestOptions]] = None,
) -> List[BatchResponse]:
"""
Expand All @@ -516,6 +519,7 @@ async def delete_objects(
index_name=index_name,
objects=[{"objectID": id} for id in object_ids],
action=Action.DELETEOBJECT,
wait_for_tasks=wait_for_tasks,
request_options=request_options,
)

Expand All @@ -524,6 +528,7 @@ async def partial_update_objects(
index_name: str,
objects: List[Dict[str, Any]],
create_if_not_exists: bool = False,
wait_for_tasks: bool = False,
request_options: Optional[Union[dict, RequestOptions]] = None,
) -> List[BatchResponse]:
"""
Expand All @@ -535,6 +540,7 @@ async def partial_update_objects(
action=Action.PARTIALUPDATEOBJECT
if create_if_not_exists
else Action.PARTIALUPDATEOBJECTNOCREATE,
wait_for_tasks=wait_for_tasks,
request_options=request_options,
)

Expand Down Expand Up @@ -5494,6 +5500,7 @@ def save_objects(
self,
index_name: str,
objects: List[Dict[str, Any]],
wait_for_tasks: bool = False,
request_options: Optional[Union[dict, RequestOptions]] = None,
) -> List[BatchResponse]:
"""
Expand All @@ -5503,13 +5510,15 @@ def save_objects(
index_name=index_name,
objects=objects,
action=Action.ADDOBJECT,
wait_for_tasks=wait_for_tasks,
request_options=request_options,
)

def delete_objects(
self,
index_name: str,
object_ids: List[str],
wait_for_tasks: bool = False,
request_options: Optional[Union[dict, RequestOptions]] = None,
) -> List[BatchResponse]:
"""
Expand All @@ -5519,6 +5528,7 @@ def delete_objects(
index_name=index_name,
objects=[{"objectID": id} for id in object_ids],
action=Action.DELETEOBJECT,
wait_for_tasks=wait_for_tasks,
request_options=request_options,
)

Expand All @@ -5527,6 +5537,7 @@ def partial_update_objects(
index_name: str,
objects: List[Dict[str, Any]],
create_if_not_exists: bool = False,
wait_for_tasks: bool = False,
request_options: Optional[Union[dict, RequestOptions]] = None,
) -> List[BatchResponse]:
"""
Expand All @@ -5538,6 +5549,7 @@ def partial_update_objects(
action=Action.PARTIALUPDATEOBJECT
if create_if_not_exists
else Action.PARTIALUPDATEOBJECTNOCREATE,
wait_for_tasks=wait_for_tasks,
request_options=request_options,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3333,16 +3333,17 @@ def get_secured_api_key_remaining_validity(secured_api_key)
#
# @param index_name [String]: The `index_name` to save `objects` in.
# @param objects [Array]: The array of `objects` to store in the given Algolia `indexName`.
# @param wait_for_tasks [Boolean]: Whether or not we should wait until every `batch` tasks has been processed, this operation may slow the total execution time of this method but is more reliable.
# @param request_options: The request options to send along with the query, they will be merged with the transporter base parameters (headers, query params, timeouts, etc.). (optional)
#
# @return [BatchResponse]
#
def save_objects(index_name, objects, request_options = {})
def save_objects(index_name, objects, wait_for_tasks = false, request_options = {})
chunked_batch(
index_name,
objects,
Search::Action::ADD_OBJECT,
false,
wait_for_tasks,
1000,
request_options
)
Expand All @@ -3352,16 +3353,17 @@ def save_objects(index_name, objects, request_options = {})
#
# @param index_name [String]: The `index_name` to delete `object_ids` from.
# @param object_ids [Array]: The object_ids to delete.
# @param wait_for_tasks [Boolean]: Whether or not we should wait until every `batch` tasks has been processed, this operation may slow the total execution time of this method but is more reliable.
# @param request_options: The request options to send along with the query, they will be merged with the transporter base parameters (headers, query params, timeouts, etc.). (optional)
#
# @return [BatchResponse]
#
def delete_objects(index_name, object_ids, request_options = {})
def delete_objects(index_name, object_ids, wait_for_tasks = false, request_options = {})
chunked_batch(
index_name,
object_ids.map { |id| {"objectID" => id} },
Search::Action::DELETE_OBJECT,
false,
wait_for_tasks,
1000,
request_options
)
Expand All @@ -3372,16 +3374,17 @@ def delete_objects(index_name, object_ids, request_options = {})
# @param index_name [String]: The `index_name` to delete `object_ids` from.
# @param objects [Array]: The objects to partially update.
# @param create_if_not_exists [Boolean]: To be provided if non-existing objects are passed, otherwise, the call will fail.
# @param wait_for_tasks [Boolean] Whether or not we should wait until every `batch` tasks has been processed, this operation may slow the total execution time of this method but is more reliable.
# @param request_options: The request options to send along with the query, they will be merged with the transporter base parameters (headers, query params, timeouts, etc.). (optional)
#
# @return [BatchResponse]
#
def partial_update_objects(index_name, objects, create_if_not_exists, request_options = {})
def partial_update_objects(index_name, objects, create_if_not_exists, wait_for_tasks = false, request_options = {})
chunked_batch(
index_name,
objects,
create_if_not_exists ? Search::Action::PARTIAL_UPDATE_OBJECT : Search::Action::PARTIAL_UPDATE_OBJECT_NO_CREATE,
false,
wait_for_tasks,
1000,
request_options
)
Expand Down
Loading

0 comments on commit 8456571

Please sign in to comment.