So recently I started working on ES and I thought of upgrading the current ElasticSearch.Net and Nest versions from 1.x to 5.x. I have noticed several changes.
My question is about the auto mapping functionality in the latest version. Earlier I had attributes for each property. Say for example:
[ElasticProperty(Name = "age", Type = FieldType.Integer)]
public int Age { get; set; }
But in the newer version, I could do something like
[Number(NumberType.Integer, Name = "age")]
public int Age { get; set; }
I am wondering if the attribute is actually required because I read in the documentation that in v5.x we have the auto mapping functionality. Would this automatically map all the fields in ES to the properties in .Net?
And when do we actually need the mapping? Is it only while creating a new type or is it required while fetching the data from ES or both?
I hope my question makes sense.
There are essentially four ways to map C# POCO properties to fields of a document within Elasticsearch:
The four ways can be combined to provide flexibility in how you map; fluent mapping using
Properties()
will take precedence over all others.When you want to map slightly differently to the mapping that would be inferred. Take your example
Here, the attribute mapping is applying exactly the same mapping as would be inferred so in this case, it would be superfluous. In contrast, imagine we don't want values to be coerced to numbers, want to ignore malformed values and don't want to include values in the
_all
field; we can achieve this with attribute mappingWhen you don't like to use attributes for mapping or when you wish to map your POCO in a way that cannot be expressed with attribute mapping, for example, multi_fields
You need to add the mapping to an index before you index the first document into the index. If you don't add the mapping before indexing the first document, by default Elasticsearch will use its own inference to infer the schema from the first document it sees.
You can add the mapping at index creation time, or after creating the index but before indexing the first document. The former is usually most common.