> For the complete documentation index, see [llms.txt](https://xiaoxiami.gitbook.io/elasticsearch/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://xiaoxiami.gitbook.io/elasticsearch/ji-chu/mapping/324dynamic-mappingff08-dong-tai-ying-she-ff09/defaultmapping-mappingzhong-de-default.md).

# \_default\_ mapping(mapping中的\_default\_)

在一个将被用于映射任何类型的基本mapping中，在[创建索引](https://www.elastic.co/guide/en/elasticsearch/reference/5.2/indices-create-index.html) 时或者调用 [PUT mapping](https://www.gitbook.com/book/xiaoxiami/elasticsearch/edit#) API时，我们可以通过添加一个名为\_default\_的映射类型来定制一个索引。

```
PUT my_index
{
  "mappings": {
    "_default_": {  # 1
      "_all": {
        "enabled": false
      }
    },
    "user": {},   # 2
    "blogpost": {  # 3
      "_all": {
        "enabled": true
      }
    }
  }
}
```

| 1 | 在mapping的\_default\_中，将[\_all](http://cwiki.apachecn.org/display/Elasticsearch/_all+field)字段设置为禁用，该mapping中的所有类型的\_all字段默认为禁用。 |
| - | ------------------------------------------------------------------------------------------------------------------------------ |
| 2 | user类型从\_default\_中继承相关设置。                                                                                                     |
| 3 | blogpost类型覆盖相关默认值，并且启用[\_all](http://cwiki.apachecn.org/display/Elasticsearch/_all+field)字段。                                   |

> Tips
>
> 当使用[PUT mapping](https://www.gitbook.com/book/xiaoxiami/elasticsearch/edit#) API更新mapping的\_default\_时，新的mapping不会与现有的mapping合并。相反，新的mapping的\_defalut\_会替换现有的。

虽然mapping的\_default\_可以在索引创建后更新，但是新的默认值只会影响之后创建的映射类型。

mapping中的\_default\_可以与索引模板一起使用，用来控制自动创建的索引中动态创建的类型。

```
PUT _template/logging
{
  "template":   "logs-*",             # 1
  "settings": { "number_of_shards": 1 },  # 2
  "mappings": {
    "_default_": {
      "_all": {                         # 3
        "enabled": false
      },
      "dynamic_templates": [
        {
          "strings": {               # 4
            "match_mapping_type": "string",
            "mapping": {
              "type": "text",
              "fields": {
                "raw": {
                  "type":  "keyword",
                  "ignore_above": 256
                }
              }
            }
          }
        }
      ]
    }
  }
}

PUT logs-2015.10.01/event/1
{ "message": "error:16" }
```

| 1 | logging模板将匹配以logs-开头的任何索引。               |
| - | ---------------------------------------- |
| 2 | 匹配的索引将会创建单个主分片。                          |
| 3 | 在新的映射类型中，\_all字段将会被默认禁用。                 |
| 4 | 字符串字段将会被创建到名为text的主字段和一个 keyword.raw字段中。 |
