Before you can index your data or search your Algolia indices,
you must initialize a search client with your application ID and API key.
You can find both in your Algolia account.
The search client handles authentication and lets you manage your indices,
for example, add data to them, or search them.
Report incorrect code
Copy
SearchClient client = new SearchClient("ALGOLIA_APPLICATION_ID", "ALGOLIA_API_KEY");SearchIndex index = client.InitIndex("ALGOLIA_INDEX_NAME");
Replace ALGOLIA_INDEX_NAME with the name of the index you want to use.
You can find your existing indices in the Algolia dashboard
or using the List indices operation.
If the index doesn’t exist, a new, empty index is created locally.
It’s created on Algolia’s servers only if you add records to the index.
Don’t use sensitive or personally identifiable information as your index name,
including usernames, IDs, or email addresses.
Index names are publicly shared.
Methods that are scoped to your Algolia applications (methods of a client object):
Managing indices
Working with dictionaries
Working with API keys
Methods that are scoped to an index are:
Search, indexing, settings, synonyms, and rules methods
The Recommend, Personalization, Insights, and A/B testing APIs have their own clients:
The API client uses Json.NET as the serializer for your POCOs. You can index your POCOs directly if they follow the .NET naming convention.Example:
C#
Report incorrect code
Copy
using Algolia.Search.Clients;using Algolia.Search.Models.Search;using System.Collections.Generic;public class Contact{ public string ObjectID { get; set; } public string Name { get; set; } public int Age { get; set; }}public class AlgoliaIntegration{ private SearchClient client; private SearchIndex index; public AlgoliaIntegrationService(string applicationId, string apiKey) { client = new SearchClient(applicationId, apiKey); index = client.InitIndex("contact"); } public void SaveContacts(IEnumerable<Contact> contacts) { // Ensure that contacts is not null and has data if (contacts != null) { index.SaveObjects(contacts); } } public Contact GetContact(string objectId) { try { Contact contact = index.GetObject<Contact>(objectId); return contact; } catch (Algolia.Search.Exceptions.AlgoliaApiException ex) { // Handle exceptions from the Algolia API here // Log the exception message: ex.Message return null; } } public IEnumerable<Contact> SearchForContact(string queryText) { try { var result = index.Search<Contact>(new Query(queryText)); return result.Hits; } catch (Algolia.Search.Exceptions.AlgoliaApiException ex) { // Handle exceptions from the Algolia API here // Log the exception message: ex.Message return new List<Contact>(); } }}// Usage:// AlgoliaIntegration algoliaService = new AlgoliaIntegration("YourApplicationID", "YourWriteAPIKey");// IEnumerable<Contact> contacts = ... // Fetch from DB or a Json file// algoliaService.SaveContacts(contacts);// Contact contact = algoliaService.GetContact("myId");// IEnumerable<Contact> searchResult = algoliaService.SearchForContact("contact");
You can override the naming strategy with the following attribute:
[JsonProperty(PropertyName = "propertyName")]
You can also add and retrieve records via JObject:
C#
Report incorrect code
Copy
using (StreamReader re = File.OpenText("contacts.json"))using (JsonTextReader reader = new JsonTextReader(re)){ JArray batch = JArray.Load(reader); index.SaveObjects(batch).Wait();}// Retrieve one JObject ContactJObject contact = index.GetObject<JObject>("myId");
Algolia objects such as Rule, Synonym, or Settings are now typed.An example with the Settings class:
C#
Report incorrect code
Copy
IndexSettings settings = new IndexSettings{ SearchableAttributes = new List<string> {"attribute1", "attribute2"}, AttributesForFaceting = new List<string> {"filterOnly(attribute2)"}, UnretrievableAttributes = new List<string> {"attribute1", "attribute2"}, AttributesToRetrieve = new List<string> {"attribute3", "attribute4"} // etc.};index.SetSettings(settings);
If you want to access all properties of the SearchResponse object,
you must add these properties to your class.
For example, to get the _rankingInfo attribute with your search:
C#
Report incorrect code
Copy
public class Contact{ public string ObjectID { get; set; } public string Name { get; set; } public int Age { get; set; } public object _rankingInfo { get; set; }}
The API client uses the built-in HttpClient of the .NET Framework.The HttpClient is wrapped in an interface: IHttpRequester.
If you want to use a custom HttpClient,
you can pass it to the constructors of SearchClient,
AnalyticsClient, or InsightsClient.For example:
C#
Report incorrect code
Copy
IHttpRequester myCustomHttpClient = new MyCustomHttpClient();SearchClient client = new SearchClient( new SearchConfig("YourApplicationID", "YourWriteAPIKey"), myCustomHttpClient);