Home > Software design >  How to Add a Component Template to an Existing Composable Index Template
How to Add a Component Template to an Existing Composable Index Template

Time:08-20

Goals:

  • Tweak the default "logs" composable index template that ships with Elasticsearch 8.
  • Change the number or replicas to 0.
  • Use the REST API

I can create a component template without issue:

PUT _component_template/no-replicas
{
  "template": {
    "settings": {
      "index": {
        "number_of_replicas": "0"
      }
    }
  }
}

However, I cannot figure out how to add the component template to the pre-installed "logs" index template, because the Create or update index template API seems to require that I pass the entire index template configuration at once.

I don't want to lose or overwrite any defaults that already exist in the "logs" index template.

Is there a way to do something like this?

PUT /_index_template/logs
{
  "ADD_COMPONENTS" : ["no-replicas"]
}

CodePudding user response:

The default logs index template is composed of the logs-mappings, logs-settings and data-streams-mappings component templates.

So you can simply retrieve logs-settings...

GET _component_template/logs-settings

...add whatever settings you need to it, and store it again:

PUT _component_template/logs-settings
{
  "template": {
    "settings": {
      "index": {
        "number_of_replicas": 0,            <---- only added this
        "lifecycle": {
          "name": "logs"
        },
        "codec": "best_compression",
        "query": {
          "default_field": [
            "message"
          ]
        }
      }
    }
  },
  "version": 2,                             <---- modified this
  "_meta": {
    "description": "default settings for the logs index template installed by x-pack",
    "managed": true
  }
}

That's it, the next logs-* index that's going to be created out of the logs index template won't have any replicas.

  • Related