Create and Drop an Index

This article provides Python sample codes for creating or dropping indexes.

Create an index

Milvus supports one index for each field in a collection. Switching index type causes the original index to be deleted. A collection uses FLAT as its default index before an index type is specified.

create_index() specifies the index type of a collection and synchronously creates indexes for the previously inserted data. When the row count of the inserted data reaches segment_row_limit, Milvus automatically creates indexes in the background. For streaming data, it is recommended to create indexes before inserting the vector so that the system can automatically build indexes for the next data. For static data, it is recommended to import all the data at first and then create indexes. See index sample program for more information about using index.
  1. Prepare the parameters required for creating an IVF_FLAT vector index.

    Different index types requires different indexing parameters. They must all have a value.
    # Prepare index param.
    >>> ivf_param = {"index_type": "IVF_FLAT", "metric_type": "L2", "params": {"nlist": 4096}}
    
    Index index = Index
        .create(collectionName, "embedding")
        .setIndexType(IndexType.IVF_FLAT)
        .setMetricType(MetricType.L2)
        .setParamsInJson(new JsonBuilder().param("nlist", 100).build());
    
  2. Create index for the collection:

    # Create an index.
    >>> client.create_index('demo_films', "Vec", ivf_param)
    
    client.createIndex(index);
    

Drop an index

After deleting the index, the vector field uses the default index type FLAT again.

>>> client.drop_index('demo_films', "Vec")
client.dropIndex(collectionName, "embedding");

FAQ

How to set the value of nlist when I build indexes? It depends on your scenario. See Performance tuning > Index for more information.
Can Milvus create different types of index for different partitions in the same collection? No. A collection can have only one index type at a time.
Does Milvus create new indexes after vectors are inserted? Yes. When the inserted vectors grow to a specified volume, Milvus creates a new segment and starts to create an index file for it at the same time. The building of the new index file does not affect the existing index files.
Edit
© 2019 - 2020 Milvus. All rights reserved.