---
type: api
title: Create or update an alias
description: **All methods and paths for this operation:**
url: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch-serverless/operation/operation-indices-put-alias
resource: https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch-serverless/operation/operation-indices-put-alias
products:
  - Elasticsearch Serverless
---

# Create or update an alias


Availability:

## Paths

- `POST` (also `PUT`) `/{index}/_aliases/{name}`
- `POST` (also `PUT`) `/{index}/_alias/{name}`

- [Api key auth](/elastic/docs-builder/docs/4371/api/doc/elasticsearch-serverless/authentication#apikeyauth)

#### Path Parameters

- `index` (union IndexName | [] IndexName)
  Comma-separated list of data streams or indices to add.
Supports wildcards (`*`).
Wildcard patterns that match both data streams and indices return an error.
- `name` (string Name)
  Alias to update.
If the alias doesn’t exist, the request creates it.
Index alias names support date math.

## Description

Adds a data stream or index to an alias.

[Aliases](https://www.elastic.co/docs/manage-data/data-store/aliases)

## Query String Parameters

- `master_timeout` (string Duration · default: 30s)
  Period to wait for a connection to the master node.
If no response is received before the timeout expires, the request fails and returns an error.
- `timeout` (string Duration · default: 30s)
  Period to wait for a response.
If no response is received before the timeout expires, the request fails and returns an error.

## Request Body

`application/json`

- `filter` ({} QueryContainer) — optional
  Query used to limit documents the alias can access.
  See [QueryContainer](https://www.elastic.co/elastic/docs-builder/docs/4371/api/doc/elasticsearch-serverless/types/_types-query_dsl-querycontainer)
- `index_routing` (string) — optional
  Value used to route indexing operations to a specific shard.
If specified, this overwrites the `routing` value for indexing operations.
Data stream aliases don’t support this parameter.
- `is_write_index` (boolean) — optional
  If `true`, sets the write index or data stream for the alias.
If an alias points to multiple indices or data streams and `is_write_index` isn’t set, the alias rejects write requests.
If an index alias points to one index and `is_write_index` isn’t set, the index automatically acts as the write index.
Data stream aliases don’t automatically set a write data stream, even if the alias points to one data stream.
- `routing` (string) — optional
  Value used to route indexing and search operations to a specific shard.
Data stream aliases don’t support this parameter.
- `search_routing` (string) — optional
  Value used to route search operations to a specific shard.
If specified, this overwrites the `routing` value for search operations.
Data stream aliases don’t support this parameter.

## Response

`application/json`

- `acknowledged` (boolean)
  For a successful response, this value is always true. On failure, an exception is returned instead.
## Code Examples

#### Console

```console
POST /my-index-2099.05.06-000001/_alias/my-alias
{
  "filter": {
    "bool": {
      "filter": [
        {
          "range": {
            "@timestamp": {
              "gte": "now-1d/d",
              "lt": "now/d"
            }
          }
        },
        {
          "term": {
            "user.id": "kimchy"
          }
        }
      ]
    }
  }
}
```

#### JavaScript

```javascript
const response = await client.indices.putAlias({
  index: "my-index-2099.05.06-000001",
  name: "my-alias",
  filter: {
    bool: {
      filter: [
        {
          range: {
            "@timestamp": {
              gte: "now-1d/d",
              lt: "now/d",
            },
          },
        },
        {
          term: {
            "user.id": "kimchy",
          },
        },
      ],
    },
  },
});
```

#### Python

```python
resp = client.indices.put_alias(
    index="my-index-2099.05.06-000001",
    name="my-alias",
    filter={
        "bool": {
            "filter": [
                {
                    "range": {
                        "@timestamp": {
                            "gte": "now-1d/d",
                            "lt": "now/d"
                        }
                    }
                },
                {
                    "term": {
                        "user.id": "kimchy"
                    }
                }
            ]
        }
    },
)
```

#### curl

```curl
curl -X POST "$ELASTICSEARCH_URL/my-index-2099.05.06-000001/_alias/my-alias" \
  -H "Authorization: ApiKey $ELASTIC_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "filter": {
      "bool": {
        "filter": [
          {
            "range": {
              "@timestamp": {
                "gte": "now-1d/d",
                "lt": "now/d"
              }
            }
          },
          {
            "term": {
              "user.id": "kimchy"
            }
          }
        ]
      }
    }
  }'
```

#### Java

```java
client.indices().putAlias(p -> p
    .filter(f -> f
        .bool(b -> b
            .filter(List.of(Query.of(q -> q
                    .range(r -> r
                        .untyped(u -> u
                            .field("@timestamp")
                            .gte(JsonData.fromJson("\"now-1d/d\""))
                            .lt(JsonData.fromJson("\"now/d\""))
                        )
                    )
                ),Query.of(qu -> qu
                    .term(t -> t
                        .field("user.id")
                        .value(FieldValue.of("kimchy"))
                    )
                )))
        )
    )
    .index("my-index-2099.05.06-000001")
    .name("my-alias")
);
```

#### C#

```c#
var response = await client.Indices
    .PutAliasAsync(indices: new[] { "my-index-2099.05.06-000001" }, name: "my-alias", d1 => d1
        .Filter(d2 => d2
            .Bool(d3 => d3
                .Filter(d4 => d4
                    .Range(new UntypedRangeQuery()
                    {
                        Field = global::Elastic.Clients.Elasticsearch.Infer.Field<MyDocument>(x => x.Timestamp),
                        Gte = "now-1d/d",
                        Lt = "now/d"
                    }), d4 => d4
                    .Term(d5 => d5
                        .Field(x => x.User.Id)
                        .Value("kimchy")
                    )
                )
            )
        )
    );
```

#### PHP

```php
$resp = $client->indices()->putAlias([
    "index" => "my-index-2099.05.06-000001",
    "name" => "my-alias",
    "body" => [
        "filter" => [
            "bool" => [
                "filter" => array(
                    [
                        "range" => [
                            "@timestamp" => [
                                "gte" => "now-1d/d",
                                "lt" => "now/d",
                            ],
                        ],
                    ],
                    [
                        "term" => [
                            "user.id" => "kimchy",
                        ],
                    ],
                ),
            ],
        ],
    ],
]);
```

#### Ruby

```ruby
response = client.indices.put_alias(
  index: "my-index-2099.05.06-000001",
  name: "my-alias",
  body: {
    "filter": {
      "bool": {
        "filter": [
          {
            "range": {
              "@timestamp": {
                "gte": "now-1d/d",
                "lt": "now/d"
              }
            }
          },
          {
            "term": {
              "user.id": "kimchy"
            }
          }
        ]
      }
    }
  }
)
```

