Create and Drop an Index
This article provides Python sample codes for creating or dropping indexes.
Create an index
Currently, a collection only supports one index type. When you change the index type of a collection, Milvus automatically deletes the old index file. Before creating other indexes, a collection uses FLAT as the default index type.
create_index()
specifies the index type of a collection and synchronously creates indexes for the previously inserted data. When the size of the subsequently inserted data reaches the index_file_size
, 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 details about using index.
-
Prepare the parameters needed to create indexes (take IVF_FLAT as an example). The index parameters are stored in a JSON string, which is represented by a dictionary in the Python SDK.
# Prepare index param. >>> ivf_param = {'nlist': 16384}
Different index types requires different indexing parameters. They must all have a value. -
Create index for the collection:
# Create an index. >>> milvus.create_index('test01', IndexType.IVF_FLAT, ivf_param)
Ensure you enable GPU when indexing and searching with
IVF_SQ8H
.
Drop an Index
After deleting the index, the collection uses the default index type FLAT again.
>>> milvus.drop_index('test01')