Link Search Menu Expand Document Documentation Menu

索引模板

索引模板允许您使用预定义的映射和设置初始化新索引。例如,如果您持续索引日志数据,您可以定义一个索引模板,以便所有这些索引都具有相同数量的分片和副本。

创建模板

要创建索引模板,请使用 PUT 或 POST 请求

PUT _index_template/<template name>
POST _index_template/<template name>

此命令创建一个名为 daily_logs 的模板,并将其应用于名称匹配模式 logs-2020-01-* 的任何新索引,同时将其添加到 my_logs 别名。

PUT _index_template/daily_logs
{
  "index_patterns": [
    "logs-2020-01-*"
  ],
  "template": {
    "aliases": {
      "my_logs": {}
    },
    "settings": {
      "number_of_shards": 2,
      "number_of_replicas": 1
    },
    "mappings": {
      "properties": {
        "timestamp": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        },
        "value": {
          "type": "double"
        }
      }
    }
  }
}

您应该会看到以下响应

{
  "acknowledged": true
}

如果您创建名为 logs-2020-01-01 的索引,您会看到它具有模板中的映射和设置。

PUT logs-2020-01-01
GET logs-2020-01-01
{
  "logs-2020-01-01": {
    "aliases": {
      "my_logs": {}
    },
    "mappings": {
      "properties": {
        "timestamp": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        },
        "value": {
          "type": "double"
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1578107970779",
        "number_of_shards": "2",
        "number_of_replicas": "1",
        "uuid": "U1vMDMOHSAuS2IzPcPHpOA",
        "version": {
          "created": "7010199"
        },
        "provided_name": "logs-2020-01-01"
      }
    }
  }
}

任何匹配此模式的附加索引——logs-2020-01-02logs-2020-01-03 等等——将继承相同的映射和设置。

索引模式不能包含以下任何字符::"+/\|?#><

检索模板

列出所有索引模板

GET _cat/templates
GET /_index_template

按名称查找模板

GET _index_template/daily_logs

获取所有匹配模式的模板列表

GET _index_template/daily*

检查特定模板是否存在

HEAD _index_template/<name>

配置多个模板

您可以为索引创建多个索引模板。如果索引名称匹配多个模板,OpenSearch 会从优先级最高的模板中获取映射和设置,并将其应用于索引。

例如,假设您有两个模板都匹配 logs-2020-01-02 索引,并且 number_of_shards 字段存在冲突。

模板 1

PUT _index_template/template-01
{
  "index_patterns": [
    "logs*"
  ],
  "priority": 0,
  "template": {
    "settings": {
      "number_of_shards": 2,
      "number_of_replicas": 2
    }
  }
}

模板 2

PUT _index_template/template-02
{
  "index_patterns": [
    "logs-2020-01-*"
  ],
  "priority": 1,
  "template": {
    "settings": {
      "number_of_shards": 3
    }
  }
}

因为 template-02 具有更高的 priority 值,所以它优先于 template-01logs-2020-01-02 索引的 number_of_shards 值为 3,number_of_replicas 为默认值 1。

删除模板

您可以使用其名称删除索引模板

DELETE _index_template/daily_logs

可组合索引模板

管理多个索引模板面临以下挑战

  • 如果索引模板之间存在重复,存储这些索引模板会导致更大的集群状态。
  • 如果您想对所有索引模板进行更改,则必须手动修改每个模板。

您可以使用可组合索引模板来克服这些挑战。可组合索引模板允许您将常见设置、映射和别名抽象为可重用的构建块,称为组件模板。

您可以组合组件模板来构成一个索引模板。

您直接在 create index 请求中指定的设置和映射会覆盖索引模板及其组件模板中指定的任何设置或映射。

创建组件模板

让我们定义两个组件模板——component_template_1component_template_2

组件模板 1

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

组件模板 2

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

使用组件模板创建索引模板

创建索引模板时,您需要将组件模板包含在 composed_of 列表中。

OpenSearch 会按照您在索引模板中指定它们的顺序应用组件模板。您在索引模板内部指定的设置、映射和别名将最后应用。

PUT _index_template/daily_logs
{
  "index_patterns": [
    "logs-2020-01-*"
  ],
  "template": {
    "aliases": {
      "my_logs": {}
    },
    "settings": {
      "number_of_shards": 2,
      "number_of_replicas": 1
    },
    "mappings": {
      "properties": {
        "timestamp": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        },
        "value": {
          "type": "double"
        }
      }
    }
  },
  "priority": 200,
  "composed_of": [
    "component_template_1",
    "component_template_2"
  ],
  "version": 3,
  "_meta": {
    "description": "using component templates"
  }
}

如果您创建名为 logs-2020-01-01 的索引,您会看到它的映射和设置都源自这两个组件模板。

PUT logs-2020-01-01
GET logs-2020-01-01

示例响应

{
  "logs-2020-01-01": {
    "aliases": {
      "my_logs": {}
    },
    "mappings": {
      "properties": {
        "@timestamp": {
          "type": "date"
        },
        "ip_address": {
          "type": "ip"
        },
        "timestamp": {
          "type": "date",
          "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
        },
        "value": {
          "type": "double"
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1625382479459",
        "number_of_shards": "2",
        "number_of_replicas": "1",
        "uuid": "rYUlpOXDSUSuZifQLPfa5A",
        "version": {
          "created": "7100299"
        },
        "provided_name": "logs-2020-01-01"
      }
    }
  }
}

索引模板选项

您可以指定以下模板选项

选项 类型 描述 必需
template 对象 指定索引设置、映射和别名。
priority 整数 索引模板的优先级。
composed_of 字符串数组 与当前模板一起应用于新索引的组件模板的名称。
version 整数 指定版本号以简化模板管理。默认值为 null
_meta 对象 指定关于模板的元信息。
剩余 350 字符

有问题?

想要贡献?