Index template 和Dynamic Template

2020-07-09  本文已影响0人  滴流乱转的小胖子

一、 Index template -- 针对所有匹配上的索引

1.1定义

帮助你设定Mappings 和 Settings, 并按照一定的规则,自动匹配到创建的索引之上

1.2两个Index Templates示例

image.png

1.3 Index Template的工作方式

当一个索引被创建时

demo

image.png
#数字字符串被映射成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*"], # 匹配以test开头的索引
    "order" : 1,
    "settings" : {
        "number_of_shards": 1,
        "number_of_replicas" : 2
    },
    "mappings" : {
        "date_detection": false,  # 关闭日期类型的推断, 日期将被转化为text
        "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

二、Dynamic Template --- 针对 一个索引

2.1 定义

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

2.2示例

image.png

2.3 匹配规则参数

image.png

2.4 demo

#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

相关阅读

上一篇 下一篇

猜你喜欢

热点阅读