ES 筆記十四:Index Template 和 Dynamic Template

CrazyZard發表於2019-10-28
  • 叢集上的索引會越來越多,例如,你會為日誌每天建立個索引
    • 使用多個索引可以讓你的更好的管理的你資料,提高效能
  • Index Templates 幫助你設定Mapping 和 Settings,並按照一定的規則,自動匹配到新建立的索引之上
    • 模板僅在一個索引被新建立時,才會產生作用。修改模板不會影響已建立的索引
    • 可以設定多個索引模板,這些設定會被merge在一起
    • 可以指定order的數值,控制merging的過程
  • index_patterns 索引名稱 [""] ["test"]表示
    ES 筆記十四:Index Template 和 Dynamic Template
  • 當一個索引被新建立時
    • 應用Elasticsearch預設settingsmappings
    • 應該order數值低的Index Template中的設定
    • 應用order高的Index Template中的設定,之前的設定會被覆蓋
    • 應用建立索引時,使用者所指定的SettingsMappings,並覆蓋之前模板中的設定

測試預設的索引型別

#測試下預設的mapping
PUT ttemplate/_doc/1
{
  "someNumber" :"1",
  "someDate" : "2019/01/01"
}
//發現someDate推算正確,但是someNumber沒有推算稱number型別
GET ttemplate/_mapping
{
  "ttemplate" : {
    "mappings" : {
      "properties" : {
        "someDate" : {
          "type" : "date",
          "format" : "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
        },
        "someNumber" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

建立 template

#建立一個預設的template
PUT _template/template_default
{
  "index_patterns" : ["*"],
  "order" : 0,
  "version": 1,
  "settings" :{
    "number_of_shards" :1,
    "number_of_replicas" :1
  }
}
#匹配test開頭的patterns
PUT _template/template_test
{
    "index_patterns" : ["test*"],
    "order" : 1,
    "settings" : {
        "number_of_shards": 1,
        "number_of_replicas" : 2
    },
    "mappings" : {
        "date_detection": false,
        "numeric_detection": true
    }
}

檢視template資訊

GET _template/template_default
GET _template/temp* //萬用字元檢視所有的
PUT testtemplate/_doc/1
{
    "someNumber":"1",
    "someDate":"2019/01/01"
}
GET testtemplate/_mapping
{
  "testtemplate" : {
    "mappings" : {
      "date_detection" : false,
      "numeric_detection" : true,
      "properties" : {
        "someDate" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "someNumber" : {
          "type" : "long"
        }
      }
    }
  }
}

GET testtemplate/_settings
{
  "testtemplate" : {
    "settings" : {
      "index" : {
        "creation_date" : "1572275616083",
        "number_of_shards" : "1",
        "number_of_replicas" : "2",
        "uuid" : "s3B3NEaZRDSq586qDVbCqA",
        "version" : {
          "created" : "7030099"
        },
        "provided_name" : "testtemplate"
      }
    }
  }
}
PUT testmy
{
    "settings":{
        "number_of_replicas":5
    }
}

PUT testmy/_doc/1
{
  "key":"value"
}
//testmy 設定完之後,會覆蓋test*開頭是settings
GET testmy/_settings
{
  "testmy" : {
    "settings" : {
      "index" : {
        "creation_date" : "1572275856520",
        "number_of_shards" : "1",
        "number_of_replicas" : "5",
        "uuid" : "yJJ02g77S6ubgs1w3OVY2g",
        "version" : {
          "created" : "7030099"
        },
        "provided_name" : "testmy"
      }
    }
  }
}
  • 根據Elasticsearch 識別的資料型別,結合欄位名稱,來動態設定欄位型別
    • 所有的字串型別都設定稱Keyword,或者關閉keyword欄位
    • is 開頭的欄位都設定成boolean
    • long_ 開頭的都設定成long型別

ES 筆記十四:Index Template 和 Dynamic Template

  • Dynamic Template 是定義在某個索引的Mapping 中
  • Template 有個名字
  • 匹配規則是個一個陣列
  • 為匹配到欄位設定Mapping
DELETE my_index
PUT my_index
{
  "mappings": {
    "dynamic_templates": [
            {
        "strings_as_boolean": {
          "match_mapping_type":   "string",
          "match":"is*",
          "mapping": {
            "type": "boolean"
          }
        }
      },
      {
        "strings_as_keywords": {
          "match_mapping_type":   "string",
          "mapping": {
            "type": "keyword"
          }
        }
      }
    ]
  }
}

PUT my_index/_doc/1
{
  "firstName":"Li",
  "isVIP":"true"
}
GET my_index/_mapping
//返回部分
"properties" : {
        "firstName" : {
          "type" : "keyword"
        },
        "isVIP" : {
          "type" : "boolean"
        }
      }

設定新的my_index

DELETE my_index
#結合路徑
PUT my_index
{
  "mappings": {
    "dynamic_templates": [
      {
        "full_name": {
          "path_match":   "name.*",
          "path_unmatch": "*.middle",
          "mapping": {
            "type":       "text",
            "copy_to":    "full_name"
          }
        }
      }
    ]
  }
}
PUT my_index/_doc/1
{
  "name": {
    "first":  "John",
    "middle": "Winston",
    "last":   "Lennon"
  }
}
//可查詢到
GET my_index/_search?q=full_name:John

相關文章