ElsticSearch中 Index Template 和 Dynamic Template

什么是 Index Template
  • Index Templates - 帮你设定 mappings 和 Settings ,并按照一定规则,自动匹配到新创建的索引值上
    1. 模版仅在一个索引被创建时,才会产生作用,修改模版不会影响已经创建的索引
    2. 你可以设定多个索引模版,这些设置会被 “merge” 在一起
    3. 你可以指定“order”的数值,控制“meriging”的过程
Index Template 的工作方式
  • 当一个索引被创建时
    1. 应用 ElasticSearch 默认的 Settings 和 mappings
    2. 应用 order 数值低的 Index Template 中的设定
    3. 应用 order 高的 Index Template 中的设定, 之前的设定会被覆盖
    4. 应用创建索引时, 用户所指定的 Setting 和 mappings , 并覆盖之前模板中的设定
创建一个默认的模版
// 创建一个默认的模版
PUT _template/template_default
{
  "index_patterns" : ["*"],   // 通配符表示所有的 模版
  "order" : 0,   
  "version" : 1, 
  "settings" : {
    "number_of_shards" : 1,    // 分片1
    "number_of_replicas" : 1   // 备份1
  }
}
// 返回结果
{
  "acknowledged" : true // 创建成功
}
// 创建一个test 开头的模版
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
// 返回信息
{
  "template_default" : {
    "order" : 0,
    "version" : 1,
    "index_patterns" : [
      "*"
    ],
    "settings" : {
      "index" : {
        "number_of_shards" : "1",
        "number_of_replicas" : "1"
      }
    },
    "mappings" : { },
    "aliases" : { }
  }
}
// 通过通配符
GET _template/template_*
// 返回信息
{
  "template_test" : {
    "order" : 1,
    "index_patterns" : [
      "test*"
    ],
    "settings" : {
      "index" : {
        "number_of_shards" : "1",
        "number_of_replicas" : "2"
      }
    },
    "mappings" : {
      "numeric_detection" : true,
      "date_detection" : false
    },
    "aliases" : { }
  },
  "template_default" : {
    "order" : 0,
    "version" : 1,
    "index_patterns" : [
      "*"
    ],
    "settings" : {
      "index" : {
        "number_of_shards" : "1",
        "number_of_replicas" : "1"
      }
    },
    "mappings" : { },
    "aliases" : { }
  }
}

测试 test 开头的 template
PUT testtemplate/_doc/1
{
  "someNumber" : "1",
  "someDate" : "2019/01/01"
}
// 获取mapping 查看
{
  "testtemplate" : {
    "mappings" : {
      "date_detection" : false,
      "numeric_detection" : true,
      "properties" : {
        "someDate" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "someNumber" : {
          "type" : "long"
        }
      }
    }
  }
}

// 获取setting
GET testtemplate/_settings

{
  "testtemplate" : {
    "settings" : {
      "index" : {
        "creation_date" : "1563174004803",
        "number_of_shards" : "1",
        "number_of_replicas" : "2",
        "uuid" : "kPDbWBwWSPaoroTWfx8WoA",
        "version" : {
          "created" : "7010099"
        },
        "provided_name" : "testtemplate"
      }
    }
  }
}

如果是在创建索引的时候,指定了setting,他就不会去选择 template 了

感谢评论的大佬指正的错误。

其实是创建索引的时候,制定了某些设置,那么索引便会优先使用这些设置。其他设置还是会去选择 template 中设置。谢谢评论的大佬!!!

什么是 Dynamic Template
  • 根据 ElasticSearch 识别的数据类型, 结合字段名称, 来动态设定字段类型
    1. 所有的字符串类型都设定成 Keyword,或者关闭 Keyword 字段
    2. is 开头的字段都设置成 boolean
    3. long_ 开头的都设置为 long 类型
  • Dynamic Template 是定义在某个索引的Mapping 中
  • Template 有一个名称
  • 匹配规则是一个数组
  • 为匹配到字段设置 Mapping
示例
//首先添加一个 index 查看mapping 
PUT my_index/_doc/1
{
  "firstName" : "Gao",
  "isVip" : "true"
}
GET my_index/_mapping

{
  "my_index" : {
    "mappings" : {
      "properties" : {
        "firstName" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "isVip" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    }
  }
}

// 再为该index 设置 dynamic template

出了点小问题。下次修改哈哈在研究一下
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值