1

I'm using the go-elasticsearch API in my application to create indices in an Elastic.co cloud cluster. The application dynamically creates an index with a template and then starts indexing documents. The template includes an alias name and look like this:

{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text"
      },
      "created_at": {
        "type": "date"
      },
      "updated_at": {
        "type": "date"
      },
      "status": {
        "type": "keyword"
      }
    }
  },
  "aliases": {
    "rollout-nodes-f0776f0": {}
  }
}

The name of the alias can change, so we pass it to the template when we create a new index. This is done with the Create indices API in Go:

indexTemplate := getIndexTemplate()
res, err := n.client.Indices.Create(
    indexName,
    n.client.Indices.Create.WithBody(indexTemplate),
    n.client.Indices.Create.WithContext(ctx),
    n.client.Indices.Create.WithTimeout(time.Second),
)

Doing some testing, this code works on localhost (without security enabled) but is not working with the cluster in Elastic.co, the index is created but not the alias. I think it should be a problem related with either the API Key permissions or some configuration in the server, but I was unable to find yet which permission I'm missing.

For more context, this is the API Key I'm using:

{
      "id": "fakeID",
      "name": "index-service-key",
      "creation": 1675350573126,
      "invalidated": false,
      "username": "fakeUser",
      "realm": "cloud-saml-kibana",
      "metadata": {},
      "role_descriptors": {
        "logstash_writer": {
          "cluster": [
            "monitor",
            "transport_client",
            "read_ccr",
            "read_ilm",
            "manage_index_templates"
          ],
          "indices": [
            {
              "names": [
                "*"
              ],
              "privileges": [
                "all"
              ],
              "allow_restricted_indices": false
            }
          ],
          "applications": [],
          "run_as": [],
          "metadata": {},
          "transient_metadata": {
            "enabled": true
          }
        }
      }
    }

Any ideas? I know I can use the POST _aliases API, but the index creation option should be working too.

ef0ntan
  • 53
  • 1
  • 9
  • You should manage your templates directly inside ES using the [`_index_template` endpoint](https://www.elastic.co/guide/en/elasticsearch/reference/current/index-templates.html), much better than spending a call just for creating the index. – Val Feb 06 '23 at 16:24
  • I can't use static templates inside ES sadly, I need to create the indices on runtime – ef0ntan Feb 06 '23 at 20:29
  • I'm curious why you can't use templates, can you elaborate? – Val Feb 06 '23 at 20:54

0 Answers0