Manage vector indexes
This topic describes how to create indexes on vector fields in Milvus to efficiently filter vector metadata. Vector indexes use advanced index structures and algorithms, such as IVF and HNSW, to effectively compress the vector space and accelerate the identification and retrieval of data points most similar to a query vector from large datasets. This significantly improves recall and response speed in scenarios such as image recognition, speech retrieval, and recommendation systems.
Background information
Milvus supports multiple index types for efficient similarity search and provides three metrics to calculate distances between vectors: cosine similarity (COSINE), Euclidean distance (L2), and inner product (IP). We recommend creating indexes on vector and scalar fields that are frequently used in queries to optimize search performance.
Prerequisites
The PyMilvus library is installed on your local client and is updated to the latest version.
To install or update the PyMilvus library, run the following command.
pip install --upgrade pymilvusA Milvus instance has been created. For more information, see Create a Milvus instance.
Preparations
Before you manage indexes, create a collection. When you create a collection, if the request explicitly specifies that an index (index) is to be created and loaded immediately, you must also specify the dimension and metric type of the vector field (dimension and metric_type) or provide index-related parameters. The following code snippet does not include these parameters, so the collection that is created has no index and is not automatically loaded into memory.
Specify the dimension and metric_type properties of the vector field.
Specify the schema and index-related parameters.
from pymilvus import MilvusClient, DataType
client = MilvusClient(
uri="http://c-xxxx.milvus.aliyuncs.com:19530", # The public endpoint of the Milvus instance.
token="<yourUsername>:<yourPassword>", # The username and password used to log on to the Milvus instance.
db_name="default" # The name of the database to connect to. This example uses the default database, default.
)
schema = MilvusClient.create_schema(
auto_id=False,
enable_dynamic_field=True,
)
schema.add_field(field_name="id", datatype=DataType.INT64, is_primary=True)
schema.add_field(field_name="vec", datatype=DataType.FLOAT_VECTOR, dim=5)
client.create_collection(
collection_name="<yourCollectioNname>",
schema=schema,
)
Create an index
You can call create_index and specify the required index parameters to create an index.
index_params = MilvusClient.prepare_index_params()
# Define the index parameters.
index_params.add_index(
field_name="vec", # Specify the vector field name, such as "vec".
metric_type="L2", # Set the metric type, such as L2.
index_type="IVF_PQ", # Set the index type, such as IVF_PQ.
index_name="vector_index" # Set the index name as needed.
)
# Create the index.
client.create_index(
collection_name="<yourCollectionname>",
index_params=index_params
)
View an index
res = client.describe_index(
collection_name="<yourCollectionname>",
index_name="<yourIndexname>"
)
print(res)
Drop an index
Before you drop a vector index, release the loaded collection. Searches cannot be performed on the collection while it is released.
client.drop_index(
collection_name="<yourCollectionname>",
index_name="<yourIndexname>"
)