Home > Asp.net > Refresh datasource, reset and run indexer using .NET Core and RestSharp

Refresh datasource, reset and run indexer using .NET Core and RestSharp

This post was most recently updated on July 19th, 2024

In this article, we are going to demonstrate the steps to refresh the data source, reset and run indexer in an azure cognitive search using .Net Core and RestSharp. Since we are going to refresh the data source and the data source here we are referring to is of type Azure Blob Storage, we need to have access to an Azure Storage Account. Incase we are referring to the type Azure SQL Database, then we would require to have access to the SQL database. Also we need to keep in hand the Primary API Key of the Azure cognitive search service.

Install following NuGet packages in your project:

1. Install-Package Azure.Storage.Blobs -Version 12.6.0


2. Install-Package RestSharp -Version 106.12.0

Add following namespaces:

1. using Azure.Storage.Blobs;

2. using RestSharp;

Add the connection and the required parameters in appsettings.json:


  • Create a model class named as ” AppSettingsModel“:



  • Register the mentioned class in Startup.cs:



  • To refresh the data source from code:

    -> Establish the connection with the data source in the search service and create the client

    -> Call the request method

    -> Add required headers

    -> Add required parameters

    -> Check the response (in case  of refreshing the data source the status code of response should be 204 and status description as ‘No Content’)






  • To reset the indexer from code:

    -> Create the client

    -> Call the request method

    -> Add required headers

    -> Check the response (in case  of resetting the indexer the status code of response should be 204 and status description as ‘No Content’)


  • To run the indexer from code:

    -> Create the client

    -> Call the request method

    -> Add required headers

    -> Check the response (in case  of running the indexer the status code of response should be 202 and status description as ‘Accepted’)



  • Create a separate model for the creating the body of the request:



    <pre class=”theme:vs2012-black lang:default decode:true”> public class CognitiveSearchDataSource
    {
    public string name { get; set; }
    public string type { get; set; }
    public Credentials credentials { get; set; }
    public Container container { get; set; }
    public DataDeletionDetectionPolicy dataDeletionDetectionPolicy { get; set; }
    }
    public class Credentials
    {
    public string connectionString { get; set; }
    }

    public class Container
    {
    public string name { get; set; }
    public object query { get; set; }
    }

    public class DataDeletionDetectionPolicy
    {
    [JsonPropertyName(“@odata.type”)]
    public string OdataType { get; set; }
    }</pre >



  • Please find the entire code below:

    <pre class=”theme:vs2012-black lang:c# decode:true “> public void RefreshCoursesCognitiveSearch()
    {
    //refresh blob using restsharp
    Console.WriteLine(“Started refreshing Courses blob data source ” + DateTime.Now.ToString());
    var client = new RestClient(_appSetting.CognitiveSearchBaseURL + “/datasources/” + _appSetting.CognitiveSearchCourseDataSourceName + “?api-version=” + _appSetting.CognitiveSearchAPIVersion);//new RestClient(“https://onenh-cognitve-search-dev.search.windows.net/datasources/azureblob-courses-bs-test?api-version=2020-06-30-Preview”);
    client.Timeout = -1;
    var request = new RestRequest(Method.PUT);
    request.AddHeader(“api-key”, _appSetting.CognitiveSearchAPIKey);
    request.AddHeader(“Content-Type”, “application/json”);
    CognitiveSearchDataSource searchDataSource = new CognitiveSearchDataSource();
    Container container = new Container();
    Credentials credentials = new Credentials();
    DataDeletionDetectionPolicy dataDeletionDetectionPolicy = new DataDeletionDetectionPolicy();
    dataDeletionDetectionPolicy.OdataType = “#Microsoft.Azure.Search.NativeBlobSoftDeleteDeletionDetectionPolicy”;
    searchDataSource.name = _appSetting.CognitiveSearchCourseDataSourceName;
    container.name = _appSetting.CourseListContainer;
    credentials.connectionString = _appSetting.AzureStorageConnectionString;
    searchDataSource.container = container;
    searchDataSource.type = “azureblob”;
    searchDataSource.dataDeletionDetectionPolicy = dataDeletionDetectionPolicy;
    searchDataSource.credentials = credentials;
    var serializeOptions = new JsonSerializerOptions
    {
    WriteIndented = true
    };
    request.AddParameter(“application/json”, System.Text.Json.JsonSerializer.Serialize(searchDataSource, serializeOptions), ParameterType.RequestBody);
    IRestResponse response = client.Execute(request);
    if (response.StatusDescription == “No Content”)
    {
    Console.WriteLine(“Completed refreshing courses blob data source ” + DateTime.Now.ToString());
    }
    else
    {
    Console.WriteLine(“Error while calling the API To refresh blob data source ” + client.BaseUrl + ” status code:” + response.StatusCode.ToString() + ” Error:” + response.Content.ToString());
    }
    Console.WriteLine(“Started reseting course indexer ” + DateTime.Now.ToString());
    var indexerResetClient = new RestClient(_appSetting.CognitiveSearchBaseURL + “/indexers/” + _appSetting.CognitiveSearchCourseIndexerName + “/reset?api-version=” + _appSetting.CognitiveSearchAPIVersion);//new RestClient(“https://onenh-cognitve-search-dev.search.windows.net/indexers/course-sg-indexer-test/reset?api-version=2020-06-30-Preview”);
    indexerResetClient.Timeout = -1;
    var indexerResetRequest = new RestRequest(Method.POST);
    indexerResetRequest.AddHeader(“api-key”, _appSetting.CognitiveSearchAPIKey);
    IRestResponse indexerResetResponse = indexerResetClient.Execute(indexerResetRequest);
    if (indexerResetResponse.StatusDescription == “No Content”)
    {
    Console.WriteLine(“Completed reseting course indexer ” + DateTime.Now.ToString());
    }
    else
    {
    Console.WriteLine(“Error while calling the API To Reset Indexer ” + indexerResetClient.BaseUrl + ” StatusCode:” + indexerResetResponse.StatusCode.ToString() + ” Error:” + indexerResetResponse.Content.ToString());
    }

    Console.WriteLine(“Started running course indexer ” + DateTime.Now.ToString());
    var indexerRunClient = new RestClient(_appSetting.CognitiveSearchBaseURL + “/indexers/” + _appSetting.CognitiveSearchCourseIndexerName + “/run?api-version=” + _appSetting.CognitiveSearchAPIVersion);//new RestClient(“https://onenh-cognitve-search-dev.search.windows.net/indexers/course-sg-indexer-test/run?api-version=2020-06-30-Preview”);
    indexerRunClient.Timeout = -1;
    var indexerRunRequest = new RestRequest(Method.POST);
    indexerRunRequest.AddHeader(“api-key”, _appSetting.CognitiveSearchAPIKey);
    IRestResponse indexerRunResponse = indexerRunClient.Execute(indexerRunRequest);
    if (indexerRunResponse.StatusDescription == “Accepted”)
    {
    Console.WriteLine(“Completed running course indexer ” + DateTime.Now.ToString());
    }
    else
    {
    Console.WriteLine(“Error while calling the API To Run Indexer ” + indexerRunClient.BaseUrl + ” StatusCode:” + indexerRunResponse.StatusCode.ToString() + ” Error:” + indexerRunResponse.Content.ToString());
    }
    Console.WriteLine(“Completed refreshing course cognitive service ” + DateTime.Now.ToString());

    }</pre>

This Article is TAGGED in , , , . BOOKMARK THE permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">