lauthu
lauthu

Reputation: 306

use wildcard in ElasticSearch index template aliases

This is my index template(simplified):

{
  "template": "user-*",
  "mappings" : {
    "ESHOP_USER" : {
      "properties" : {
        "id" : {
          "type" : "long"
        },
        "nickname" : {
          "type" : "text"
        },
        "createTime" : {
          "type" : "keyword"
        },
        "updateTime" : {
          "type" : "keyword"
        }
      }
    }
  },
  "aliases" : {
      "user-test-alias" : {
          "index" : "user-test*"
      },
      "user-prod-alias" : {
          "index" : "user-prod*"
      }
  }
}

What I want to do:

Indices with name pattern user-* share the same template, and I want to add user-test-alias to all indices with name pattern user-test*, and user-prod-alias to all indices with name pattern user-prod*.

What I got:

With above template, I got all indices with name pattern user-* getting two aliases: user-test-alias and user-prod-alias.

I knew if I split this template to test and prod templates, or use POST /_aliases after indices creation, I can solve the problem. But is there some way to achieve my goal with only one index template?

Upvotes: 1

Views: 1972

Answers (1)

Val
Val

Reputation: 217304

What I would do is to use 3 templates:

  • one that is common to both environments
  • one for test (just for the alias)
  • one for prod (just for the alias as well)

First template (common) applied first:

{
  "template": "user-*",
  "order": 0,
  "mappings" : {
    "ESHOP_USER" : {
      "properties" : {
        "id" : {
          "type" : "long"
        },
        "nickname" : {
          "type" : "text"
        },
        "createTime" : {
          "type" : "keyword"
        },
        "updateTime" : {
          "type" : "keyword"
        }
      }
    }
  }
}

Second template (test alias) applied next:

{
  "template": "user-test-*",
  "order": 1,
  "aliases" : {
      "user-test-alias" : {
          "index" : "user-test*"
      }
  }
}

Third template (prod alias) applied next:

{
  "template": "user-prod-*",
  "order": 1,
  "aliases" : {
      "user-prod-alias" : {
          "index" : "user-prod*"
      }
  }
}

Upvotes: 3

Related Questions