This chapter introduces how to quickly get started with Milvus development using Python.
By running the sample code we provide, you will have a preliminary understanding of Milvus's functions.
Version Requirements
- Milvus 2.3.0
- Python 3 (3.7.1 or higher)
- PyMilvus 2.3.x
Install Milvus Python SDK
python3 -m pip install pymilvus==
Download Sample Code
Use the following command to directly download hello_milvus.py or use the following command for downloading.
$ wget https://www.tizi365.com/storage/hello_milvus.py
Explanation of Sample Code
The sample code performs the following steps.
- Import the PyMilvus package:
from pymilvus import (
connections,
utility,
FieldSchema,
CollectionSchema,
DataType,
Collection,
)
- Connect to the server:
connections.connect("default", host="localhost", port="19530")
- Create a collection:
fields = [
FieldSchema(name="pk", dtype=DataType.INT64, is_primary=True, auto_id=False),
FieldSchema(name="random", dtype=DataType.DOUBLE),
FieldSchema(name="embeddings", dtype=DataType.FLOAT_VECTOR, dim=8)
]
schema = CollectionSchema(fields, "hello_milvus is the simplest demo example, used to introduce the API")
hello_milvus = Collection("hello_milvus", schema)
- Insert vectors into the collection:
import random
entities = [
[i for i in range(3000)], # field pk
[float(random.randrange(-20, -10)) for _ in range(3000)], # field random
[[random.random() for _ in range(8)] for _ in range(3000)], # field embeddings
]
insert_result = hello_milvus.insert(entities)
hello_milvus.flush()
- Create an index on the entities:
index = {
"index_type": "IVF_FLAT",
"metric_type": "L2",
"params": {"nlist": 128},
}
hello_milvus.create_index("embeddings", index)
- Load the collection into memory and perform vector similarity search:
hello_milvus.load()
vectors_to_search = entities[-1][-2:]
search_params = {
"metric_type": "L2",
"params": {"nprobe": 10},
}
result = hello_milvus.search(vectors_to_search, "embeddings", search_params, limit=3, output_fields=["random"])
- Perform vector queries:
result = hello_milvus.query(expr="random > -14", output_fields=["random", "embeddings"])
- Perform mixed search:
result = hello_milvus.search(vectors_to_search, "embeddings", search_params, limit=3, expr="random > -12", output_fields=["random"])
- Delete entities based on primary key:
expr = f"pk in [{entities[0][0]}, {entities[0][1]}]"
hello_milvus.delete(expr)
- Delete the collection:
utility.drop_collection("hello_milvus")
Running the sample code
Execute the following command to run the sample code.
$ python3 hello_milvus.py
Below shows the returned results and query latency:
=== Connecting to Milvus ===
Does the collection named hello_milvus exist in Milvus: False
=== Creating collection `hello_milvus` ===
=== Inserting entities ===
Number of entities in Milvus: 3000
=== Creating IVF_FLAT index ===
=== Loading ===
=== Searching based on vector similarity ===
Match: (Distance: 0.0, ID: 2998), Random Field: -11.0
Match: (Distance: 0.11455299705266953, ID: 1581), Random Field: -18.0
Match: (Distance: 0.1232629269361496, ID: 2647), Random Field: -13.0
Match: (Distance: 0.0, ID: 2999), Random Field: -11.0
Match: (Distance: 0.10560893267393112, ID: 2430), Random Field: -18.0
Match: (Distance: 0.13938161730766296, ID: 377), Random Field: -14.0
Search latency = 0.2796 seconds
=== Querying using `random > -14` ===
Query result:
-{'pk': 9, 'random': -13.0, 'embeddings': [0.298433, 0.931987, 0.949756, 0.598713, 0.290125, 0.094323, 0.064444, 0.306993]}
Search latency = 0.2970 seconds
=== Performing mixed search using `random > -12` ===
Match: (Distance: 0.0, ID: 2998), Random Field: -11.0
Match: (Distance: 0.15773043036460876, ID: 472), Random Field: -11.0
Match: (Distance: 0.3273330628871918, ID: 2146), Random Field: -11.0
Match: (Distance: 0.0, ID: 2999), Random Field: -11.0
Match: (Distance: 0.15844076871871948, ID: 2218), Random Field: -11.0
Match: (Distance: 0.1622171700000763, ID: 1403), Random Field: -11.0
Search latency = 0.3028 seconds
=== Deleting using expression `pk in [0, 1]` ===
Querying before deletion based on expression `pk in [0, 1]` -> Result:
-{'pk': 0, 'random': -18.0, 'embeddings': [0.142279, 0.414248, 0.378628, 0.971863, 0.535941, 0.107011, 0.207052, 0.98182]}
-{'pk': 1, 'random': -15.0, 'embeddings': [0.57512, 0.358512, 0.439131, 0.862369, 0.083284, 0.294493, 0.004961, 0.180082]}
Querying after deletion based on expression `pk in [0, 1]` -> Result: []
=== Deleting collection `hello_milvus` ===