An alias in Milvus typically refers to a substitute name for a collection that allows it to uniquely identify itself in the cluster. Collection aliases in Milvus are globally unique; hence, you cannot assign the same alias to multiple collections. However, you can create multiple aliases in a single collection.
In this tutorial, we will learn how we can create an alias for a given collection in Milvus using the PyMilvus package.
Requirements:
To follow along with this post, ensure that you have the following:
- Access to a Milvus server
- Python 3.10+ and above
- Installed PyMilvus
- Installed Milvus CLI
Setup the Sample Collection
Before proceeding, let us ensure that we have a test collection for demonstration purposes. If you already have a collection that you wish to use, feel free to skip this section.
The previous command should create a collection to store the data about cars.
Create an Alias in PyMilvus
Once we define it, we can build the Python code to create an alias for the collection.
The source code is as demonstrated in the following:
utility.create_alias(
collection_name = "car",
alias = "garage"
)
In the previous code, we use the “utility.create_alias” function from the “pymilvus.utility” class to create an alias for the “car” collection.
The method accepts two main arguments:
- Collection_name – It specifies the collection name on which you wish to create an alias.
- Alias – The target alias.
Create a Collection in Milvus CLI
If you have access to the Milvus CLI, you can use the “create alias” command to setup a new alias for the collection name.
The command syntax is as follows:
Where:
- The -c refers to the collection name.
- The -a specifies the alias name.
For example, to create two aliases for the “car” collection, you can use the commands as follows:
Drop an Alias Using PyMilvus
To drop an existing collection alias, you can use the following code:
utility.drop_alias(alias = "garage")
The drop_alias() function accepts the name of the alias that you wish to drop.
Delete a Collection Using the Milvus CLI
To delete a collection alias using the Milvus CLI, you can use the command syntax as shown in the following:
For example, the following command deletes the “garage” alias from the “car” collection.
Alter a Collection Alias Using PyMilvus
We can also alter an existing collection to another alias. An example is as follows:
utility.alter_alias(
collection_name = "car",
alias = "garage"
)
The alter_alias() function accepts two main arguments:
- Collection_name – The name of the collection to alter the alias to.
- Alias – The collection alias to alter.
Conclusion
This tutorial covers all the fundamental actions that you can use to create, alter, and delete the collection alias in Milvus using various techniques.