Elasticsearch 【mappings 動態模板dynamic_templates】

1、shell

使用dynamic_templates能夠徹底控制新字段的映射,你設置能夠經過字段名或數據類型應用一個徹底不一樣的映射。app

每一個模板都由一個名字用於描述這個模板的用途,一個mapping字段用於指導這個映射怎麼使用,和至少一個參數match來定義這個模板使用哪一個字段。模板是按照順序檢測的,第一個匹配的模板會被啓用。一般把具體的模板放置在第一位,否則通用性模板會匹配,致使具體的是沒法被匹配到的。spa

注:如下都是在sense上操做的code

例子:咱們爲/my_index/my_type 分別建立orm

es:字段名以_es結尾的且是string類型的,須要使用spanish分詞器索引

enn:其餘字段的且是string類型的,須要使用english分詞器string

DELETE /my_index

PUT /my_index
{
    "mappings":{
        "my_type":{
            "dynamic_templates":[
                {
                    "es":{                                     =>模板名稱,隨意,通常要有語義
                        "match":"*_es",                        =>匹配字段名稱
                        "match_mapping_type":"string"          =>匹配字段類型
                        "mapping":{                            => 當匹配到以後,該字段的具體設置
                            "type":"string",
                            "anaylzer":"spanish"
                        }
                    }
                },
                {
                    "en":{                                     =>模板名稱,隨意,通常要有語義
                        "match":"*",                           =>匹配字段名稱(任意,通用的順序要在以後)
                        "match_mapping_type":"string"          =>匹配字段類型
                        "mapping":{                            => 當匹配到以後,該字段的具體設置
                            "type":"string",
                            "anaylzer":"english"
                        }
                    }
                }
            ]
        }
    }
}


POST /my_index/my_type
{
  "str_es":"xxx",            => 匹配es (字段名稱匹配,類型匹配)
  "long_es":123,             => 不會匹配es (字段名稱匹配,但類型不匹配)
  "date_es":"2015-01-01",    => 不會匹配es (字段名稱匹配,但類型不匹配)
  "long_en":123,             => 不會匹配en (字段名稱匹配,但類型不匹配)
  "str_en":"xxx",            => 匹配en (字段名稱匹配,但類型不匹配)
  "date_en":"2015-01-01"     => 不會匹配en (字段名稱匹配,但類型不匹配)
}


//mapping以下,正是結果
GET /my_index/my_type
{
   "my_index": {
      "mappings": {
         "my_type": {
            "dynamic_templates": [
               {
                  "es": {
                     "mapping": {
                        "analyzer": "spanish",
                        "type": "string"
                     },
                     "match": "*_es",
                     "match_mapping_type": "string"
                  }
               },
               {
                  "en": {
                     "mapping": {
                        "analyzer": "english",
                        "type": "string"
                     },
                     "match": "*",
                     "match_mapping_type": "string"
                  }
               }
            ],
            "properties": {
               "date_en": {
                  "type": "date",
                  "format": "dateOptionalTime"
               },
               "date_es": {
                  "type": "date",
                  "format": "dateOptionalTime"
               },
               "long_en": {
                  "type": "long"
               },
               "long_es": {
                  "type": "long"
               },
               "str_en": {
                  "type": "string",
                  "analyzer": "english"
               },
               "str_es": {
                  "type": "string",
                  "analyzer": "spanish"
               },
               "str_es2": {
                  "type": "string",
                  "analyzer": "english"
               }
            }
         }
      }
   }
}


2、默認映射
_default_

一般一個索引中的全部類型具備共享的字段和設置,用_default_映射來指定會設置更方便,而不是每次建立一個類型是重複操做。_default_映射新類型的模板,除非在本身類型的明確進行覆蓋
相關文章
相關標籤/搜索