首页 > 解决方案 > Azure 搜索:缓存 ServiceClient 而不依赖于 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类型的构造函数需要一个索引名称作为参数。我有很多索引,我想避免为每个索引创建一个静态变量,

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

标签: azureazure-storageazure-blob-storage

解决方案


在 v11 中,SearchServiceClient实际上分为 3 个不同的客户端:SearchClientSearchIndexClientSearchIndexerClient,每个客户端都有不同的用法。您可以在此处查看详细信息。

因此,当您使用 时SearchClient,它被定义为具有索引名称参数。您不能像SearchServiceClientv10 中那样重复使用它。

但你可以这样做:

SearchIndexClient adminClient = new SearchIndexClient(serviceEndpoint, credential); 

SearchClient ingesterClient = adminClient.GetSearchClient(indexName);

推荐阅读