In Milvus, an entity refers to the most basic data unit. Think of an entity as an instance in the database that is identified by a unique ID composed of several attributes.
For example, each product could be considered an entity in a product recommendation system. The attributes of it could be things like product title, description, price, etc.
Entities in Milvus are stored in data collections which is similar to a table in a relational database.
This tutorial teaches us how to insert and delete the entities in a Milvus collection using Python and the PyMilvus SDK.
Requirements:
Make sure that you have the following installed:
- Milvus server
- PyMilvus (Python SDK for Milvus)
- NumPy
You can install PyMilvus and NumPy using pip:
Establish a Connection to the Milvus Server
First, import the necessary modules and connect to the Milvus server:
import numpy as np
connections.connect(alias='default')
print(f"\nSuccessfully connected to Milvus server.")
Create a Collection and Insert the Data
Next, we create a collection and insert some data using NumPy as shown in the following code:
# Define a schema for the collection
dim = 128
fields = [
FieldSchema(name="id", dtype=DataType.INT64, is_primary=True, auto_id=True),
FieldSchema(name="embedding", dtype=DataType.FLOAT_VECTOR, dim=dim)
]
schema = CollectionSchema(fields=fields, description="Sample collection")
# Create a collection
collection = Collection(name="sample_collection", schema=schema)
# Generate some vectors to insert into the collection
vectors = np.random.random((10, dim)).tolist()
data = [
("embedding", vectors)
]
# Insert the data into the collection
ids = collection.insert(data)
print(f"\nSuccessfully inserted {len(ids)} vectors into collection.")
Load the Data and Get the Entity
Now, flush the collection data to the disk and get the entities:
collection.load()
print("\nSuccessfully flushed data to disk.")
# Get the entities
entities = collection.get_entities(ids)
print(f"\nRetrieved {len(entities)} entities.")
Delete the Entities
Next, we delete some entities by their IDs:
status = collection.delete(ids_to_delete)
print(f"\nSuccessfully deleted entities with ids: {ids_to_delete}")
Verify the Deletion
Lastly, we can verify that the entities have been successfully deleted as follows:
# get the deleted entities
deleted_entities = collection.get_entities(ids_to_delete)
print(f"\nRetrieved {len(deleted_entities)} entities.")
Conclusion
This tutorial demonstrates how to delete the entities from a Milvus collection using NumPy and PyMilvus.