Helper methods extend or combine API operations to make using the Search API easier.

Browse for records

Required ACL: browse

Retrieves all records from an index.

This helper method is based on the Browse for records API operation.

It iterates over the paginated API response and returns an iterator that lets you aggregate all records.

1
2
3
4
5
6
7
8
9
10
11
12
13
SearchClient.BrowseObjects<T>(
  string indexName,
  BrowseParamsObject browseParams,
  // Optional parameter
  RequestOptions requestOptions,
)

SearchClient.BrowseObjectsAsync<T>(
  string indexName,
  BrowseParamsObject browseParams,
  // Optional parameter
  RequestOptions requestOptions,
)

Parameters

indexName
type: string
Required

Index name from which to get all records.

browseParams
type: BrowseParamsObject
Required

Parameters for the Browse for records request.

T
type: Type parameter

The model for your index’s records, for example, Algolia.Search.Models.Search.Hit.

requestOptions
type: RequestOptions
default: null

Extra parameters, such as headers or timeouts. For more information, see Request options.

Browse for rules

Required ACL: settings

Retrieves all rules from an index.

This helper method is based on the Search for rules API operation.

It iterates over the paginated API response and returns an iterable that lets you aggregate all rules.

1
2
3
4
5
6
7
8
9
10
11
12
13
SearchClient.BrowseRules(
  String indexName,
  SearchRulesParams searchRulesParams,
  // Optional parameters
  RequestOptions requestOptions,
)

SearchClient.BrowseRulesAsync(
  String indexName,
  SearchRulesParams searchRulesParams,
  // Optional parameters
  RequestOptions requestOptions,
)

Parameters

indexName
type: string
Required

Index name from which to get all rules.

searchRulesParams
type: SearchRulesParams

Parameters for the Search for rules request.

requestOptions
type: RequestOptions
default: null

Extra parameters, such as headers or timeouts. For more information, see Request options.

Browse for synonyms

Required ACL: settings

Retrieves all synonyms from an index.

This helper method is based on the Search for synonyms API operation.

It iterates over the paginated API response and aggregates all synonyms.

1
2
3
4
5
6
7
8
9
10
11
12
13
SearchClient.BrowseSynonyms(
  string indexName,
  SearchSynonymsParams searchSynonymsParams,
  // Optional parameters
  RequestOptions requestOptions,
)

SearchClient.BrowseSynonymsAsync(
  string indexName,
  SearchSynonymsParams searchSynonymsParams,
  // Optional parameters
  RequestOptions requestOptions,
)

Parameters

indexName
type: string
Required

Index name from which to get all synonyms.

searchSynonymsParams
type: SearchSynonymsParams

Parameters for the Search for synonyms request.

requestOptions
type: RequestOptions
default: null

Extra parameters, such as headers or timeouts. For more information, see Request options.

Delete records

Required ACL: deleteObject

Deletes records from an index using their object IDs.

This helper methods is based on the Batch operations on one index API operation.

It constructs the batch write request with the deleteObject action and automatically sends requests in batches of 1,000 records.

1
2
3
4
5
6
7
SearchClient.DeleteObjectsAsync(
  string indexName,
  IEnumerable<String> objectIDs,
  // Optional parameters
  RequestOptions options,
  CancellationToken cancellationToken,
)

Parameters

indexName
type: string
Required

Index name from which to delete records.

objectIDs
type: IEnumerable
items: String
Required

Object IDs to delete.

cancellationToken
type: CancellationToken
default: default

Cancellation token for cancelling the request.

options
type: RequestOptions

Extra parameters, such as headers or timeouts. For more information, see Request options.

Create secured API keys

Creates a secured API key without any calls to Algolia’s servers.

This helper method takes a parent API key and adds restrictions to it, which can’t be changed at query time.

Secured API keys are forms of hash-based message authentication codes (HMAC).

This helper performs these steps to create a secured API key:

  1. Compute a SHA-256 HMAC with the parent API key as secret, and a URL-encoded list of query parameters as message. These query parameters will be applied with every use of the API key and can’t be changed by users.

  2. Concatenate the SHA-256 HMAC with the list of query parameters to a single string.

  3. Encode the whole string in base64.

1
2
3
4
SearchClient.GenerateSecuredApiKey(
  string parentApiKey,
  SecuredApiKeyRestrictions restriction,
)

Parameters

parentApiKey
type: string
Required

The API key to be used as secret for the secured API key. The secured API key inherits all restrictions from this API key. You can’t use an Admin API key.

restriction
type: SecuredApiKeyRestrictions

Restrictions to apply to the secured API key. You must add at least one restriction. If you make requests with a secured API key with the same restrictions as the parent_api_key, the API returns an error with the status 403.

restriction ➔ SecuredApiKeyRestrictions

SearchParams
type: SearchParamsObject

Search parameters that will always be applied with this API key. For more information, see Search.

Filters
type: string

Filters that will always be applied with this API key.

ValidUntil
type: long

Timestamp when the API key expires, in seconds since the Unix epoch.

RestrictIndices
type: List
items: string

Index names that this API key should be allowed to access. You can use the wildcard character *. For example, restrict_indices: "dev_*" permits using all indices with names starting with dev_.

RestrictSources
type: string

IP network range that is allowed to use this API key.

UserToken
type: string

Pseudonymous user identifier to restrict usage of this API key to specific users. By default, rate limits are set based on IP addresses. This can be an issue if many users search from the same IP address. To avoid this, add a user token to each generated API key.

Get secured API key remaining validity

Retrieves the time in seconds until the secured API key expires.

This helper method returns the amount of seconds left until the secured API key expires, as defined by its validUntil property. If the key already expired, this method returns a negative number, indicating the number of seconds since the key expired.

1
2
3
SearchClient.getSecuredApiKeyRemainingValidity(
  string securedApiKey,
)

Parameters

securedApiKey
type: string
Required

Secured API key for which to retrieve the time of validity.

Update attributes of records

Required ACL: addObject

Adds or updates attributes of records

This helper methods is based on the Batch operations on one index API operation.

It constructs the batch write request with the partialUpdateObject or partialUpdateObjectNoCreate action (depending on whether the create_if_not_exists option is true or false) and automatically sends requests in batches of 1,000 records.

1
2
3
4
5
6
7
8
SearchClient.PartialUpdateObjectsAsync<T>(
  string indexName,
  IEnumerable<T> objects,
  bool createIfNotExists,
  // Optional parameters
  RequestOptions options,
  CancellationToken cancellationToken,
)

Parameters

indexName
type: string
Required

Index name where to update records.

objects
type: IEnumerable<T>
Required

Records to update.

T
type: type parameter

The model of your index’s records.

createIfNotExists
type: bool
Required

Whether to create a new record if it doesn’t exist.

options
type: RequestOptions

Extra parameters, such as headers or timeouts. For more information, see Request options.

cancellationToken
type: CancellationToken
default: default

Cancellation token for cancelling the request.

Replace all records

Required ACL: addObject

Replaces all records in an index with new ones.

This helper lets you replace all records in an index without interrupting the ability to search.

It’s based on the Batch indexing operations on one index and Copy or move an index API operations and performs these actions:

  1. Copy settings, synonyms, and rules to a temporary index.
  2. Add the records from the objects parameter to the temporary index.
  3. Move the temporary index to the original index to replace it.

If you use an API key with indexes restrictions, you need to allow access to the index INDEX_NAME_tmp_* (replace INDEX_NAME with the name of your original index).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
SearchClient.ReplaceAllObjects<T>(
  string indexName,
  IEnumerable<T> objects,
  int batchSize,
  // Optional parameters
  RequestOptions options,
  CancellationToken cancellationToken,
)

SearchClient.ReplaceAllObjectsAsync<T>(
  string indexName,
  IEnumerable<T> objects,
  int batchSize,
  // Optional parameters
  RequestOptions options,
  CancellationToken cancellationToken,
)

Parameters

indexName
type: string
Required

Index name where to replace all records.

objects
type: IEnumerable<T>
Required

Records that replace the existing records in your index.

T
type: type parameter
Required

The model of your index’s records.

batchSize
type: int
default: 1,000

Batch size for the indexing operation.

options
type: RequestOptions

Extra parameters, such as headers or timeouts. For more information, see Request options.

cancellationToken
type: CancellationToken
default: default

Cancellation token for cancelling the request.

Save records

Required ACL: addObject

Adds records to an index.

This helper methods is based on the Batch operations on one index API operation.

It constructs the batch write request with the addObject action and automatically sends requests in batches of 1,000 records.

1
2
3
4
5
6
SearchClient.saveObjects(
  String indexName,
  Iterable<T> objects,
  // Optional parameters
  RequestOptions requestOptions,
)

Parameters

indexName
type: String
Required

Index name to which to add records.

objects
type: Iterable<T>
Required

Records to add.

requestOptions
type: RequestOptions

Extra parameters, such as headers or timeouts. For more information, see Request options.

Wait for API keys

Waits for an API key operation to be finished.

When you add, update, or delete API keys, it might take a while before the change is applied.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
SearchClient.WaitForApiKey(
  ApiKeyOperation operation,
  string key,
  // Optional parameter
  ApiKey apiKey,
  int maxRetries,
  Func<int, int> timeout,
  RequestOptions requestOptions,
  CancellationToken ct,
)

SearchClient.WaitForApiKeyAsync(
  ApiKeyOperation operation,
  string key,
  // Optional parameter
  ApiKey apiKey,
  int maxRetries,
  Func<int, int> timeout,
  RequestOptions requestOptions,
  CancellationToken ct,
)

Parameters

key
type: string
Required

The API key that was added, update, or deleted. For example, you can get this value from the API response when creating or updating.

operation
type: ApiKeyOperation
Required

The type of operation to wait for:

  • ApiKeyOperation.Add. Wait for the addition of a new API key
  • ApiKeyOperation.Update. Wait for the update of an existing API key
  • ApiKeyOperation.Delete. Wait for the deletion of an API key.
apiKey
type: ApiKey

When waiting for an API key to be updated, apiKey expects the original ApiKey object you passed to the Update API key operation.

maxRetries
type: int
default: 50

Maximum number of times the API key’s status should be checked.

timeout
type: Func<int,int>
default: null

A function that takes one parameter, the current number of retries, and returns the number of seconds after which to check the API key’s status again.

requestOptions
type: RequestOptions

Extra parameters, such as headers or timeouts. For more information, see Request options.

ct
type: CancellationToken
default: default

Cancellation token to cancel the request.

Examples

Wait until an API key is added:

1
2
3
4
5
6
7
8
9
using Algolia.Search.Clients;
using Algolia.Search.Models.Search;

var client = new SearchClient(new SearchConfig("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY"));

var response = await client.WaitForApiKeyAsync(
  "api-key-add-operation-test-csharp",
  Enum.Parse<ApiKeyOperation>("Add")
);

Wait until an API key is updated:

1
2
3
4
5
6
7
8
9
using Algolia.Search.Clients;
using Algolia.Search.Models.Search;

var client = new SearchClient(new SearchConfig("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY"));

var response = await client.WaitForApiKeyAsync(
  "api-key-add-operation-test-csharp",
  Enum.Parse<ApiKeyOperation>("Add")
);

Wait until an API key is deleted:

1
2
3
4
5
6
7
8
9
using Algolia.Search.Clients;
using Algolia.Search.Models.Search;

var client = new SearchClient(new SearchConfig("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY"));

var response = await client.WaitForApiKeyAsync(
  "api-key-add-operation-test-csharp",
  Enum.Parse<ApiKeyOperation>("Add")
);

Wait for application-level tasks

Waits for an application-level task to complete.

While indexing operations are index-level tasks, some tasks, such as working with dictionaries, are application-level tasks. They’re asynchronous: when you make a request, Algolia adds the task to a queue and runs it depending on the server load. The response includes a taskID property that you can use to wait until the task is complete.

This helper method polls the Check application task status API operation and stops if the task’s status is published.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
SearchClient.WaitForAppTask(
  long taskId,
  // Optional parameters
  int maxRetries,
  Func<int,int> timeout,
  RequestOptions requestOptions,
  CancellationToken ct,
)

SearchClient.WaitForAppTaskAsync(
  long taskId,
  // Optional parameters
  int maxRetries,
  Func<int,int> timeout,
  RequestOptions requestOptions,
  CancellationToken ct,
)

Parameters

taskId
type: long
Required

Task ID for which to wait.

maxRetries
type: int
default: 50

Maximum number of times the task status should be checked.

timeout
type: Func<int,int>
default: null

A function that takes one parameter, the current number of retries, and returns the number of seconds after which to check the task’s status again.

requestOptions
type: RequestOptions
default: null

Extra parameters, such as headers or timeouts. For more information, see Request options.

ct
type: CancellationToken
default: default

Cancellation token to cancel the request.

Examples

1
2
3
4
5
6
using Algolia.Search.Clients;
using Algolia.Search.Models.Search;

var client = new SearchClient(new SearchConfig("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY"));

var response = await client.WaitForAppTaskAsync(123L);

Wait for index operations

Waits for an indexing task to complete.

Indexing operations are asynchronous. When you make a request for an indexing operation, such as, adding or updating records, Algolia adds a task to a queue and returns a task_id property with the response. The task itself runs separately, depending on the server load.

This helper method polls the Check task status API operation and stops if the task’s status is published.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
SearchClient.WaitForTask(
  string indexName,
  long taskId,
  // Optional parameters
  int maxRetries,
  Func<int,int> timeout,
  RequestOptions requestOptions,
  CancellationToken ct,
)

SearchClient.WaitForTaskAsync(
  string indexName,
  long taskId,
  // Optional parameters
  int maxRetries,
  Func<int,int> timeout,
  RequestOptions requestOptions,
  CancellationToken ct,
)

Parameters

indexName
type: string
Required

Index name for which to check the task.

taskId
type: long
Required

Task ID for which to wait.

maxRetries
type: int
default: 50

Maximum number of times the task status should be checked.

timeout
type: Func<int,int>
default: null

A function that takes one parameter, the current number of retries, and returns the number of seconds after which to check the task’s status again.

requestOptions
type: RequestOptions
default: null

Extra parameters, such as headers or timeouts. For more information, see Request options.

ct
type: CancellationToken
default: default

Cancellation token to cancel the request.

Examples

1
2
3
4
5
6
using Algolia.Search.Clients;
using Algolia.Search.Models.Search;

var client = new SearchClient(new SearchConfig("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY"));

var response = await client.WaitForTaskAsync("ALGOLIA_INDEX_NAME", 123L);
Did you find this page helpful?