在一个将被用于映射任何类型的基本mapping中,在创建索引时或者调用PUT mapping API时,我们可以通过添加一个名为default的映射类型来定制一个索引。

    1. PUT my_index
    2. {
    3. "mappings": {
    4. "_default_": { #1
    5. "_all": {
    6. "enabled": false
    7. }
    8. },
    9. "user": {}, #2
    10. "blogpost": { #3
    11. "_all": {
    12. "enabled": true
    13. }
    14. }
    15. }}

    | 1 | 在mapping的default中,将_all字段设置为禁用,该mapping中的所有类型的all字段默认为禁用。 |
    | 2 | user类型从_default
    中继承相关设置。 |
    | 3 | blogpost类型覆盖相关默认值,并且启用_all字段。 |

    Tips

    当使用PUT mapping 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字段中。 |

    原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/5.3/default-mapping.html(修改该链接为官网对应的链接)

    译文链接 : http://www.apache.wiki/pages/viewpage.action?pageId=9405471(修改该链接为 ApacheCN 对应的译文链接)

    贡献者 : 曾少峰ApacheCNApache中文网