AI

Connect to the Milvus Server Using Python

Milvus is an open-source vector database that is designed for efficient and scalable similarity search and analytics on a massive-scale vector data.

Milvus provides a powerful solution to manage and query the high-dimensional vectors that represent the images, text, or other complex data types.

At its core, Milvus engages a vector similarity search engine which utilizes the advanced algorithms and data structures to facilitate the fast and accurate nearest-neighbor search operations.

Milvus ensures the efficient measures to handle the computational challenges using indexing, dimensionality reduction, and approximate search methods.

In this tutorial, we will learn how to connect to the Milvus server using Python.

Requirements:

To follow along with this tutorial, ensure that you have the following:

  1. Milvus 2.2.10
  2. Python 3 (3.11.0 or later)
  3. Pip package manager

With the given requirements met, we can proceed with the tutorial.

Installing the PyMilvus Package

PyMilvus is a high-performance open-source Python library that provides an efficient and scalable data indexing and retrieval capabilities for large-scale similarity search applications.

We can install PyMilvus using pip with the following command:

$ sudo pip3 install pymilvus

This should download the PyMilvus package and install it on your system.

Connect to the Server Using PyMilvus

In order to connect to the server, we need to import the “connect” function from the PyMilvus package.

The function syntax is as follows:

connect(alias, **kwargs)

The function accepts the following parameters:

  • Alias – This specifies an alias that is assigned to the connection for easier identification.
  • kwargs: host – It specifies the IP address of the Milvus connection.
  • kwargs: port – It specifies the port of the Milvus connection.
  • kwargs: uri – It specifies the Milvus instance endpoint, usually in the form of <scheme>://<host>:<port>.
  • kwargs: secure – This parameter defines whether TLS/SSL is required to access the endpoint.

The function has no return value but establishes a connection to the specified Milvus server.

Example Code:

The following source code demonstrates how to use the “connect” function to create a connection to the Milvus server:

from pymilvus import connections

connections.connect(

alias="default",

host='localhost',

port='19530'

)

uri="http://localhost:19530"

connections.connect(uri=uri)

The previous code defines the connection parameters including the alias, host, and port. We then use the connections.connect() method to pass the URI of the Milvus server.

You can also connect to a specific collection using the “dbname” parameter as follows:

from pymilvus import connections

connections.connect(

alias="default",

host='localhost',

port='19530'

)

uri="http://localhost:19530"

connections.connect(uri,db_name="film")

In this case, we should connect to the “film” collection as specified in the “db_name” parameter.

Conclusion

We learned how to use the PyMilvus package to establish a connection to the Milvus server using Python. You can check the documentation to explore the package functionality.

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