Azure Cognitive Search: How to use AddSearchClient<TBuilder,TConfiguration>(TBuilder, TConfiguration)

huangapple go评论82阅读模式
英文:

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 =&gt;
{
    b.AddSearchClient(
        new Uri(&quot;https://&quot; + searchServiceConfiguration.Host),
        searchServiceConfiguration.IndexName,
        new AzureKeyCredential(searchServiceConfiguration.QueryApiKey));
    b.AddSearchIndexClient(new Uri(&quot;https://&quot; + searchServiceConfiguration.Host), new AzureKeyCredential(searchServiceConfiguration.AdminApiKey));
});

But there is an overload, which takes configuration object: AddSearchClient&lt;TBuilder,TConfiguration&gt;(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 =&gt;
    {
        builder.AddSearchClient(new Uri(&quot;http://my.search.com&quot;), &quot;indexName&quot;, new AzureKeyCredential(&quot;ApiKey&quot;))
        .ConfigureOptions((options) =&gt;
        {
            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.

huangapple
  • 本文由 发表于 2023年6月30日 00:27:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定