Connecting to Milvus service

Run any one of the following commands to connect with Milvus.

Before you begin

Make sure that the following items are installed or available:

  • Python and PyMilvus package. For more information, see About PyMilvus.
  • Hostname and port for the Milvus instance. You can get the Milvus host and port information from Infrastructure manager (click the Milvus service to open the Details page and note the host and port information from the GRPC host).
  • Assign a role to the user or service ID from Infrastructure manager. For information about user roles, see Managing roles and privileges.

Procedure

You can connect to a Milvus service by using an API key or an IAM token.

  1. Provision a Milvus service in watsonx.data. For more information, see Adding a Milvus service.
  2. Run one of the following commands by using Python SDK (PyMilvus) to connect with Milvus for the gRPC route:
    • To connect to Milvus using GRPC calls, use one of the following commands:
      print(fmt.format("start connecting to Milvus"))
      connections.connect(host="<host>", port="<port>", secure=True, user="ibmlhapikey_<username>", password="<api-key>")
      has = utility.has_collection("hello_milvus")
      print(f"Does collection hello_milvus exist in Milvus: {has}")

      or

      from pymilvus import MilvusClient, DataType
      
      client = MilvusClient(
          host=<host>,
          port=<port>,
          secure=True,
          user="ibmlhapikey_<username>",
          password="<api-key>"
      )
      Note: Replace <api-key> with the IBM API key.
    • To connect to Milvus using an IAM token, use one of the following commands:
      connections.connect(host="<host>", port="<port>", secure=True, user="ibmlhtoken_<username>", password="<token>")

      or

      from pymilvus import MilvusClient, DataType
      
      milvus_uri = "https://<ibmlhtoken_<username>>:<token>@<host>:<port>"
      client = MilvusClient(
          uri=milvus_uri,
          secure=True
      )
      Note: Replace <token> with the IAM token.
    • To connect to Milvus using a Uniform Resource Identifier (URI), use one of the following commands:
      print(fmt.format("start connecting to Milvus"))
      connections.connect( alias="default", uri="https://<host>:<grpc-port>", user = "ibmlhtoken_<username>", password = "<token>" )
      has = utility.has_collection("hello_milvus")
      print(f"Does collection hello_milvus exist in Milvus: {has}")

      or

      from pymilvus import MilvusClient, DataType
      
      milvus_uri = "https://<ibmlhapikey_<username>>:<api-key>@<host>:<port>"
      client = MilvusClient(
          uri=milvus_uri,
          secure=True
      )
      Note: Replace <token> with the IAM token.
  3. To connect to Milvus using the REST route and make an API call, run the following command:
    curl --request GET \
         --url "https://<REST-host>:<REST-port>/api/v1/vector/collections" \
         --header "Authorization: Basic $(echo -n '<user>:<password>' | base64)" \
         --header "Content-Type: application/json"

    For more information, see RESTful API reference.

    Note: It is recommended to use the APIs through the proxy route. The APIs through the REST route are deprecated and might be removed in a future release.
  4. To connect to Milvus using the proxy route and make an API call, run the following command:
    curl --request POST \
    --url "https://<proxy-host>:<port>/v2/vectordb/collections/list" \
    --header "Authorization: Basic $(echo -n 'user:password' | base64)" \
    --header "Content-Type: application/json" \
    -d '{
        "dbName": "default"
    }'

    For example:

    curl --request POST \
    --url "https://e633a638-420b-499c-9291-288e425a2d25.cdc406pd09pasng7elgg.lakehouse.dev.appdomain.cloud:32699/v2/vectordb/collections/list" \
    --header 'accept: application/json' \
    --header "Authorization: Basic $(echo -n 'ibmlhapikey_<username>:<value of api key>' | base64)" \
    --header 'Content-Type: application/json'
    -d '{
        "dbName": "default"
    }'
Note: As of watsonx.data version 2.2, it is recommended to use API version v2/. API version v1/ might be deprecated and removed in a future release.
Important: Under Milvus version v2/ APIs, the User and Roles category of APIs are not supported for the Milvus service in watsonx.data. For more information, see Milvus APIs.

What to do next

You can perform the following operations after establishing a connection with a Milvus service:

Manage data
Manage databases
You can create databases in Milvus and allocate privileges to certain users to manage them. A Milvus cluster supports a maximum of 64 databases. For more information, see Manage Databases.
Manage collections
You can create and manage collections using the SDK of your choice. A Milvus cluster supports a maximum of 65,536 collections. For more information, see Manage Collections.
Manage partitions
You can create and manage partitions in a collection. A Milvus cluster supports a maximum of 4095 partitions. For more information, see Manage Partitions.
Manage indexes
You can create and manage indexes on vector fields in a collection. For more information, see Manage Indexes.
Search and Query
For information about searching and querying data in Milvus, see Search, Query and Get.

Related API

For information on related API, see: