IdField method is deleted in NEST 2.0 / ElasticSearch.NET. I cannot find documentation on how to do it in fluent Api now, in 2.0. Any suggestions? Thank you.
Id inference in NEST 2.x is very similar to NEST 1.x. There are essentially three ways:
Id
on the POCO. For example, imagine you have the following type that you index into Elasticsearch
class MyDTO
{
public Guid Id { get; set; }
public string Name { get; set; }
public string OtherName { get; set; }
}
NEST will look to find a property named Id
on the POCO and use the value of this property for the id of the document
ElasticsearchType
attributeYou can attribute the POCO with ElasticsearchType
and use the IdProperty
property to signal to NEST to use a different property for the document id. For example,
[ElasticsearchType(IdProperty = nameof(Name))]
class MyOtherDTO
{
public Guid Id { get; set; }
public string Name { get; set; }
public string OtherName { get; set; }
}
This will use the Name
property as the document id
ConnectionSettings
' InferMappingFor<T>
methodYou can configure the ConnectionSettings
to infer the id property for a given type
var connectionSettings = new ConnectionSettings(new Uri("http://localhost:9200"))
.InferMappingFor<MyOtherDTO>(m => m
.IdProperty(p => p.OtherName)
);
This will use the OtherName
property as the document id.
This is cached per ConnectionSettings
instance so if it is unchanging, I would recommend configuring the settings at startup and using them for the lifetime of the application (you can also use ElasticClient
as a singleton too; it's threadsafe).
With InferMappingFor<T>
you can also set the index name and type name for a CLR type, as well as rename any properties (i.e. map a POCO property to a different property name in ES mapping) and ignore POCO properties.
The order of precedence for id inference is:
InferMappingFor<T>()
IdProperty()
on ConnectionSettings
ElasticsearchTypeAttribute
on POCOId
on the POCOIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With