AI

Manage Collection Aliases in Milvus

Milvus is a free, open-source vector database for similarity searches and AI applications. It’s highly scalable, robust, and reliable which enables you to build the applications such as recommendation systems, image search, text search, etc.

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:

  1. Access to a Milvus server
  2. Python 3.10+ and above
  3. Installed PyMilvus
  4. 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.

milvus_cli > create collection -c car -f id:INT64:primary_field -f vector:FLOAT_VECTOR:128 -f color:INT64:color -f brand:INT64:brand -p id -a -d 'car_collection'

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:

from pymilvus import utility

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:

  1. Collection_name – It specifies the collection name on which you wish to create an alias.
  2. 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:

create alias -c (text) -a (text) [-A] [-t (float)]

Where:

  1. The -c refers to the collection name.
  2. The -a specifies the alias name.

For example, to create two aliases for the “car” collection, you can use the commands as follows:

milvus_cli > create alias -c car -a garage -a carAlias2

Drop an Alias Using PyMilvus

To drop an existing collection alias, you can use the following code:

from pymilvus import utility

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:

delete alias -a (text) [-t (float)]

For example, the following command deletes the “garage” alias from the “car” collection.

Milvus_cli > delete alias -a garage

Alter a Collection Alias Using PyMilvus

We can also alter an existing collection to another alias. An example is as follows:

from pymilvus import utility

utility.alter_alias(

collection_name = "car",

alias = "garage"

)

The alter_alias() function accepts two main arguments:

  1. Collection_name – The name of the collection to alter the alias to.
  2. 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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list