Csharp

Manage Indices

Create an index

To create an index, you need to perform any indexing operation like:

  • set settings
  • add object

List indices - listIndexes

You can list all your indices along with their associated information (number of entries, disk size, etc.) with the listIndexes method:

var result = client.ListIndexes();
// Asynchronous
// var result = await client.ListIndexesAsync();
System.Diagnostics.Debug.WriteLine(res);

Delete an index - deleteIndex

You can delete an index using its name:

client.DeleteIndex("contacts");
// Asynchronous
// await client.DeleteIndexAsync("contacts");

Clear an index - ClearIndex

You can delete the index contents without removing settings and index specific API keys by using the clearIndex command:

index.ClearIndex();
// Asynchronous
// await index.ClearIndexAsync();

Copy index - CopyIndex

You can copy an existing index using the copy command.

Warning: The copy command will overwrite the destination index.

// Copy MyIndex in MyIndexCopy
client.CopyIndex("MyIndex", "MyIndexCopy");
// Asynchronous
await client.CopyIndexAsync("MyIndex", "MyIndexCopy");

Move index - MoveIndex

In some cases, you may want to totally reindex all your data. In order to keep your existing service running while re-importing your data we recommend the usage of a temporary index plus an atomical move using the MoveIndex method.

// Rename MyNewIndex in MyIndex (and overwrite it)
client.MoveIndex("MyNewIndex", "MyIndex");
// Asynchronous
// await client.MoveIndexAsync("MyNewIndex", "MyIndex");

Note:

The MoveIndex method will overwrite the destination index, and delete the temporary index.

Warning

The MoveIndex operation will override all settings of the destination, There is one exception for the replicas parameter which is not impacted.

For example, if you want to fully update your index MyIndex every night, we recommend the following process:

  1. Get settings and synonyms from the old index using Get settings and Get synonym.
  2. Apply settings and synonyms to the temporary index MyTmpIndex, (this will create the MyTmpIndex index) using Set settings and Batch synonyms (make sure to remove the replicas parameter from the settings if it exists).
  3. Import your records into a new index using Add Objects.
  4. Atomically replace the index MyIndex with the content and settings of the index MyTmpIndex using the Move index method. This will automatically override the old index without any downtime on the search.
  5. You’ll end up with only one index called MyIndex, that contains the records and settings pushed to MyTmpIndex and the replica-indices that were initially attached to MyIndex will be in sync with the new data.