Run Milvus using Python

After the Milvus server boots successfully, test the platform using our Python sample code.

  1. Install pymilvus_orm and its dependencies:
pip install pymilvus-orm==2.0.0rc2
Python version 3.6 or higher is required. View Python documentation for information about installing the correct version for your system.
  1. Download sample code
$ wget
  1. Scan This sample code does the following:
  • Imports the pymilvus package:
from pymilvus_orm import connections, FieldSchema, CollectionSchema, DataType, Collection
  • Connects to the Milvus server:
  • Creates a collection:
dim = 128
default_fields = [
    FieldSchema(name="count", dtype=DataType.INT64, is_primary=True),
    FieldSchema(name="random_value", dtype=DataType.DOUBLE),
    FieldSchema(name="float_vector", dtype=DataType.FLOAT_VECTOR, dim=dim)
default_schema = CollectionSchema(fields=default_fields, description="test collection")

print(f"\nCreate collection...")
collection = Collection(name="hello_milvus", schema=default_schema)
  • Inserts vectors in the new collection:
import random
nb = 3000
vectors = [[random.random() for _ in range(dim)] for _ in range(nb)]
        [i for i in range(nb)],
        [float(random.randrange(-20,-10)) for _ in range(nb)],
  • Builds an IVF_FLAT index and loads the collection to memory:
default_index = {"index_type": "IVF_FLAT", "params": {"nlist": 128}, "metric_type": "L2"}
collection.create_index(field_name="float_vector", index_params=default_index)
  • Conducts a vector similarity search:
topK = 5
search_params = {"metric_type": "L2", "params": {"nprobe": 10}}
# define output_fields of search result
res =
    vectors[-2:], "float_vector", search_params, topK,
    "count > 100", output_fields=["count", "random_value"]
  1. Run
$ python3

The returned results and query latency show as follows:

Returned results

Congratulations! You have successfully booted Milvus Standalone and run your first vector similarity search.

Is this page helpful?
Scored Successfully!