Azure 搜索:缓存 ServiceClient 而不依赖于 v11 中的索引名称

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

我目前使用 Azure 搜索 API 的 v10 并创建一个静态变量,如下所示:

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

在服务器端,这个变量在请求之间重复使用,所以我不必一遍又一遍地初始化它。我从 https://docs.microsoft.com/en-us/azure/azure-functions/manage-connections 得到提高性能的想法。

现在,v11 具有完全不同的数据类型。新的 SearchClient 类型的 constructor 需要一个索引名称作为参数。我有很多索引,我想避免为每个索引创建一个静态变量,

在 v11 中,是否可以像我以前那样重新使用搜索客户端?

在v11中,SearchServiceClient实际上分为3个不同的客户端:SearchClientSearchIndexClientSearchIndexerClient,每个客户端都有不同的用法。详情可以看here

所以当你使用SearchClient时,它被定义为有一个索引名称参数。您不能像 SearchServiceClient 在 v10.

中那样重复使用它

但是你可以像下面那样做:

SearchIndexClient adminClient = new SearchIndexClient(serviceEndpoint, credential); 

SearchClient ingesterClient = adminClient.GetSearchClient(indexName);