AI

Delete Entities in Milvus

Milvus is an open-source vector database that facilitates the similarity search and AI analytics. It can manage and search the massive feature vectors with high performance and scalability, making it an ideal tool for artificial intelligence, machine learning, and other data-intensive applications.

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:

pip install pymilvus numpy

Establish a Connection to the Milvus Server

First, import the necessary modules and connect to the Milvus server:

from pymilvus import connections, DataType

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:

from pymilvus import Collection, FieldSchema, CollectionSchema

# 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:

# Load data to memory
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:

ids_to_delete = ids[:5]
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:

collection.load()
# 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.

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