英文:
Azure Cognitive Search: How to use AddSearchClient<TBuilder,TConfiguration>(TBuilder, TConfiguration)
问题
在Microsoft.Extensions.Azure
命名空间中有一个扩展方法,用于方便地注册客户端。它运行良好。
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAzureClients(b =>
{
b.AddSearchClient(
new Uri("https://" + searchServiceConfiguration.Host),
searchServiceConfiguration.IndexName,
new AzureKeyCredential(searchServiceConfiguration.QueryApiKey));
b.AddSearchIndexClient(new Uri("https://" + searchServiceConfiguration.Host), new AzureKeyCredential(searchServiceConfiguration.AdminApiKey));
});
但是有一个重载方法,它接受配置对象:AddSearchClient<TBuilder, TConfiguration>(TBuilder, TConfiguration)
。
我需要配置SearchClientOptions
,像这样:
private readonly SearchClientOptions _options = new()
{
Serializer = new JsonObjectSerializer(
new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
})
};
我想知道是否可以在注册SearchClient时使用这些选项(以及其他属性:密钥、URI、索引名称)。到目前为止,我尝试创建所需的配置对象,但没有成功。也许有人有一个想法如何使用提到的重载方法,以及是否允许传递选项?
我尝试找出TConfiguration对象应该是什么样子,但没有成功。
英文:
There is an extension method located in Microsoft.Extensions.Azure
namespace for ease registering clients. It works nicely.
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAzureClients(b =>
{
b.AddSearchClient(
new Uri("https://" + searchServiceConfiguration.Host),
searchServiceConfiguration.IndexName,
new AzureKeyCredential(searchServiceConfiguration.QueryApiKey));
b.AddSearchIndexClient(new Uri("https://" + searchServiceConfiguration.Host), new AzureKeyCredential(searchServiceConfiguration.AdminApiKey));
});
But there is an overload, which takes configuration object: AddSearchClient<TBuilder,TConfiguration>(TBuilder, TConfiguration)
.
I need to configure SearchClientOptions
like this
private readonly SearchClientOptions _options = new()
{
Serializer = new JsonObjectSerializer(
new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
})
};
I'm wondering if I can configure a client with these options (and other props: key, uri, index name) when registering SearchClient. So far, no luck with creating required configuration object. Maybe somebody have an idea how to use mentioned overload and if this allow to pass options?
I tried to find out how TConfiguration object should look like, but no success.
答案1
得分: 1
这是如何配置 SearchClientOptions
的示例代码:
public void ConfigureServices(IServiceCollection services)
{
services.AddAzureClients(builder =>
{
builder.AddSearchClient(new Uri("http://my.search.com"), "indexName", new AzureKeyCredential("ApiKey"))
.ConfigureOptions((options) =>
{
options.Serializer = new JsonObjectSerializer(
new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
options.Retry.MaxRetries = 10;
});
});
}
供参考,这是 Microsoft.Extensions.Azure 文档,你可以在其中找到如何使用 AddAzureClients
并配置选项的信息。
英文:
This is how you can configure the SearchClientOptions
:
public void ConfigureServices(IServiceCollection services)
{
services.AddAzureClients(builder =>
{
builder.AddSearchClient(new Uri("http://my.search.com"), "indexName", new AzureKeyCredential("ApiKey"))
.ConfigureOptions((options) =>
{
options.Serializer = new JsonObjectSerializer(
new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
options.Retry.MaxRetries = 10;
});
});
}
For reference, here is the Microsoft.Extensions.Azure documentation, where you can find how to use AddAzureClients
and configure options.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论