初步解析 Elasticsearch Document 核心元数据
文字内容整理自B站中华石杉的 Elasticsearch 顶尖高手系列课程核心知识篇
Talk is cheap, Show me code.
作为技术类的文章,上来先看代码。
在 Kibana 的 Dev Tools Console 中,查询一条数据:
GET /test_index/test_type/1
{
"_index": "test_index",
"_type": "test_type",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"test_content": "test test"
}
}
然后再放一段官方文档:
Each document has metadata associated with it, such as the _index, mapping _type, and _id metadata fields. The behavior of some of these metadata fields can be customized when a mapping type is created.

_index 元数据
代表一个 document 存放在哪个 index 中
类似的数据放在一个索引,非类似的数据放不同索引:
product index(包含了所有的商品)
sales index(包含了所有的商品销售数据)
inventory index(包含了所有库存相关的数据)
如果你把比如product,sales,human resource(employee),全都放在一个大的index里面,比如说company index,不合适的。
index中包含了很多类似的 document:类似是什么意思,其实指的就是说,这些 document 的 fields 很大一部分是相同的,你说你放了 3 个 document,每个 document 的 fields 都完全不一样,这就不是类似了,就不太适合放到一个 index 里面去了。
索引名称必须是小写的,不能用下划线开头,不能包含逗号:product,website,blog
_type 元数据
代表 document 属于index中的哪个类别(type)
一个索引通常会划分为多个 type,逻辑上对 index 中有些许不同的几类数据进行分类:因为一批相同的数据,可能有很多相同的 fields,但是还是可能会有一些轻微的不同,可能会有少数fields是不一样的,举个例子,就比如说,商品,可能划分为电子商品,生鲜商品,日化商品,等等。
type 名称可以是大写或者小写,但是同时不能用下划线开头,不能包含逗号
_id元数据
代表 document 的唯一标识,与 index 和 type一起,可以唯一标识和定位一个 document
我们可以手动指定 document 的 id(put /index/type/id),也可以不指定,由 es 自动为我们创建一个id
以下来自官方文档,中文翻译是我自己的理解
Metadata fields
Each document has metadata associated with it, such as the _index, mapping _type, and _id metadata fields. The behavior of some of these metadata fields can be customized when a mapping type is created.
Identity metadata fields
_index
The index to which the document belongs.
代表一个 document 存放在哪个 index 中
_type
The document’s mapping type.
代表 document 属于 index 中的哪个类别
_id
The document’s ID.
document 的唯一标识
Document source metadata fields
_source
The original JSON representing the body of the document.
表达一个 document 的主体(body)的原始 JSON 串
_size
The size of the _source field in bytes, provided by the mapper-size plugin.
_source 元数据的大小
Indexing metadata fields
_field_names
All fields in the document which contain non-null values.
文档(document)中具有非空值的全部字段名称
_ignored
All fields in the document that have been ignored at index time because of ignore_malformed.
在索引时因为 ignore_malformed 可以被忽略的文档字段
Routing metadata field
_routing
A custom routing value which routes a document to a particular shard.
可以将文档存储在指定 shard 的自定义路由
Other metadata field
_meta
Application specific metadata.
应用特定元数据