Search code examples
azureazure-storageazure-blob-storage

Azure Search: Cache ServiceClient without depending on index name in v11


I currently use v10 of the Azure Search APIs and create a static variable like so:

private static SearchServiceClient SearchServiceClient = new SearchServiceClient(searchServiceName, credentials);

On the server side, this variable is re-used between requests so I don't have to initialize it over and over. I got the idea from https://learn.microsoft.com/en-us/azure/azure-functions/manage-connections to improve performance.

Now, v11 has completely different data types. The new SearchClient type's constructor expects an index name as a parameter. I have many indexes and I'd like to avoid creating a static variable for each index,

In v11, is it possible to re-use a search client in the way I used to be able to do?


Solution

  • In v11, the SearchServiceClient is actually split into 3 different clients: SearchClient, SearchIndexClient, SearchIndexerClient, each of them has different usage. You can see here for the details.

    So when you use SearchClient, it is defined to have an index name parameter. You can not re-use it like what SearchServiceClient does in v10.

    But you can do it like below:

    SearchIndexClient adminClient = new SearchIndexClient(serviceEndpoint, credential); 
    
    SearchClient ingesterClient = adminClient.GetSearchClient(indexName);