使用示例

编辑

在以下示例中,将创建一个组件模板和一个索引模板。索引模板引用了两个组件模板,但只有 @package 组件模板存在。

创建组件模板 logs-foo_component1。必须在索引模板之前创建它,因为它不是可选的。

resp = client.cluster.put_component_template(
    name="logs-foo_component1",
    template={
        "mappings": {
            "properties": {
                "host.name": {
                    "type": "keyword"
                }
            }
        }
    },
)
print(resp)
response = client.cluster.put_component_template(
  name: 'logs-foo_component1',
  body: {
    template: {
      mappings: {
        properties: {
          'host.name' => {
            type: 'keyword'
          }
        }
      }
    }
  }
)
puts response
const response = await client.cluster.putComponentTemplate({
  name: "logs-foo_component1",
  template: {
    mappings: {
      properties: {
        "host.name": {
          type: "keyword",
        },
      },
    },
  },
});
console.log(response);
PUT _component_template/logs-foo_component1
{
  "template": {
    "mappings": {
      "properties": {
        "host.name": {
          "type": "keyword"
        }
      }
    }
  }
}

接下来,将创建索引模板,它引用两个组件模板。

  "composed_of": ["logs-foo_component1", "logs-foo_component2"]

之前,只创建了 logs-foo_component1 组件模板,这意味着 logs-foo_component2 缺失。因此,将以下条目添加到配置中:

  "ignore_missing_component_templates": ["logs-foo_component2"],

在创建模板期间,它不会验证 logs-foo_component2 是否存在。

resp = client.indices.put_index_template(
    name="logs-foo",
    index_patterns=[
        "logs-foo-*"
    ],
    data_stream={},
    composed_of=[
        "logs-foo_component1",
        "logs-foo_component2"
    ],
    ignore_missing_component_templates=[
        "logs-foo_component2"
    ],
    priority=500,
)
print(resp)
response = client.indices.put_index_template(
  name: 'logs-foo',
  body: {
    index_patterns: [
      'logs-foo-*'
    ],
    data_stream: {},
    composed_of: [
      'logs-foo_component1',
      'logs-foo_component2'
    ],
    ignore_missing_component_templates: [
      'logs-foo_component2'
    ],
    priority: 500
  }
)
puts response
const response = await client.indices.putIndexTemplate({
  name: "logs-foo",
  index_patterns: ["logs-foo-*"],
  data_stream: {},
  composed_of: ["logs-foo_component1", "logs-foo_component2"],
  ignore_missing_component_templates: ["logs-foo_component2"],
  priority: 500,
});
console.log(response);
PUT _index_template/logs-foo
{
  "index_patterns": ["logs-foo-*"],
  "data_stream": { },
  "composed_of": ["logs-foo_component1", "logs-foo_component2"],
  "ignore_missing_component_templates": ["logs-foo_component2"],
  "priority": 500
}

索引模板 logs-foo 已成功创建。可以基于此模板创建数据流。

resp = client.indices.create_data_stream(
    name="logs-foo-bar",
)
print(resp)
response = client.indices.create_data_stream(
  name: 'logs-foo-bar'
)
puts response
const response = await client.indices.createDataStream({
  name: "logs-foo-bar",
});
console.log(response);
PUT _data_stream/logs-foo-bar

查看数据流的映射,它将包含 host.name 字段。

在稍后的阶段,可能会添加缺失的组件模板。

resp = client.cluster.put_component_template(
    name="logs-foo_component2",
    template={
        "mappings": {
            "properties": {
                "host.ip": {
                    "type": "ip"
                }
            }
        }
    },
)
print(resp)
response = client.cluster.put_component_template(
  name: 'logs-foo_component2',
  body: {
    template: {
      mappings: {
        properties: {
          'host.ip' => {
            type: 'ip'
          }
        }
      }
    }
  }
)
puts response
const response = await client.cluster.putComponentTemplate({
  name: "logs-foo_component2",
  template: {
    mappings: {
      properties: {
        "host.ip": {
          type: "ip",
        },
      },
    },
  },
});
console.log(response);
PUT _component_template/logs-foo_component2
{
  "template": {
    "mappings": {
      "properties": {
        "host.ip": {
          "type": "ip"
        }
      }
    }
  }
}

这不会立即对数据流产生影响。只有在下次数据流自动滚动更新或触发手动滚动更新时,映射 host.ip 才会显示在数据流映射中。

resp = client.indices.rollover(
    alias="logs-foo-bar",
)
print(resp)
response = client.indices.rollover(
  alias: 'logs-foo-bar'
)
puts response
const response = await client.indices.rollover({
  alias: "logs-foo-bar",
});
console.log(response);
POST logs-foo-bar/_rollover