docs-site/content/0.24.0/api/collection-alias.md
An alias is a virtual collection name that points to a real collection. If you're familiar with symbolic links on Linux, it's very similar to that.
Aliases are useful when you want to reindex your data in the background on a new collection and switch your application to it without any changes to your code. Let's take an example.
Let's say we have a collection called companies_june10 and an alias called companies pointing to that collection.
companies ---> companies_june10
On the next day (June 11), we will create a new collection called companies_june11 and start indexing the documents in the background into this collection. When we are done indexing, if we updated the companies alias to point to this new collection, your application would immediately start querying against the freshly indexed collection.
companies ---> companies_june11
Convenient isn't it? Let's now look at how we can create, update and manage aliases.
aliased_collection = {
'collection_name': 'companies_june11'
}
// Creates/updates an alias called `companies` to the `companies_june11` collection
client.aliases().upsert('companies', aliased_collection)
$aliasedCollection = [
'collection_name' => 'companies_june11'
]
# Creates/updates an alias called `companies` to the `companies_june11` collection
$client->aliases->upsert('companies', $aliasedCollection)
aliased_collection = {
'collection_name': 'companies_june11'
}
# Creates/updates an alias called `companies` to the `companies_june11` collection
client.aliases.upsert('companies', aliased_collection)
aliased_collection = {
'collection_name' => 'companies_june11'
}
# Creates/updates an alias called `companies` to the `companies_june11` collection
client.aliases.upsert('companies', aliased_collection)
final aliasedCollection = {
'collection_name': 'companies_june11'
};
// Creates/updates an alias called `companies` to the `companies_june11` collection
await client.aliases.upsert('companies', aliased_collection);
CollectionAliasSchema collectionAlias = new CollectionAliasSchema();
collectionAlias.collectionName("companies_june11");
client.aliases().upsert("companies", collectionAlias);
let collection = CollectionAliasSchema(collectionName: "companies_june11")
// Creates/updates an alias called `companies` to the `companies_june11` collection
let (collectionAlias, response) = try await client.aliases().upsert(name: "companies", collection: collection)
curl "http://localhost:8108/aliases/companies" -X PUT \
-H "Content-Type: application/json" \
-H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" -d '{
"collection_name": "companies_june11"
}'
{
"name": "companies",
"collection_name": "companies_june11",
}
PUT ${TYPESENSE_HOST}/aliases/:alias
| Parameter | Required | Description |
|---|---|---|
| collection_name | yes | Name of the collection you wish to map the alias to. |
We can find out which collection an alias points to by fetching it.
<Tabs :tabs="['JavaScript','PHP','Python','Ruby','Dart','Java','Swift','Shell']"> <template v-slot:JavaScript>client.aliases('companies').retrieve()
$client->aliases['companies']->retrieve()
client.aliases['companies'].retrieve()
client.aliases['companies'].retrieve
await client.alias('companies').retrieve();
CollectionAliasSchema collectionAlias = client.aliases("companies").retrieve();
let (collectionAlias, response) = try await client.aliases().retrieve(name: "companies")
curl -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
"http://localhost:8108/aliases/companies"
{
"name": "companies",
"collection_name": "companies_june11",
}
GET ${TYPESENSE_HOST}/aliases/:alias
List all aliases and the corresponding collections that they map to.
<Tabs :tabs="['JavaScript','PHP','Python','Ruby','Dart','Java','Swift','Shell']"> <template v-slot:JavaScript>client.aliases().retrieve()
$client->aliases->retrieve()
client.aliases.retrieve()
client.aliases.retrieve
await client.aliases.retrieve();
CollectionAliasesResponse collectionAliasesResponse = client.aliases().retrieve();
let (collectionAliases, response) = try await client.aliases().retrieve()
curl -H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" \
"http://localhost:8108/aliases"
{
"aliases": [
{
"name": "companies",
"collection_name": "companies_june11"
},
{
"name": "employees",
"collection_name": "employees_june11"
}
]
}
GET ${TYPESENSE_HOST}/aliases
client.aliases('companies').delete()
$client->aliases['companies']->delete()
client.aliases['companies'].delete()
client.aliases['companies'].delete
await client.alias('companies').delete();
CollectionAliasSchema collectionAlias = client.aliases("companies").delete();
let (collectionAlias, response) = try await client.aliases().delete(name: "companies")
curl "http://localhost:8108/aliases/companies" -X DELETE
-H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}"
{
"name": "companies",
"collection_name": "companies_june11"
}
DELETE ${TYPESENSE_HOST}/aliases/:alias