笔记十四:Index Template 和 Dynamic Template

管理多个索引

  • 集群上的索引会越来越多,例如,你会为日志每天创建个索引
    • 使用多个索引可以让你的更好的管理的你数据,提高性能

      Index Template

  • Index Templates 帮助你设定Mapping 和 Settings,并按照一定的规则,自动匹配到新创建的索引之上
    • 模板仅在一个索引被新创建时,才会产生作用。修改模板不会影响已创建的索引
    • 可以设定多个索引模板,这些设置会被merge在一起
    • 可以指定order的数值,控制merging的过程

两个 Index Templates

  • index_patterns 索引名称 [“*”] [“test*”]表示
    ES 笔记十四:Index Template 和 Dynamic Template

Index Template 工作方式

  • 当一个索引被新创建时
    • 应用Elasticsearch默认settingsmappings
    • 应该order数值低的Index Template中的设定
    • 应用order高的Index Template中的设定,之前的设定会被覆盖
    • 应用创建索引时,用户所指定的SettingsMappings,并覆盖之前模板中的设定

Demo

测试默认的索引类型

#测试下默认的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* //通配符查看所有的

写入新的数据,index以test开头

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"
      }
    }
  }
}

设置test开头 settings

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"
      }
    }
  }
}

Dynamic Template

  • 根据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
es
本作品采用《CC 协议》,转载必须注明作者和本文链接
快乐就是解决一个又一个的问题!
CrazyZard
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!