实践004-elasticsearch之Index Template和Dynamic Template

盛勃
• 阅读 1158

[toc]


一、Index Template简介

1. 什么是index模板?

帮助你设定mappingssettings; 并按照一定的规则,自动匹配到新建的索引上;

2. 关于index模板的几个问题

Q1: 已新建了索引A使用了模板M,修改了M后会影响A吗?

A: 不会! 模板仅仅在一个索引被创建时,才产生作用。修改模板不会影响已经创建的索引。

Q2: 可否设定使用多个index模板?

A: 可以! 设定多个索引模板,这些模板会被merge在一起。

Q3: 设定多个index模板,起作用是按什么顺序?

A: 你可以指定order的数值,控制merging的过程。order大的有限起作用!

二、 index template案例

2.1 两个模板定义和创建索引的作用优先级

2.1.1 模板1设定所有索引创建时->分片数1,副本数1

# 1. 所有索引:分片数1,副本数1
PUT _template/my_template
{
  "index_patterns": ["*"],
  "order": 10, // order大的优先
  "version": 1,
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  }
}

2.1.2 模板2设定test开头的索引设置主分片1,副本数2; 关闭date识别,开启数字识别

  • date识别(date_detection)就是:字符串的日期,识别为date(默认是开启的)
  • 数字识别(numeric_detection)就是:字符串的数字,识别为数字类型(默认是关闭的);
#2.test开头的索引设置主分片1,副本数2; 关闭date识别,开启数字识别
PUT _template/my_template_test
{
  "index_patterns": ["test*"],
  "order": 1,
  "settings":{
    "number_of_shards": 1,
    "number_of_replicas": 2
  },
  "mappings":{
    "date_detection": false,
    "numeric_detection": true
  }
}

2.1.3 创建test开头的索引,验证

PUT test_template_index/_doc/1
{
  "someNumber": "1",
  "somDate": "2019/01/01"
}

看看作用效果:

GET test_template_index/_mapping

看看结果:

{
  "test_template_index" : {
    "mappings" : {
      "date_detection" : false,
      "numeric_detection" : true,
      "properties" : {
        "somDate" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "someNumber" : {
          "type" : "long"
        }
      }
    }
  }
}

可见模板2起作用了: somDate被识别为text而非date; someNumber被识别为long而非text了;

默认的话,somDate是会被识别为date, someNumber会被识别为text的!

我们再看看关于shardsreplicas的数量,谁的起作用了:

GET test_template_index/_settings

看看结果:两个都是1

{
  "test_template_index" : {
    "settings" : {
      "index" : {
        "creation_date" : "1650989910135",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "HdvbZrC8SwCH9xA3CbzFxg",
        "version" : {
          "created" : "7050299"
        },
        "provided_name" : "test_template_index"
      }
    }
  }
}
看来这个是模板1起作用了,order大的被选择了!

所以结论:

  • 多个模板匹配的话,都会启用
  • 最后将所有配置merge, order大的会覆盖小的中的配置;

三、Dynamic Template简介

3.1 什么是动态模板?

主要用来动态设定字段类型:根据ES识别的数据类型,结合字段名称来动态设定字段类型。

举例:

  • 设置所有字符串类型都为keyword, 或者关闭text类型的keyword子字段;
  • is开头的字段都设定为boolean;
  • long_开头的字段都设定为long类型;

3.2 动态模板注意

  • 是定义在某个索引mappings中--> 区别于index template;
  • template有一个名称;
  • 匹配规则为一个数组;
  • 为匹配字段设置 mapping;

3.3 动态模板的例子

指定某个索引的is*识别为boolean;字符串都设为keyword

例1-动态模板设定:

PUT my_dynamic_template_index
{
    "mappings": {
        "dynamic_templates": [
            {
                "is_x_to_boolean": { // 自定义名称
                    "match_mapping_type": "string",
                    "match": "is*",
                    "mapping": {
                        "type": "boolean"
                    }
                }
            },
            {
                "string_to_keyword": { // 自定义名称
                    "match_mapping_type": "string",
                    "mapping": {
                        "type": "keyword"
                    }
                }
            }
        ]
    }
}

注意: dynamic_templates是个数组,其中每个对象是一个动态模板,每个模板有一个自定义的名字:

例1-动态模板测试:

PUT my_dynamic_template_index/_doc/1
{
  "firstName":"Nie", "isVip":"true"
}

查看mapping:

GET my_dynamic_template_index/_mapping

结果:

{
  "my_dynamic_template_index" : {
    "mappings" : {
      "dynamic_templates" : [
        {
          "is_x_to_boolean" : {
            "match" : "is*",
            "match_mapping_type" : "string",
            "mapping" : {
              "type" : "boolean"
            }
          }
        },
        {
          "string_to_keyword" : {
            "match_mapping_type" : "string",
            "mapping" : {
              "type" : "keyword"
            }
          }
        }
      ],
      "properties" : {
        "firstName" : {
          "type" : "keyword"
        },
        "isVip" : {
          "type" : "boolean"
        }
      }
    }
  }
}

可以看到起作用了。

点赞
收藏
评论区
推荐文章
Easter79 Easter79
3年前
vue 自定义步骤条组件(css)
话不多说直接上组件代码。。<template<div<ulclass"steps"<livfor"(item,index)inSetData":key"itemindex":class"{'a
虾米大王 虾米大王
3年前
java代码007
code007.jsp包含文件标识<%举例新建一个top.jsp文件,内容如下:新建一个copyright.jsp文件,内容如下:%Stringcopyright"AllCopyright&copy;某某公司";%%copyright%新建一个index.jsp
虾米大王 虾米大王
3年前
java代码095
code095.jspEL表达式访问数组<%Stringarr2(String)request.getAttribute("book");for(inti0;i$index:$bookindex
Wesley13 Wesley13
3年前
Java操作ElasticSearch(基础篇)
1.前言   看本文前需要先对ElasticSearch有一定了解(比如什么是index什么是type)   本人推荐直接使用ElasticSearchJavaAPI来操作..本人最开始是用springbootspringdataelasticsearchstarter来作为orm的..操作是方便了很多.支持类spr
Stella981 Stella981
3年前
Elasticsearch Index Templates(索引模板)
索引模板,故名思议,就是创建索引的模板,模板中包含公共的配置(settings)和映射(Mapping),并包含一个简单触发条件,及条件满足时使用该模板创建一个新的索引。注意:模板只在创建索引时应用。更改模板不会对现有索引产生影响。当使用createindexAPI时,作为createindex调用的一部分定义的设置/映射将优先于模板中定义的任
Wesley13 Wesley13
3年前
MySQL ORDER BY主键id加LIMIT限制走错索引
背景及现象report\_product\_sales\_data表数据量2800万;经测试,在当前数据量情况下,orderby主键id,limit最大到49的时候可以用到索引report\_product\_sales\_data\_hq\_code\_orgz\_id\_index,大于49时就走PRIMARY主键索引。
Stella981 Stella981
3年前
Elasticsearch基本概念及核心配置文件详解
<divid"cnblogs\_post\_body"class"blogpostbody"<p&nbsp;  Elasticsearch5.X,下列的是Elasticsearch2.X系类配置,其实很多配置都是相互兼容的</p<h2id"1配置文件"1.配置文件</h2<prename"code"<codeclass
Stella981 Stella981
3年前
Elasticsearch学习总结二 elasticSearch一些基本用法
一.elasticSearch提供了一些基本的rest命令,基本如下:/index/_search搜索指定索引下的数据,http://ip:9200/index/_search查询当前索引下的数据/index/查看指定索引的详细信
Wesley13 Wesley13
3年前
vant 底部导航组件的实现(tabbar)
简单设计一个底部导航的功能组件1.路由跳转2.选项卡的原理3.路由拦截<template<vantabbarvmodel"active"class"active_tab"<vantabbaritemvfor"(item,index)intabbars"
Stella981 Stella981
3年前
PostgreSQL 主键自动增长
建立主键并设置自动增加的办法好好几种,这里记录我测试过的:droptablepro_process;CREATETABLE"public"."pro_process"("id"SERIALprimarykey,//设置主键并自动增长"county_code"varchar(6)COLLATE
Stella981 Stella981
3年前
ElasticSearch 索引设置总结
在使用ES时,我们常见的就是需要生成一个template来定义索引的设置,分词器,Mapping.本文将基于项目经验来总结一些常用的配置。Index设置 index.refresh\_interval   配置一个刷新时间,将indexbuffer刷新到oscache的时间间隔,刷新到oscache的