Bootstrap

Elasticsearch Mapping Root Object

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

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

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.

好处:

如果不需要上述好处,可以禁用 _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