ElasticSearch-自动映射 [英] ElasticSearch - Auto mapping

查看:157
本文介绍了ElasticSearch-自动映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以最近我开始研究ES,并考虑将当前的ElasticSearch.Net和Nest版本从1.x升级到5.x.我注意到一些变化.

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; }

我想知道是否确实需要该属性,因为我在文档中读到在v5.x中我们具有自动映射功能.这会自动将ES中的所有字段映射到.Net中的属性吗?

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?

我们何时真正需要映射?是仅在创建新类型时还是从ES或同时从两者中获取数据时需要?

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.

推荐答案

基本上有

  • 使用自动映射从POCO属性类型推断 Elasticsearch字段类型
  • 使用属性 以明确控制属性类型如何映射到字段类型
  • 使用流利的映射 以控制属性类型如何映射到字段类型
  • 通过实现访问者来应用映射约定
  • 可以将这四种方式结合起来,以灵活地绘制地图;使用Properties()进行流利的映射将优先于所有其他映射.

    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; }
    

    在这里,属性映射所应用的映射与推断的完全相同,因此在这种情况下,它将是多余的.相反,假设我们不希望将值强制转换为数字,不希望忽略格式错误的值,也不想在_all字段中包含值;我们可以通过属性映射来实现

    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?

    当您不喜欢使用属性进行映射时,或者希望以无法通过属性映射表达的方式来映射POCO时,例如,

    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

    我们何时真正需要映射?是仅在创建新类型时还是从ES或同时从两者中获取数据时需要?

    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?

    在将第一个文档编入索引之前,需要将映射添加到索引.如果您在索引第一个文档之前未添加映射,则默认情况下,Elasticsearch将使用其自己的推论从其看到的第一个文档推论该模式.

    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.

    这篇关于ElasticSearch-自动映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆