Elasticsearch Mapping Overview
Elasticsearch Mapping Overview,文字内容来自 B 站中华石杉 Elasticsearch 高手进阶课程,英文内容来自官方文档。
Review
To summarize the flow of index requests, this is what happens:
The node accepting the request will be the coordinator. It consults the mappings to determine which shard to send the request to.
The request is sent to the primary of that shard.
The primary writes the operation to its translog, and relays the request to the replicas.
When a sufficient number of replicas have acknowledged, the primary returns success.
The coordinator returns success when all the sub-operations (in e.g. a bulk request) have succeeded.
Mapping
mapping,就是 index 的 type 的元数据,每个 type 都有一个自己的 mapping,决定了数据类型,建立倒排索引的行为,还有进行搜索的行为
Mapping is the process of defining how a document, and the fields it contains, are stored and indexed.
Indices created in Elasticsearch 7.0.0 or later no longer accept a _default_ mapping.
In an Elasticsearch index, fields that have the same name in different mapping types are backed by the same Lucene field internally.
往 Elasticsearch 里面直接插入数据,Elasticsearch 会自动建立索引,同时建立 type 以及对应的 mapping
mapping 中就自动定义了每个 field 的数据类型
不同的数据类型(比如说 text 和 date),可能有的是 exact value,有的是 full text
exact value,在建立倒排索引的时候,分词的时候,是将整个值一起作为一个关键词建立到倒排索引中的;full text,会经历各种各样的处理,分词,normaliztion(时态转换,同义词转换,大小写转换),才会建立到倒排索引中
同时呢,exact value 和 full text 类型的 field 就决定了,在一个搜索过来的时候,对 exact value field 或者是 full text field 进行搜索的行为也是不一样的,会跟建立倒排索引的行为保持一致;比如说 exact value 搜索的时候,就是直接按照整个值进行匹配,full text query string 会进行分词和 normalization 再去倒排索引中去搜索
可以用 Elasticsearch 的 dynamic mapping,让其自动建立 mapping,包括自动设置数据类型;也可以提前手动创建 index 和 type 的 mapping,自己对各个 field 进行设置,包括数据类型,包括索引行为,包括分词器,等等
Mapping explosion
Defining too many fields in an index can lead to a mapping explosion, which can cause out of memory errors and difficult situations to recover from.
Use the mapping limit settings to limit the number of field mappings (created manually or dynamically) and prevent documents from causing a mapping explosion.
版权声明: 本文为 InfoQ 作者【escray】的原创文章。
原文链接:【http://xie.infoq.cn/article/0c70f6e7dbbf1e0b20ea7a2b1】。
本文遵守【CC-BY 4.0】协议,转载请保留原文出处及本版权声明。
评论