ElasticSearch索引模板

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: ElasticSearch索引模板

索引模板_template

[toc]

Template 介绍

Index template 定义在创建新 index 时可以自动应用的 settingsmappings。 Elasticsearch 根据与 index 名称匹配的 index 模式将模板应用于新索引。这个对于我们想创建的一系列的 Index 具有同样的 settings 及 mappings。

比如我们希望每一天/月的日志的index都具有同样的设置。

Index template 仅在 index 创建期间应用。 对 index template 的更改不会影响现有索引。 create index API请求中指定的设置和映射会覆盖索引模板中指定的任何设置或映射。

索引模板Index Template

参数说明

参数 说明
template 匹配的索引,匹配多个规则: "index_patterns": ["te*", "bar*"]
order 排序,多个索引模板命中,合并所有配置,如参数重复取order最大的为准。这里的 “order” 的意思是:如果索引与多个模板匹配,则 Elasticsearch 应用此模板的顺序。该值为1,如果有更高 order 的 template,这个 settings 或 mappings 有可能被其它的 template 所覆盖。
aliases 索引别名

创建一个索引模板 Index Template

PUT _template/my_logs  
{
  "template": "test-*", 
  "order":    1, 
  "settings": {
    "number_of_shards": 1 
  },
  "mappings": {
    "properties":{
        "account_holder_name":{
          "type":"text",
          "fields":{
            "keyword":{
              "type":"keyword",
              "ignore_above":256
            }
          }
        },
        "certificate_type":{
          "type":"text",
          "fields":{
            "keyword":{
              "type":"keyword",
              "ignore_above":256
            }
          }
        }
        }
  },
  "aliases": {
    "{index}-alias": {} 
  }
}
  • 创建一个名为 my_logs 的模板。
  • 将这个模板应用于所有以 logstash- 为起始的索引。
  • 这个模板将会覆盖默认的 logstash 模板,因为默认模板的 order 更低。
  • 限制主分片数量为 1 。
  • 添加这个索引至 last_3_months 别名中。
  • {index}-alias 中 {index} 是索引名称
  • 创建certificate_typeaccount_holder_name两个字段,类型为text

这个模板指定了所有名字以 logstash- 为起始的索引的默认设置,不论它是手动还是自动创建的。 如果我们认为明天的索引需要比今天更大的容量,我们可以更新这个索引以使用更多的分片。

这个模板还将新建索引添加至了{index}-alias别名中,然而从那个别名中删除旧的索引则需要手动执行。

测试

不存在的索引直接添加数据

直接向索引添加数据,此时未创建这个索引

PUT test-123/_doc/123
{
  "i12d":123
}

查看索引结构,发现我们模板创建的字段已经存在,说明模板创建成功

GET test-123
{
   
   
  "test-123" : {
   
   
    "aliases" : {
   
   
      "test-123-alias" : {
   
    }
    },
    "mappings" : {
   
   
      "properties" : {
   
   
        "account_holder_name" : {
   
   
          "type" : "text",
          "fields" : {
   
   
            "keyword" : {
   
   
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "certificate_type" : {
   
   
          "type" : "text",
          "fields" : {
   
   
            "keyword" : {
   
   
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "i12d" : {
   
   
          "type" : "long"
        }
      }
    },
    "settings" : {
   
   
      "index" : {
   
   
        "routing" : {
   
   
          "allocation" : {
   
   
            "include" : {
   
   
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "test-123",
        "creation_date" : "1673858087923",
        "number_of_replicas" : "1",
        "uuid" : "FAkaooiET62QohAYn3Sjrw",
        "version" : {
   
   
          "created" : "7140299"
        }
      }
    }
  }
}

创建索引

PUT test-test3
{
  "settings":{
    "number_of_shards":6,
    "number_of_replicas":0
  },
  "mappings":{
      "properties":{
        "region":{
          "type":"integer"
        },
        "account_holder_name":{
          "type": "integer"
        }
      }
  }
}

做了一些验证项

  • 分片和副本数与模板不相同
  • 新增一个字段
  • account_holder_name字段类型与模板不相同

查看索引信息

GET test-test3
{
   
   
  "test-test3" : {
   
   
    "aliases" : {
   
   
      "test-test3-alias" : {
   
    }
    },
    "mappings" : {
   
   
      "properties" : {
   
   
        "account_holder_name" : {
   
   
          "type" : "integer",
          "fields" : {
   
   
            "keyword" : {
   
   
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "certificate_type" : {
   
   
          "type" : "text",
          "fields" : {
   
   
            "keyword" : {
   
   
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "region" : {
   
   
          "type" : "integer"
        }
      }
    },
    "settings" : {
   
   
      "index" : {
   
   
        "routing" : {
   
   
          "allocation" : {
   
   
            "include" : {
   
   
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "6",
        "provided_name" : "test-test3",
        "creation_date" : "1673858131778",
        "number_of_replicas" : "0",
        "uuid" : "MGE8mZ46ReSisIVhDBeTQw",
        "version" : {
   
   
          "created" : "7140299"
        }
      }
    }
  }
}

总结

  • 模板已存在,我们手动未指定的,模板的生效
  • 模板已存在,我们手动执行了,按照我们指定的为准

组合索引模板 Index Template 7.8版本之后引入

模板有两种类型:索引模板和组件模板。 组件模板是可重用的构建块,用于配置映射,设置和别名。 你使用组件模板来构造索引模板,但它们不会直接应用于一组索引。 索引模板可以包含组件模板的集合,也可以直接指定设置,映射和别名。

ElasticSearch索引模板

  • 索引1使用 索引模板一索引模板一 使用 组件模板一组件模板二

  • 索引2使用索引模板二索引模板一,索引模板又分别引用不同组件模板,这里会合并索引模板二索引模板一的配置,重复的内容根据索引模板配置的order字段取相对大的值

  • 索引4使用了索引模板四,索引模板可以不使用组件模板

创建基于组件模板的索引模板 Index Template

创建组件模板

组件模板1:

PUT _component_template/component_template1
{
   
   
  "template": {
   
   
    "mappings": {
   
   
      "properties": {
   
   
        "@timestamp": {
   
   
          "type": "date"
        }
      }
    }
  }
}

组件模板2:

PUT _component_template/component_template2
{
   
   
  "template": {
   
   
    "mappings": {
   
   
      "properties": {
   
   
        "ip_address": {
   
   
          "type": "ip"
        }
      }
    }
  }
}

在上面,我们定义了组件模板 component_template1 以及组件模板 component_template2 两个组件模板。

我们运用上面的两个组件模板 component template 来组合一个索引模板 index tempate。

创建索引模板 index tempate:

PUT _index_template/template_1
{
   
   
  "index_patterns": ["te*", "bar*"],
  "template": {
   
   
    "settings": {
   
   
      "number_of_shards": 1
    },
    "mappings": {
   
   
      "_source": {
   
   
        "enabled": false
      },
      "properties": {
   
   
        "host_name": {
   
   
          "type": "keyword"
        },
        "created_at": {
   
   
          "type": "date",
          "format": "EEE MMM dd HH:mm:ss Z yyyy"
        }
      }
    },
    "aliases": {
   
   
      "mydata": {
   
    }
    }
  },
  "priority": 200,
  "composed_of": ["component_template1", "component_template2"],
  "version": 3,
  "_meta": {
   
   
    "description": "my custom"
  }
}

注意这个部分

"composed_of": ["component_template1", "component_template2"]

这里是创建的索引模板引用了component_template1,component_template1组件模板

我们指定了index_patterns参数,以 tebar 开头的索引都会应用这个索引模板。

在Kibana中创建索引进行测试

创建索引:

PUT test

查看索引信息:

GET test
{
   
   
  "test" : {
   
   
    "aliases" : {
   
   
      "mydata" : {
   
    }
    },
    "mappings" : {
   
   
      "_source" : {
   
   
        "enabled" : false
      },
      "properties" : {
   
   
        "@timestamp" : {
   
   
          "type" : "date"
        },
        "created_at" : {
   
   
          "type" : "date",
          "format" : "EEE MMM dd HH:mm:ss Z yyyy"
        },
        "host_name" : {
   
   
          "type" : "keyword"
        },
        "ip_address" : {
   
   
          "type" : "ip"
        }
      }
    },
    "settings" : {
   
   
      "index" : {
   
   
        "routing" : {
   
   
          "allocation" : {
   
   
            "include" : {
   
   
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "test",
        "creation_date" : "1673862378764",
        "number_of_replicas" : "1",
        "uuid" : "8lePbT1bSmWanCKSkp8LPg",
        "version" : {
   
   
          "created" : "7140299"
        }
      }
    }
  }
}

对比一下,发现索引模板的字段和索引模板配置的组件模板字段都生效了。

模拟测试多组件模板

由于模板不仅可以由多个组件模板组成,还可以由索引模板本身组成。比较复杂,因此有两个模拟API(不生成实际索引,只用来查看验证)可以确定最终的索引设置是什么

指定索引名测试生成的索引信息

格式:

POST /_index_template/_simulate_index/${索引名称}

样例:

POST /_index_template/_simulate_index/test
{
   
   
  "template" : {
   
   
    "settings" : {
   
   
      "index" : {
   
   
        "number_of_shards" : "1"
      }
    },
    "mappings" : {
   
   
      "_source" : {
   
   
        "enabled" : false
      },
      "properties" : {
   
   
        "@timestamp" : {
   
   
          "type" : "date"
        },
        "created_at" : {
   
   
          "type" : "date",
          "format" : "EEE MMM dd HH:mm:ss Z yyyy"
        },
        "host_name" : {
   
   
          "type" : "keyword"
        },
        "ip_address" : {
   
   
          "type" : "ip"
        }
      }
    },
    "aliases" : {
   
   
      "mydata" : {
   
    }
    }
  },
  "overlapping" : [
    {
   
   
      "name" : "my_logs",
      "index_patterns" : [
        "test-*"
      ]
    }
  ]
}

指定索引模板测试生成的索引信息

格式:

POST /_index_template/_simulate/${索引模板名称}

样例:

# 上面我们已经创建了索引模板(Index Template)template_1
POST /_index_template/_simulate/template_1
{
   
   
  "template" : {
   
   
    "settings" : {
   
   
      "index" : {
   
   
        "number_of_shards" : "1"
      }
    },
    "mappings" : {
   
   
      "_source" : {
   
   
        "enabled" : false
      },
      "properties" : {
   
   
        "@timestamp" : {
   
   
          "type" : "date"
        },
        "created_at" : {
   
   
          "type" : "date",
          "format" : "EEE MMM dd HH:mm:ss Z yyyy"
        },
        "host_name" : {
   
   
          "type" : "keyword"
        },
        "ip_address" : {
   
   
          "type" : "ip"
        }
      }
    },
    "aliases" : {
   
   
      "mydata" : {
   
    }
    }
  },
  "overlapping" : [
    {
   
   
      "name" : "my_logs",
      "index_patterns" : [
        "test-*"
      ]
    }
  ]
}

验证同时使用多组件模板、索引模板优先级

定义两个组件模板(component template)

PUT /_component_template/ct1
{
   
   
  "template": {
   
   
    "settings": {
   
   
      "index.number_of_shards": 2
    }
  }
}

PUT /_component_template/ct2
{
   
   
  "template": {
   
   
    "settings": {
   
   
      "index.number_of_replicas": 0
    },
    "mappings": {
   
   
      "properties": {
   
   
        "@timestamp": {
   
   
          "type": "date"
        }
      }
    }
  }
}

模拟索引模板查看生成索引信息

POST /_index_template/_simulate
{
   
   
  "index_patterns": ["my*"],
  "template": {
   
   
    "settings" : {
   
   
        "index.number_of_shards" : 3
    }
  },
  "composed_of": ["ct1", "ct2"]
}

返回结果

{
   
   
  "template" : {
   
   
    "settings" : {
   
   
      "index" : {
   
   
        "number_of_shards" : "3",
        "number_of_replicas" : "0"
      }
    },
    "mappings" : {
   
   
      "properties" : {
   
   
        "@timestamp" : {
   
   
          "type" : "date"
        }
      }
    },
    "aliases" : {
   
    }
  },
  "overlapping" : [ ]
}

得出结论:

  • 索引模板如果设置与组件模板冲突,则以索引模板为主
  • 索引模板没有设置,则组件模板配置的内容生效

使用Kibana创建组合索引模板 Index Template

进入Kibana

image-20230117100122034

image-20230117100228037

在索引模板中,可以看到,template_1 索引模板由component_template1, component_template2组成

image-20230117100552901

创建组件模板

image-20230117100713724

image-20230117100757628

image-20230117100923584

image-20230117100951335

image-20230117101022574

image-20230117101036496

创建完成后可以在管理页面看到

未引用的组件模板可以删除,已经被索引模板引用的组件模板不可以删除

image-20230117101119967

参考

https://elasticstack.blog.csdn.net/article/details/100553185

https://elasticstack.blog.csdn.net/article/details/113751797

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
目录
相关文章
|
29天前
|
存储 自然语言处理 关系型数据库
ElasticSearch索引 和MySQL索引那个更高效实用那个更合适
ElasticSearch索引 和MySQL索引那个更高效实用那个更合适
38 0
|
2月前
|
存储 算法 NoSQL
Elasticsearch拆分索引知多少
Elasticsearch拆分索引知多少
33 0
|
1月前
|
JSON 监控 数据管理
【Elasticsearch专栏 12】深入探索:Elasticsearch使用索引生命周期管理(ILM)自动化删除旧数据
Elasticsearch的ILM功能允许用户定义策略,自动管理索引从创建到删除的生命周期。用户可以设置策略,根据索引年龄或大小自动删除旧数据,节省存储空间。通过应用ILM策略于索引模板,新索引将遵循预定义的生命周期。用户还可以监控ILM状态,确保策略按预期执行。使用ILM,用户可以高效地管理数据,确保旧数据及时删除,同时保持数据完整性和安全性。
|
2月前
|
存储 自然语言处理 搜索推荐
【Elasticsearch专栏 01】深入探索:Elasticsearch的正向索引和倒排索引是什么?
正向索引根据文档ID直接查找文档内容,适用于精确匹配场景;而倒排索引则基于文档内容构建,通过关键词快速定位相关文档,适用于全文搜索,显著提高查询效率,是搜索引擎的核心技术。
|
1天前
|
安全 API 数据安全/隐私保护
Elasticsearch 通过索引阻塞实现数据保护深入解析
Elasticsearch 通过索引阻塞实现数据保护深入解析
|
2月前
|
存储 自然语言处理 搜索推荐
【Elasticsearch专栏 02】深入探索:Elasticsearch为什么使用倒排索引而不是正排索引
倒排索引在搜索引擎中更受欢迎,因为它直接关联文档内容,支持全文搜索和模糊搜索,提高查询效率。其紧凑的结构减少了存储空间,并方便支持多种查询操作。相比之下,正排索引在搜索效率、存储和灵活性方面存在局限。
|
2月前
|
API 索引
Elasticsearch Index Shard Allocation 索引分片分配策略
Elasticsearch Index Shard Allocation 索引分片分配策略
77 1
|
2月前
|
存储 自然语言处理 关系型数据库
Elasticsearch创建一个索引怎么也这么复杂
Elasticsearch创建一个索引怎么也这么复杂
41 0
|
2月前
|
Java 网络架构 索引
Elasticsearch 如何实现索引的伪·命名空间
Elasticsearch 如何实现索引的伪·命名空间
9 0
|
2月前
|
存储 Java API
在生产环境中部署Elasticsearch:最佳实践和故障排除技巧———索引与数据上传(二)
在生产环境中部署Elasticsearch:最佳实践和故障排除技巧———索引与数据上传(二)

相关产品

  • 检索分析服务 Elasticsearch版