在NEST 2.0 / ElasticSearch.NET中删除了IdField方法。我现在在2.0中找不到如何在流利的Api中做到这一点的文档。有什么建议? 谢谢。
答案 0 :(得分:9)
Id inference in NEST 2.x与NEST 1.x非常相似。基本上有三种方式:
Id
的媒体资源中推断出来。 例如,假设您具有索引到Elasticsearch
的以下类型class MyDTO
{
public Guid Id { get; set; }
public string Name { get; set; }
public string OtherName { get; set; }
}
NEST将在POCO上查找名为Id
的属性,并将此属性的值用于文档的ID
ElasticsearchType
属性 您可以将POCO归为ElasticsearchType
,并使用IdProperty
属性向NEST发送信号,以便为文档ID使用不同的属性。例如,
[ElasticsearchType(IdProperty = nameof(Name))]
class MyOtherDTO
{
public Guid Id { get; set; }
public string Name { get; set; }
public string OtherName { get; set; }
}
这将使用Name
属性作为文档ID
ConnectionSettings
'InferMappingFor<T>
方法 您可以配置ConnectionSettings
以推断给定类型的id属性
var connectionSettings = new ConnectionSettings(new Uri("http://localhost:9200"))
.InferMappingFor<MyOtherDTO>(m => m
.IdProperty(p => p.OtherName)
);
这将使用OtherName
属性作为文档ID。
这是 per ConnectionSettings
实例的缓存,所以如果它不变,我建议在启动时配置设置并在应用程序的生命周期中使用它们(您也可以使用{{ 1}}作为单身人士;它是线程安全的。)
使用ElasticClient
,您还可以设置CLR类型的索引名称和类型名称,以及重命名任何属性(即将POCO属性映射到ES映射中的其他属性名称)并忽略POCO属性。
id推理的优先顺序是:
InferMappingFor<T>
上InferMappingFor<T>()
IdProperty()
ConnectionSettings
关于POCO ElasticsearchTypeAttribute
的媒体中推断ID
醇>