ElasticSearch - Auto mapping

2019-08-19 16:33发布

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.

1条回答
劳资没心,怎么记你
2楼-- · 2019-08-19 16:47

There are essentially four ways to map C# POCO properties to fields of a document within Elasticsearch:

  1. Using Automap to infer Elasticsearch field types from POCO property types
  2. Using Attributes to explicitly control how property types map to field types
  3. Using fluent mapping to control how property types map to field types
  4. Applying mapping conventions through implementing a visitor

The four ways can be combined to provide flexibility in how you map; fluent mapping using Properties() will take precedence over all others.

When would you use attributes over inferred mapping?

When you want to map slightly differently to the mapping that would be inferred. Take your example

[Number(NumberType.Integer, Name = "age")]
public int Age { get; set; }

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 mapping

[Number(NumberType.Integer, Name = "age", Coerce = false, IgnoreMalformed = true, IncludeInAll = false)]
public int Age { get; set; }

When would you use fluent mapping over attributes?

When 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

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?

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.

查看更多
登录 后发表回答