elasticsearch 使用多个索引嵌套ElasticClient以索引文档
起初,我有1个索引,我的elasticclient在startup.cs中的设置如下所示
elasticsearch 使用多个索引嵌套ElasticClient以索引文档,
elasticsearch,nest,
elasticsearch,Nest,起初,我有1个索引,我的elasticclient在startup.cs中的设置如下所示 public static IServiceCollection AddElasticClient(this IServiceCollection services) { var elasticSettings = services.BuildServiceProvider().GetService<IOptions<ElasticSettings>>().Value;
public static IServiceCollection AddElasticClient(this IServiceCollection services)
{
var elasticSettings = services.BuildServiceProvider().GetService<IOptions<ElasticSettings>>().Value;
var settings = new ConnectionSettings(new Uri(elasticSettings.Uri));
settings
.ThrowExceptions(elasticSettings.ThrowExceptions)
.PrettyJson(elasticSettings.PrettyJson)
.DefaultIndex(elasticSettings.Index)
.BasicAuthentication(elasticSettings.Username, elasticSettings.Password)
.DefaultMappingFor<CorrelationContext>(ms => ms.Ignore(p => p.DgpHeader));
var client = new ElasticClient(settings);
services.AddSingleton<IElasticClient>(client);
return services;
}
现在有了一个新的要求,他们可以根据这个要求提供要写入的索引的名称。
不同索引的所有身份验证数据都是通过配置设置提供的,所以我在启动时就有了所有可用的数据。
文档类型始终相同
我找到了在查询时指定索引而不是索引时指定索引的示例
我能否在ElasticClient中提供多个索引,并在执行IndexDocument时指定索引?
还是每个索引都需要一个单独的客户端
如果是后者,我是否仍然可以使用DI将客户机注入到我的编写器中,或者我必须当场创建一个
Thx
我正在使用Nest 7.6.1,而不是使用
索引文档
,您可以使用索引同步
方法来控制其他请求参数
var indexResponse = await _elasticClient.IndexAsync(doc, descriptor => descriptor.Index("other"));
IndexDocument
是一种包装方法,它向客户端隐藏了索引文档的复杂性
请求身份验证配置
var indexResponse = await _elasticClient.IndexAsync(doc,
descriptor => descriptor
.Index("other")
.RequestConfiguration(rq => rq.BasicAuthentication("user", "pass")));
我需要为每个索引提供凭据(它们都不同)。但我似乎不能这样做。或者我遗漏了什么?可以通过
RequestConfiguration
,查看我的更新。太好了。今天晚些时候我会试试的。优先权转移:-(
var indexResponse = await _elasticClient.IndexAsync(doc,
descriptor => descriptor
.Index("other")
.RequestConfiguration(rq => rq.BasicAuthentication("user", "pass")));