Manage Aliases
In Milvus, an alias is a secondary, mutable name for a collection. Using aliases provides a layer of abstraction that allows you to dynamically switch between collections without modifying your application code. This is particularly useful in production environments for seamless data updates, A/B testing, and other operational tasks.
This page demonstrates how to create, list, reassign, and drop collection aliases.
Why Use an Alias?
The primary benefit of using an alias is to decouple your client application from a specific, physical collection name.
Imagine you have a live application that queries a collection named prod_data
. When you need to update the underlying data, you can perform the update without any service interruption. The workflow would be:
- Create a New Collection: Create a new collection, for instance,
prod_data_v2
. - Prepare Data: Load and index the new data in
prod_data_v2
. - Switch the Alias: Once the new collection is ready for service, atomically reassign the alias
prod_data
from the old collection toprod_data_v2
.
Your application continues to send requests to the alias prod_data
, experiencing zero downtime. This mechanism enables seamless updates and simplifies operations like blue-green deployments for your vector search service.
Key Properties of Aliases:
- A collection can have multiple aliases.
- An alias can only point to one collection at a time.
- When processing a request, Milvus first checks if a collection with the provided name exists. If not, it then checks if the name is an alias for a collection.
Create Alias
The following code snippet demonstrates how to create an alias for a collection.
from pymilvus import MilvusClient
client = MilvusClient(
uri="http://localhost:19530",
token="root:Milvus"
)
# 9. Manage aliases
# 9.1. Create aliases
client.create_alias(
collection_name="my_collection_1",
alias="bob"
)
client.create_alias(
collection_name="my_collection_1",
alias="alice"
)
import io.milvus.v2.service.utility.request.CreateAliasReq;
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
String CLUSTER_ENDPOINT = "http://localhost:19530";
String TOKEN = "root:Milvus";
// 1. Connect to Milvus server
ConnectConfig connectConfig = ConnectConfig.builder()
.uri(CLUSTER_ENDPOINT)
.token(TOKEN)
.build();
MilvusClientV2 client = new MilvusClientV2(connectConfig);
// 9. Manage aliases
// 9.1 Create alias
CreateAliasReq createAliasReq = CreateAliasReq.builder()
.collectionName("my_collection_1")
.alias("bob")
.build();
client.createAlias(createAliasReq);
createAliasReq = CreateAliasReq.builder()
.collectionName("my_collection_1")
.alias("alice")
.build();
client.createAlias(createAliasReq);
import { MilvusClient, DataType } from "@zilliz/milvus2-sdk-node";
const address = "http://localhost:19530";
const token = "root:Milvus";
const client = new MilvusClient({address, token});
// 9. Manage aliases
// 9.1 Create aliases
res = await client.createAlias({
collection_name: "my_collection_1",
alias: "bob"
})
console.log(res.error_code)
// Output
//
// Success
//
res = await client.createAlias({
collection_name: "my_collection_1",
alias: "alice"
})
console.log(res.error_code)
// Output
//
// Success
//
import (
"context"
"fmt"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
milvusAddr := "localhost:19530"
client, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
fmt.Println(err.Error())
// handle error
}
defer client.Close(ctx)
err = client.CreateAlias(ctx, milvusclient.NewCreateAliasOption("my_collection_1", "bob"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
err = client.CreateAlias(ctx, milvusclient.NewCreateAliasOption("my_collection_1", "alice"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/aliases/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"aliasName": "bob",
"collectionName": "my_collection_1"
}'
# {
# "code": 0,
# "data": {}
# }
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/aliases/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"aliasName": "alice",
"collectionName": "my_collection_1"
}'
# {
# "code": 0,
# "data": {}
# }
List Aliases
The following code snippet demonstrates the procedure to list the aliases allocated to a specific collection.
# 9.2. List aliases
res = client.list_aliases(
collection_name="my_collection_1"
)
print(res)
# Output
#
# {
# "aliases": [
# "bob",
# "alice"
# ],
# "collection_name": "my_collection_1",
# "db_name": "default"
# }
import io.milvus.v2.service.utility.request.ListAliasesReq;
import io.milvus.v2.service.utility.response.ListAliasResp;
// 9.2 List alises
ListAliasesReq listAliasesReq = ListAliasesReq.builder()
.collectionName("my_collection_1")
.build();
ListAliasResp listAliasRes = client.listAliases(listAliasesReq);
System.out.println(listAliasRes.getAlias());
// Output:
// [bob, alice]
// 9.2 List aliases
res = await client.listAliases({
collection_name: "my_collection_1"
})
console.log(res.aliases)
// Output
//
// [ 'bob', 'alice' ]
//
aliases, err := client.ListAliases(ctx, milvusclient.NewListAliasesOption("my_collection_1"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println(aliases)
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/aliases/list" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{}'
# {
# "code": 0,
# "data": [
# "bob",
# "alice"
# ]
# }
Describe Alias
The following code snippet describes a specific alias in detail, including the name of the collection to which it has been allocated.
# 9.3. Describe aliases
res = client.describe_alias(
alias="bob"
)
print(res)
# Output
#
# {
# "alias": "bob",
# "collection_name": "my_collection_1",
# "db_name": "default"
# }
import io.milvus.v2.service.utility.request.DescribeAliasReq;
import io.milvus.v2.service.utility.response.DescribeAliasResp;
// 9.3 Describe alias
DescribeAliasReq describeAliasReq = DescribeAliasReq.builder()
.alias("bob")
.build();
DescribeAliasResp describeAliasRes = client.describeAlias(describeAliasReq);
System.out.println(describeAliasRes);
// Output:
// DescribeAliasResp(collectionName=my_collection_1, alias=bob)
// 9.3 Describe aliases
res = await client.describeAlias({
collection_name: "my_collection_1",
alias: "bob"
})
console.log(res)
// Output
//
// {
// status: {
// extra_info: {},
// error_code: 'Success',
// reason: '',
// code: 0,
// retriable: false,
// detail: ''
// },
// db_name: 'default',
// alias: 'bob',
// collection: 'my_collection_1'
// }
//
alias, err := client.DescribeAlias(ctx, milvusclient.NewDescribeAliasOption("bob"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println(alias)
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/aliases/describe" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"aliasName": "bob"
}'
# {
# "code": 0,
# "data": {
# "aliasName": "bob",
# "collectionName": "my_collection_1",
# "dbName": "default"
# }
# }
Alter Alias
You can reallocate the alias already allocated to a specific collection to another.
# 9.4 Reassign aliases to other collections
client.alter_alias(
collection_name="my_collection_2",
alias="alice"
)
res = client.list_aliases(
collection_name="my_collection_2"
)
print(res)
# Output
#
# {
# "aliases": [
# "alice"
# ],
# "collection_name": "my_collection_2",
# "db_name": "default"
# }
res = client.list_aliases(
collection_name="my_collection_1"
)
print(res)
# Output
#
# {
# "aliases": [
# "bob"
# ],
# "collection_name": "my_collection_1",
# "db_name": "default"
# }
import io.milvus.v2.service.utility.request.AlterAliasReq;
// 9.4 Reassign alias to other collections
AlterAliasReq alterAliasReq = AlterAliasReq.builder()
.collectionName("my_collection_2")
.alias("alice")
.build();
client.alterAlias(alterAliasReq);
ListAliasesReq listAliasesReq = ListAliasesReq.builder()
.collectionName("my_collection_2")
.build();
ListAliasResp listAliasRes = client.listAliases(listAliasesReq);
System.out.println(listAliasRes.getAlias());
listAliasesReq = ListAliasesReq.builder()
.collectionName("my_collection_1")
.build();
listAliasRes = client.listAliases(listAliasesReq);
System.out.println(listAliasRes.getAlias());
// Output:
// [bob]
// 9.4 Reassign aliases to other collections
res = await client.alterAlias({
collection_name: "my_collection_2",
alias: "alice"
})
console.log(res.error_code)
// Output
//
// Success
//
res = await client.listAliases({
collection_name: "my_collection_2"
})
console.log(res.aliases)
// Output
//
// [ 'alice' ]
//
res = await client.listAliases({
collection_name: "my_collection_1"
})
console.log(res.aliases)
// Output
//
// [ 'bob' ]
//
err = client.AlterAlias(ctx, milvusclient.NewAlterAliasOption("alice", "my_collection_2"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
aliases, err := client.ListAliases(ctx, milvusclient.NewListAliasesOption("my_collection_2"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println(aliases)
aliases, err = client.ListAliases(ctx, milvusclient.NewListAliasesOption("my_collection_1"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println(aliases)
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/aliases/alter" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"aliasName": "alice",
"collectionName": "my_collection_2"
}'
# {
# "code": 0,
# "data": {}
# }
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/aliases/describe" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"aliasName": "alice"
}'
# {
# "code": 0,
# "data": {
# "aliasName": "alice",
# "collectionName": "my_collection_2",
# "dbName": "default"
# }
# }
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/aliases/describe" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"aliasName": "bob"
}'
# {
# "code": 0,
# "data": {
# "aliasName": "alice",
# "collectionName": "my_collection_1",
# "dbName": "default"
# }
# }
Drop Alias
The following code snippet demonstrates the procedure to drop an alias.
# 9.5 Drop aliases
client.drop_alias(
alias="bob"
)
client.drop_alias(
alias="alice"
)
import io.milvus.v2.service.utility.request.DropAliasReq;
// 9.5 Drop alias
DropAliasReq dropAliasReq = DropAliasReq.builder()
.alias("bob")
.build();
client.dropAlias(dropAliasReq);
dropAliasReq = DropAliasReq.builder()
.alias("alice")
.build();
client.dropAlias(dropAliasReq);
// 9.5 Drop aliases
res = await client.dropAlias({
alias: "bob"
})
console.log(res.error_code)
// Output
//
// Success
//
res = await client.dropAlias({
alias: "alice"
})
console.log(res.error_code)
// Output
//
// Success
//
err = client.DropAlias(ctx, milvusclient.NewDropAliasOption("bob"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
err = client.DropAlias(ctx, milvusclient.NewDropAliasOption("alice"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/aliases/drop" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"aliasName": "bob"
}'
# {
# "code": 0,
# "data": {}
# }
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/aliases/drop" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d '{
"aliasName": "alice"
}'
# {
# "code": 0,
# "data": {}
# }