美文网首页
3.13-IndexTemplate和DynamicTempla

3.13-IndexTemplate和DynamicTempla

作者: 落日彼岸 | 来源:发表于2020-03-23 16:54 被阅读0次

什么是Index Template

  • Index Template - 帮助你设定Mapping和Setting,并按照一定的规则,自动匹配到新创建的索引上

    • 模板仅在一个索引被新创建时,才会产生作用.修改模板不会影响已创建的索引

    • 你可以设定多个索引模板,这些设置会被"merge"在一起

    • 你可以指定"order"的数值,控制"merging"的过程

两个Index Templates

PUT _template/template_default
{
  "index_patterns": ["*"],
  "order" : 0,
  "version": 1,
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas":1
  }
}

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

Index Template的工作方式

  • 当一个索引被创建时

    • 应用ElasticSearch默认的settings和mappings

    • 应用order数值低的Index Template 中的设定

    • 应用order高的Index Template中的设定,之前的设定会被覆盖

    • 应用创建索引时,用户所指定的Settings和Mappings,并覆盖之前模板中的设定

什么是Dynamic Template

  • 根据ElasticSearch识别的数据类型,结合字段名称,来动态设定字段类型

    • 所有的字符串类型都设定成Keyword,或者关闭keyword字段

    • is开头的字段都设置成boolean

    • long_ 开头的都设置成long类型

Dynamic Template

  • Dynamic Template是定义在某个索引的Mapping中

  • Template有一个名称

  • 匹配规则是一个数组

  • 为匹配到字段设置Mapping

PUT my_index
{
  "mappings": {
    "dynamic_templates": [
      {
        "full_name": {
          "path_match":   "name.*",
          "path_unmatch": "*.middle",
          "mapping": {
            "type":       "text",
            "copy_to":    "full_name"
          }
        }
      }
    ]
  }
}

课程Demo

#数字字符串被映射成text,日期字符串被映射成日期
PUT ttemplate/_doc/1
{
    "someNumber":"1",
    "someDate":"2019/01/01"
}
GET ttemplate/_mapping


#Create a default template
PUT _template/template_default
{
  "index_patterns": ["*"],
  "order" : 0,
  "version": 1,
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas":1
  }
}


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
get testtemplate/_settings

PUT testmy
{
    "settings":{
        "number_of_replicas":5
    }
}

put testmy/_doc/1
{
  "key":"value"
}

get testmy/_settings
DELETE testmy
DELETE /_template/template_default
DELETE /_template/template_test



#Dynaminc Mapping 根据类型和字段名
DELETE my_index

PUT my_index/_doc/1
{
  "firstName":"Ruan",
  "isVIP":"true"
}

GET my_index/_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"
          }
        }
      }
    ]
  }
}


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

相关阅读

相关文章

网友评论

      本文标题:3.13-IndexTemplate和DynamicTempla

      本文链接:https://www.haomeiwen.com/subject/cyswyhtx.html