写点什么

Elasticsearch Mapping Root Object

用户头像
escray
关注
发布于: 2021 年 03 月 07 日
Elasticsearch Mapping Root Object

Elasticsearch Mapping Root Object,内容来自 B 站中华石杉 Elasticsearch 顶尖高手系列课程核心知识篇,英文内容来自 Elasticsearch: The Definitive Guide [2.x],在新的版本中 Mapping types have been removed。


在目前的 Elasticsearch 中,Mapping types 已经被取消了,可以直接查看 Mappings 相关文档。

The Root Object


The uppermost level of a mapping is known as the root object.


  • properties: the mapping for each field that a document may contain

  • metadata fields: all of which start with an underscore, such as _type, _id, and _source

  • settings: control how the dynamic detection of new fields is handled, such as analyzer, dynamic_date_formats, and dynamic_templates

  • Other settings: which can be applied both to the root object and to fields of type object, such as enabled, dynamic and include_in_all


root object


就是某个 type 对应的 mapping JSON,包括了 properties,metadata(_id,_source,_type),settings(analyzer),其他 settings(比如 include_in_all)


PUT /my_index{  "mappings": {    "my_type": {      "properties": {}    }  }}
复制代码
Properties


  • type: The datatype that the field contains

  • index: Whether a field should be searchable as full text (analyzed), searchable as an exact value (not_analyzed), or not searchable to all (no)

  • analyzer: Which analyzer to use for a full-text field, both at index time and at search time.


PUT /my_index/_mapping/my_type{  "properties": {    "title": {      "type": "text"    }  }}
复制代码
Metadata: _source Field


By default, Elasticsearch stores the JSON string representing the document body in the _source field. Like all stored fields, the _source field is compressed before being written to disk.


好处:


  1. 查询的时候,直接可以拿到完整的 document,不需要先拿 document id,再发送一次请求拿 document

  2. partial update 基于 _source 实现

  3. reindex 时,直接基于 _source 实现,不需要从数据库(或者其他外部存储)查询数据再修改

  4. 可以基于 _source 定制返回 field

  5. debug query 更容易,因为可以直接看到 _source


如果不需要上述好处,可以禁用 _source


PUT /my_index{  "mappings": {    "my_type": {      "_source": {        "enable": false      }    }  }}
复制代码


在查询的时候也可以只返回需要的字段:


GET /_search{  "query": { "match_all": {} }}
GET _search{ "query": { "match_all": {} }, "_source": ["type", "updated_at"]}
复制代码


In Elasticsearch, setting individual document fields to be stored is usually a false optimization. The whole document is already stored as the _source field.

Metadata: _all Field


Metadata all Field: a special field that indexes the values from all other fields as one big string. The query_string query clause (and searches performed as ?q=john) defaults to search in the _all field if no other field is specified.


将所有 field 打包在一起,作为一个_all field,建立索引。没指定任何 field 进行搜索时,就是使用 all field 在搜索。


_all 在新版本的 Elasticsearch 中似乎已经不再使用了。

Metadata: Document Identity 标识性 metadata


  • _index: The string ID of the document, default indexed but not stored

  • _type: The type name of the document, default neither indexed nor stored

  • _id: The index where the document lives, default neither indexed nor stored

  • _uid: The _type and _id concatenated together as type#id, default stored(can be retrieved) and indexed(searchable)


you can query the _id field as though it were a real field. Elasticsearch uses the _uid field to derive the _id


发布于: 2021 年 03 月 07 日阅读数: 16
用户头像

escray

关注

Let's Go 2017.11.19 加入

在学 Elasticsearch 的项目经理

评论

发布
暂无评论
Elasticsearch Mapping Root Object