# Aliases

cbElasticSearch offers the `AliasBuilder` for assistance in adding and removing index aliases.

For creating an alias:

```js
getWireBox().getInstance( "AliasBuilder@cbElasticSearch" )
    .add( indexName = "myIndex", aliasName = "newAlias" )
    .save();
```

For removing an alias:

```js
getWireBox().getInstance( "AliasBuilder@cbElasticSearch" )
    .remove( indexName = "otherIndex", aliasName = "randomAlias" )
    .save();
```

For bulk operations, use the cbElasticSearch client's `applyAliases` method. These operations are performed in the same transaction (i.e. atomic), so it's safe to use for switching the alias from one index to another.

```js
var removeAliasAction = getWireBox().getInstance( "AliasBuilder@cbElasticSearch" )
    .remove( indexName = "testIndexName", aliasName = "aliasNameOne" );
var addNewAliasAction = getWireBox().getInstance( "AliasBuilder@cbElasticSearch" )
    .add( indexName = "testIndexName", aliasName = "aliasNameTwo" );

variables.client.applyAliases(
    // a single alias action can also be provided
    aliases = [ removeAliasAction, addNewAliasAction ]
);
```

## Retrieving Aliases

The client's `getAliases` method allows you to retrieve a map containing information on aliases in use in the connected cluster.

```js
var aliasMap = getInstance( "Client@cbelasticsearch" ).getAliases();
```

The corresponding object will have two keys: `aliases` and `unassigned`. The former is a map of aliases with their corresponding index, the latter is an array of indexes which are unassigned to any alias.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cbelasticsearch.ortusbooks.com/indices/aliases.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
