elasticsearch中创建索引模板_index_template
  TEZNKK3IfmPf 2023年11月14日 75 0

1、什么是索引模板

        索引模版是创建索引时自动应用提前设置好的settings、mappings和aliases,通过索引的名称进行匹配。
        对索引模版的更改时不会影响目前已经根据索引模版创建的索引。

2、索引模板作用及使用场景

        使用索引模版可以省去创建索引时再次指定settings、mappings、aliases的步骤,具体的应用场景比较常用的就有日志索引。

        需求如下:查询日志索引名称为log,每天根据当天日期生成索引(log-20232302),所有的索引使用相同的settings和mappings,且alias指向最新日期的log索引那么我们就可以使用索引模版来实现。

3、创建索引模板

        创建模板索引的参数及模板控制参数如下;

  • index_patterns: 必须的参数,使用通配符定义匹配索引的规则。
  • priority:可选的参数,索引模版的匹配优先级,如果不填默认0(最低优先级),多个模版时,优先匹配优先级高的模版。
  • template:可选参数,但是我认为是必须的,少了这个参数,索引模版的意义在哪呢是不是,可以说是核心参数。可以配置索引的settings,mappings,aliases。

        settings:索引的settings设置。

        mappings:索引的mappings设置。

        aliases:对象形式,key是别名的名称,并且还支持如下参数:

                filter:可选,对象类型,限制别名能访问的文档

                index_routing:可选,字符串,索引操作时的路由值,如果指定会覆盖routing的值

                is_hidden:可选,布尔类型,如果设置为true,隐藏别名。默认false,该别名指定的所有索引必须有相同的is_hidden值

                is_write_index:可选,布尔类型,如果设置为true,该索引为别名的写索引

                routing:可选,字符串,索引和搜索操作时的路由值

                search_routing:可选,字符串,搜索操作时的路由值,如果指定会覆盖routing的值

  • version:索引模版的版本号,Elasticsearch不会自动生成
  • composed_of:可选,字符串数组,可选可使用的组件模版的有序数组。按照数组中组件模版的顺序合并,最后一个模版具有最高的优先级
  • data_stream:可选,对象类型,如果索引模版中包含该该对象,可以是空对象,则使用模版创建索引数据流和支持的索引

        支持如下参数:

                hidden:可选,布尔类型,如果为 true,数据流隐藏,默认 false
                allow_custom_routing: 可选,布尔类型,如果为 true,则数据流支持自定义路由,默认 false

  • _meta:可选,对象类型,该索引模版的用户元数据配置
     

创建一个简单的索引模板:

PUT _index_template/log_template
{
  "index_patterns": "log*",
  "priority": "1",
  "template": {
    "settings": {
      "number_of_shards": "2",
      "number_of_replicas": "1"
    },
    "mappings": {
      "properties": {
        "creater": {
          "type": "keyword"
        },
        "module": {
          "type": "keyword"
        },
        "content": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          }
        },
        "time": {
          "type": "date",
          "format": "strict_date_optional_time||epoch_millis||yyyy-MM-dd HH:mm:ss"
        }
      }
    },
    "aliases": {
      "log": {}
    }
  }
}

elasticsearch中创建索引模板_index_template

4、查看索引模板

GET _index_template/log_template

# 响应结果
{
  "index_templates" : [
    {
      "name" : "log_template",
      "index_template" : {
        "index_patterns" : [
          "log*"
        ],
        "template" : {
          "settings" : {
            "index" : {
              "number_of_shards" : "2",
              "number_of_replicas" : "1"
            }
          },
          "mappings" : {
            "properties" : {
              "module" : {
                "type" : "keyword"
              },
              "creater" : {
                "type" : "keyword"
              },
              "time" : {
                "format" : "strict_date_optional_time||epoch_millis||yyyy-MM-dd HH:mm:ss",
                "type" : "date"
              },
              "content" : {
                "type" : "text",
                "fields" : {
                  "keyword" : {
                    "type" : "keyword"
                  }
                }
              }
            }
          },
          "aliases" : {
            "log" : { }
          }
        },
        "composed_of" : [ ],
        "priority" : 1
      }
    }
  ]
}

elasticsearch中创建索引模板_index_template

5、校验索引模板是否存在

        存在返回200,不存在返回404。

elasticsearch中创建索引模板_index_template

elasticsearch中创建索引模板_index_template

6、使用索引模板

6.1 通过索引模板创建索引

以上面创建的索引模板log*为例,索引名称以log开头的索引都会自动使用索引模板创建。

创建索引log-2023-03-04:

PUT log-2023-03-04

elasticsearch中创建索引模板_index_template

查看索引log-2023-03-04:

GET log-2023-03-04

# 响应结果
{
  "log-2023-03-04" : {
    "aliases" : {
      "log" : { }
    },
    "mappings" : {
      "properties" : {
        "content" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword"
            }
          }
        },
        "creater" : {
          "type" : "keyword"
        },
        "module" : {
          "type" : "keyword"
        },
        "time" : {
          "type" : "date",
          "format" : "strict_date_optional_time||epoch_millis||yyyy-MM-dd HH:mm:ss"
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "2",
        "provided_name" : "log-2023-03-04",
        "creation_date" : "1684712008494",
        "number_of_replicas" : "1",
        "uuid" : "PLGea-euRF6RqFO8d4xstA",
        "version" : {
          "created" : "7170699"
        }
      }
    }
  }
}

elasticsearch中创建索引模板_index_template

6.2 同时匹配多个索引模板的选择

        在实际使用中,我们会创建多个索引模板,这个时候该如何选择呢?

        首先再创建一个索引模版log_template2,匹配模式设置'log-2023*',优先级设置2,该模版time字段设置为keyword,上面log_template模版设置time为date类型。

PUT _index_template/log_template2
{
  "index_patterns": "log-2023*",
  "priority": "2",
  "template": {
    "settings": {
      "number_of_shards": "2",
      "number_of_replicas": "1"
    },
    "mappings": {
      "properties": {
        "creater": {
          "type": "keyword"
        },
        "module": {
          "type": "keyword"
        },
        "content": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          }
        },
        "time": {
          "type": "keyword"
        }
      }
    },
    "aliases": {
      "log-2023": {}
    }
  }
}

elasticsearch中创建索引模板_index_template

创建索引log-2023-03-04-v2:

elasticsearch中创建索引模板_index_template

查看索引log-2023-03-04-v2:

elasticsearch中创建索引模板_index_template

        可以看出索引og-2023-03-04-v2使用了log_template2模板,log_template2优先级设置为2高于log_template的优先级。索引匹配到多个模版时优先使用优先级高的模版。

6.3 使用索引模板创建索引别名

        在上面使用的两个索引模板中,创建索引时都自动添加了别名,别名操作语句如下:

添加别名:

方法一:

POST _aliases
{
  "actions": [
    {
      "add": {
        "index": "log-*",
        "alias": "logs"
      }
    }
  ]
}

elasticsearch中创建索引模板_index_template

elasticsearch中创建索引模板_index_template

方法二:

elasticsearch中创建索引模板_index_template

删除别名:

POST _aliases
{
  "actions": [
    {
      "remove": {
        "index": "log-2023-03-04-v2",
        "alias": "log-2023"
      }
    }
  ]
}

elasticsearch中创建索引模板_index_template

批量删除别名:

POST _aliases
{
  "actions": [
    {
      "remove": {
        "index": "log-2023*",
        "alias": "logs"
      }
    }
  ]
}

elasticsearch中创建索引模板_index_template

elasticsearch中创建索引模板_index_template

7、删除索引模板

DELETE _index_template/log_template

elasticsearch中创建索引模板_index_template

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2024年05月17日   47   0   0 查询mysql索引
  TEZNKK3IfmPf   2024年03月29日   55   0   0 mysql索引
TEZNKK3IfmPf