Csharp

Indexing

Add Objects - AddObjects

Each entry in an index has a unique identifier called objectID.

There are two ways to add an entry to the index:

  1. Supplying an objectID.
    1. If the objectID does not exist in the index, the record will be created
    2. If the objectID already exists the record will be replaced
  2. Not supplying an objectID. Algolia will automatically assign an objectID and you will be able to access it in the response.

Using your own unique IDs when creating records is a good way to make future updates easier without having to keep track of Algolia’s generated IDs. The value you provide for objectIDs can be an integer or a string.

You don’t need to explicitly create an index, it will be automatically created the first time you add an object. Objects are schema less so you don’t need any configuration to start indexing. If you wish to configure things, the settings section provides details about advanced settings.

Example with automatic objectID assignments:

List<JObject> objs = new List<JObject>();
objs.Add(JObject.Parse(@"{""firstname"":""Jimmie"",
                          ""lastname"":""Barninger""}"));
objs.Add(JObject.Parse(@"{""firstname"":""Warren"",
                          ""lastname"":""Speach""}"));
var res = index.AddObjects(objs);
// Asynchronous
// var res = await index.AddObjectsAsync(objs);
System.Diagnostics.Debug.WriteLine(res);

Example with manual objectID assignments:

List<JObject> objs = new List<JObject>();
objs.Add(JObject.Parse(@"{""objectID"":""1"",
                          ""firstname"":""Jimmie"",
                          ""lastname"":""Barninger""}"));
objs.Add(JObject.Parse(@"{""objectID"":""2"",
                          ""firstname"":""Warren"",
                          ""lastname"":""Speach""}"));
var res = index.AddObjects(objs);
// Asynchronous
// var res = await index.AddObjectsAsync(objs);
System.Diagnostics.Debug.WriteLine(res);

To add a single object, use the following method:

var res = index.AddObject(JObject.Parse(@"{""firstname"":""Jimmie"",
                                           ""lastname"":""Barninger""}"), "myID");
// Asynchronous
// var res = await index.AddObjectAsync(JObject.Parse(@"{""firstname"":""Jimmie"",
//                                                       ""lastname"":""Barninger""}"), "myID");
System.Diagnostics.Debug.WriteLine("objectID=" + res["objectID"]);

Update objects - SaveObjects

You have three options when updating an existing object:

  1. Replace all its attributes.
  2. Replace only some attributes.
  3. Apply an operation to some attributes.

Each record needs to contain the objectID key.

Examples:

To replace all attributes existing objects:

List<JObject> objs = new List<JObject>();
objs.Add(JObject.Parse(@"{""firstname"":""Jimmie"",
                          ""lastname"":""Barninger"",
                          ""objectID"":""myID1""}"));
objs.Add(JObject.Parse(@"{""firstname"":""Warren"",
                          ""lastname"":""Speach"",
                          ""objectID"": ""myID2""}"));
var res = index.SaveObjects(objs);
// Asynchronous
var res = await index.SaveObjectsAsync(objs);
System.Diagnostics.Debug.WriteLine(res);

To update a single object, you can use the following method:

index.SaveObject(JObject.Parse(@"{""firstname"":""Jimmie"",
                                  ""lastname"":""Barninger"",
                                  ""city"":""New York"",
                                  ""objectID"":""myID""}"));
// Asynchronous
// await index.SaveObjectAsync(JObject.Parse(@"{""firstname"":""Jimmie"",
//                                              ""lastname"":""Barninger"",
//                                              ""city"":""New York"",
//                                              ""objectID"":""myID""}"));

Partial update objects - PartialUpdateObjects

You have many ways to update an object’s attributes:

  1. Set the attribute value
  2. Add a string or number element to an array
  3. Remove an element from an array
  4. Add a string or number element to an array if it doesn’t exist
  5. Increment an attribute
  6. Decrement an attribute

Example to update only the city attribute of an existing object:

index.PartialUpdateObject(JObject.Parse(@"{""city"":""San Francisco"",
                                           ""objectID"":""myID""}"));
// Asynchronous
// await index.PartialUpdateObjectAsync(JObject.Parse(@"{""city"":""San Francisco"",
//                                                       ""objectID"":""myID""}"));

// You can avoid the automatic creation of the object by specifying false for the second parameter:
index.PartialUpdateObject(JObject.Parse(@"{""city"":""San Francisco"",
                                           ""objectID"":""myID""}"), false);

Example to add a tag:

index.PartialUpdateObject(JObject.Parse(@"{""_tags"":{""value"": ""MyTag"", ""_operation"": ""Add"" },
                              ""objectID"":""myID""}"));
// Asynchronous
// await index.PartialUpdateObjectAsync(JObject.Parse(@"{""_tags"":{""value"": ""MyTag"", ""_operation"": ""Add"" },
//                                                       ""objectID"":""myID""}"));

Example to remove a tag:

index.PartialUpdateObject(JObject.Parse(@"{""_tags"":{""value"": ""MyTag"", ""_operation"": ""Remove"" },
                              ""objectID"":""myID""}"));
// Asynchronous
// await index.PartialUpdateObjectAsync(JObject.Parse(@"{""_tags"":{""value"": ""MyTag"", ""_operation"": ""Remove"" },
//                                                       ""objectID"":""myID""}"));

Example to add a tag if it doesn’t exist:

index.PartialUpdateObject(JObject.Parse(@"{""_tags"":{""value"": ""MyTag"", ""_operation"": ""AddUnique"" },
                              ""objectID"":""myID""}"));
// Asynchronous
// await index.PartialUpdateObjectAsync(JObject.Parse(@"{""_tags"":{""value"": ""MyTag"", ""_operation"": ""AddUnique"" },
//                                                       ""objectID"":""myID""}"));

Example to increment a numeric value:

index.PartialUpdateObject(JObject.Parse(@"{""price"":{""value"": 42, ""_operation"": ""Increment"" },
                              ""objectID"":""myID""}"));
// Asynchronous
// await index.PartialUpdateObjectAsync(JObject.Parse(@"{""price"":{""value"": 42, ""_operation"": ""Increment"" },
//                                                       ""objectID"":""myID""}"));

Note: Here we are incrementing the value by 42. To increment just by one, put value:1.

Example to decrement a numeric value:

index.PartialUpdateObject(JObject.Parse(@"{""price"":{""value"": 42, ""_operation"": ""Decrement"" },
                              ""objectID"":""myID""}"));
// Asynchronous
// await index.PartialUpdateObjectAsync(JObject.Parse(@"{""price"":{""value"": 42, ""_operation"": ""Decrement"" },
//                                                       ""objectID"":""myID""}"));

Note: Here we are decrementing the value by 42. To decrement just by one, put value:1.

To partial update multiple objects using one API call, you can use the following method:

List<JObject> objs = new List<JObject>();
objs.Add(JObject.Parse(@"{""firstname"":""Jimmie"",
                          ""objectID"":""myID1""}"));
objs.Add(JObject.Parse(@"{""firstname"":""Warren"",
                          ""objectID"": ""myID2""}"));
var res = index.PartialUpdateObjects(objs);
// Asynchronous
// var res = await index.PartialUpdateObjectsAsync(objs);
System.Diagnostics.Debug.WriteLine(res);

Delete objects - DeleteObjects

You can delete objects using their objectID:

List<string> ids = new List<string>();
ids.Add(@"myID1");
ids.Add(@"myID2");
var res = index.DeleteObjects(ids);
// Asynchronous
// var res = await index.DeleteObjectsAsync(ids);
System.Diagnostics.Debug.WriteLine(res);

To delete a single object, you can use the following method:

index.DeleteObject("myID");
// Asynchronous
// await index.DeleteObjectAsync("myID");

Delete by query - deleteByQuery

The “delete by query” helper deletes all objects matching a query. Internally, the API client will browse the index (as in Backup / Export an index), delete all matching hits, and wait until all deletion tasks have been applied.

Be careful when using this method. Calling it with an empty query will result in cleaning the index of all its records.

Query query = /* any browse-compatible query parameters */;
index.DeleteByQuery(query);
// Asynchronous
// await index.DeleteByQueryAsync(query);

Wait for operations - WaitTask

All write operations in Algolia are asynchronous by design.

It means that when you add or update an object to your index, our servers will reply to your request with a taskID as soon as they understood the write operation.

The actual insert and indexing will be done after replying to your code.

You can wait for a task to complete using the waitTask method on the taskID returned by a write operation.

For example, to wait for indexing of a new object:

var res = index.AddObject(JObject.Parse(@"{""firstname"":""Jimmie"",
                                           ""lastname"":""Barninger""}"), "myID");
// Asynchronous
// var res = await index.AddObjectAsync(JObject.Parse(@"{""firstname"":""Jimmie"",
//                                                       ""lastname"":""Barninger""}"), "myID");
index.WaitTask(res["taskID"].ToString());
// Asynchronous
// await index.WaitTaskAsync(res["taskID"].ToString());

If you want to ensure multiple objects have been indexed, you only need to check the biggest taskID.

Did you find this page helpful?

We're always looking for advice to help improve our documentation! Please let us know what's working (or what's not!) - we're constantly iterating thanks to the feedback we receive.

Send us your suggestions!