Elastic search 7+为什么去掉了mapping type

本文探讨了Elasticsearch为何移除mapping types概念。主要基于三点:一是type映射中同名字段在内部由同一Lucene字段支持,限制了字段类型的灵活性;二是不同实体在同一个index中导致数据稀疏,影响Lucene的压缩效率;三是ES与SQL数据库的类比误导,表间独立性在ES中不存在。

Why are mapping types being removed?

Initially, we spoke about an “index” being similar to a “database” in an SQL database, and a “type” being equivalent to a “table”.

This was a bad analogy that led to incorrect assumptions. In an SQL database, tables are independent of each other. The columns in one table have no bearing on columns with the same name in another table. This is not the case for fields in a mapping type.

In an Elasticsearch index, fields that have the same name in different mapping types are backed by the same Lucene field internally. In other words, using the example above, the user_namefield in the user type is stored in exactly the same field as the user_name field in the tweet type, and both user_name fields must have the same mapping (definition) in both types.

This can lead to frustration when, for example, you want deleted to be a date field in one type and a boolean field in another type in the same index.

On top of that, storing different entities that have few or no fields in common in the same index leads to sparse data and interferes with Lucene’s ability to compress documents efficiently.

For these reasons, we have decided to remove the concept of mapping types from Elasticsearch.

【以上摘自官网原文】链接

主要意思有3点:

1、我们经常把二维数据库与ES作类比的方式是不正确的假设。把“index”类比为数据库,“type”类比为表。具体原因是,数据库的表是物理独立的,一个表的列跟另外一张表相同名称的列没有关系,而ES中并非如此,不同type映射类型中具有相同名称的字段在内部由相同的Lucene字段支持。

2、当您想要索引一个deleted字段在不同的type中数据类型不一样。一个类型中为日期字段,另外一个类型中为布尔字段时,这可能会导致ES的存储失败,因为这影响了ES的初衷设计。

3、另外,在一个index中建立很多实体,type,没有相同的字段,会导致数据稀疏,最终结果是干扰了Lucene有效压缩文档的能力,说白了就是影响ES的存储、检索效率。

Elasticsearch中,"type" 是用于定义文档在索引内部如何被组织的一个属性名称。它是 Elasticsearch 实现数据分层的重要组成部分。当我们在创建索引入口(索引映射)时,我们可以指定一个或多个 "type" 来描述存储在索引中的不同类别的数据。这种设计使得搜索引擎能够对各种不同结构的数据进行高效处理和搜索。 ### Elasticsearchtype 的作用 1. **数据分类**:允许将相似或相关性质的数据存储在同一索引的不同部分。每个 type 表示索引中一组具有共同字段结构的数据类别。 2. **查询灵活性**:通过类型,查询变得更有针对性和高效。用户可以根据需要检索特定类型的数据,而不影响索引内其他类型的数据。 3. **字段管理**:每个 type 内部可以定义不同的字段以及它们的类型、属性(如分析器、格式化等)。这样的灵活性有助于定制数据的索引和搜索行为。 4. **API 约定**:在旧版本的 Elasticsearch 中,所有请求数据都会使用默认的 type “_doc”。从7.x 版本开始,Elasticsearch 引入了一种更细粒度的方式,允许通过 API 参数指定 type。 5. **过渡到更精细的分隔**:随着 Elasticsearch 的演进,从简单的类型转换到了更精细的“index pattern”和数据模型的概念,类型的作用逐渐转变为引导更高级的索引策略和数据组织逻辑。 ### Elasticsearchtype 的使用变化 从早期版本到最新的 Elasticsearchtype 的使用经历了一些改变: - **7.x 版本及之后**:引入了“index pattern”的概念替代了传统的 type,使得索引布局更加灵活,支持更复杂的多层级数据组织和搜索需求。 - **从文档到数据模型**:随着对数据模型的关注增加,Elasticsearch 开始鼓励用户构建更复杂的数据结构,其中类型的概念逐步淡化,转而强调数据模型和字段层次的设计。 ### 示例 假设在一个博客应用中,你可以有一个名为 “articles”的索引,它包含多个 type,比如:“posts”,专门用于存放文章;“comments”,用于存放评论。 ```json PUT articles/_mapping { "mappings": { "posts": { "properties": { "title": {"type": "text"}, "content": {"type": "text"}, "author_id": {"type": "keyword"} } }, "comments": { "properties": { "text": {"type": "text"}, "post_id": {"type": "integer"}, "user_id": {"type": "keyword"} } } } } ``` 在这个例子中,“posts” 和 “comments” 分别代表了不同类型的数据结构,它们各自拥有独特的字段集合和特性。 --- ### 相关问题: 1. **如何在 Elasticsearch 中创建和管理 type?** 2. **Elasticsearch 中的 type 与 index pattern 有何区别?** 3. **在实际应用中,何时应该创建新的 type?** 请注意,上述内容适用于当前主流的 Elasticsearch 使用习惯,并可能随版本更新有所变化。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java技术之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值