elasticsearch index 的 type 解释

本文解释了Elasticsearch中类型的运作方式。类型是Elasticsearch为了区分不同文档而引入的概念,在底层Lucene中并不存在类型的概念。文章还介绍了如何通过指定类型来限定字段查询,以及不同类型间相同字段名但不同映射类型的做法并不推荐。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Index names are different indices. Types are just syntactic sugar to add separation between types of documents. If you know Lucene, type is just a field on a doc.


Let me explain a bit how types work in elasticsearch. At the end of the day, Lucene index has Documents and has no notion of types. Types are something added (as best as possible) by elasticsearch. A lucene document is a flat structure key value pair (so objects in json are also translated to this), and a type ends up as another field within the document (called _type).


Then you build a query and in the field (any query that acces a field to execute on), you can specify type1.field1, and in this case, it will be automatically detected, and the query will be wrapped to only be executed match on docs with _type:type1 (in an efficient manner), on field1.


This does mean that across types, having the same field name with different mappings types (for example, type1 has field1 of type numeric, and type2 also has field1 but of type string) should not be done (working on automatically detecting that...).

So, the type prefix when you define the field name should allow to only search on a field against that specific type. (At the end of the day, it is wrapped in a filtered query with a _type:type1 term filter)

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 中的 typeindex pattern 有何区别?** 3. **在实际应用中,何时应该创建新的 type?** 请注意,上述内容适用于当前主流的 Elasticsearch 使用习惯,并可能随版本更新有所变化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值