Build an Index on Vectors
This guide describes how to build an index on vectors in Milvus.
Vector indexes are an organizational unit of metadata used to accelerate vector similarity search. You need to create an index before you can perform ANN searches against your Milvus.
See Vector Index for more information about the mechanism and varieties of vector indexes.
By default, Milvus does not index a segment with less than 1,024 rows. To change this parameter, configure rootCoord.minSegmentSizeToEnableIndex
in milvus.yaml
.
The following example builds a 1024-cluster IVF_FLAT index with Euclidean distance (L2) as the similarity metric. You can choose the index and metrics that suit your scenario. See Similarity Metrics for more information.
When interacting with Milvus using Python code, you have the flexibility to choose between PyMilvus and MilvusClient (new). For more information, refer to Python SDK.
Prepare index parameter
Prepare the index parameters as follows:
index_params = {
"metric_type":"L2",
"index_type":"IVF_FLAT",
"params":{"nlist":1024}
}
const index_params = {
metric_type: "L2",
index_type: "IVF_FLAT",
params: JSON.stringify({ nlist: 1024 }),
};
idx, err := entity.NewIndexIvfFlat( // NewIndex func
entity.L2, // metricType
1024, // ConstructParams
)
if err != nil {
log.Fatal("fail to create ivf flat index parameter:", err.Error())
}
final IndexType INDEX_TYPE = IndexType.IVF_FLAT; // IndexType
final String INDEX_PARAM = "{\"nlist\":1024}"; // ExtraParam
Parameter | Description | Options |
---|---|---|
metric_type |
Type of metrics used to measure the similarity of vectors. | For floating point vectors:
|
index_type |
Type of index used to accelerate the vector search. | For floating point vectors:
|
params |
Building parameter(s) specific to the index. | See In-memory Index and On-disk Index for more information. |
|
Parameter | Description | Option |
---|---|---|
metric_type |
Type of metrics used to measure the similarity of vectors. | For floating point vectors:
|
index_type |
Type of index used to accelerate the vector search. | For floating point vectors:
|
params |
Building parameter(s) specific to the index. | See In-memory Index and On-disk Index for more information. |
|
Parameter | Description | Options |
---|---|---|
NewIndex func |
Function to create entity. Index according to different index types. | For floating point vectors:
|
metricType |
Type of metrics used to measure the similarity of vectors. | For floating point vectors:
|
ConstructParams |
Building parameter(s) specific to the index. | See In-memory Index and On-disk Index for more information. |
|
Parameter | Description | Options |
---|---|---|
IndexType |
Type of index used to accelerate the vector search. | For floating point vectors:
|
ExtraParam |
Building parameter(s) specific to the index. | See In-memory Index and On-disk Index for more information. |
|
Build index
Build the index by specifying the vector field name and index parameters.
from pymilvus import Collection, utility
# Get an existing collection.
collection = Collection("book")
collection.create_index(
field_name="book_intro",
index_params=index_params
)
utility.index_building_progress("book")
# Output: {'total_rows': 0, 'indexed_rows': 0}
await milvusClient.createIndex({
collection_name: "book",
field_name: "book_intro",
extra_params: index_params,
});
err := milvusClient.CreateIndex(
context.Background(), // ctx
"book", // CollectionName
"book_intro", // fieldName
idx, // entity.Index
false, // async
)
if err != nil {
log.Fatal("fail to create index:", err.Error())
}
milvusClient.createIndex(
CreateIndexParam.newBuilder()
.withCollectionName("book")
.withFieldName("book_intro")
.withIndexType(INDEX_TYPE)
.withMetricType(MetricType.L2)
.withExtraParam(INDEX_PARAM)
.withSyncMode(Boolean.FALSE)
.build()
);
Parameter | Description |
---|---|
field_name |
Name of the vector field to build index on. |
index_params |
Parameters of the index to build. |
Parameter | Description |
---|---|
collection_name |
Name of the collection to build index in. |
field_name |
Name of the vector field to build index on. |
extra_params |
Parameters of the index to build. |
Parameter | Description |
---|---|
ctx |
Context to control API invocation process. |
CollectionName |
Name of the collection to build index on. |
fieldName |
Name of the vector field to build index on. |
entity.Index |
Parameters of the index to build. |
async |
Switch to control sync/async behavior. The deadline of context is not applied in sync building process. |
What’s next
- Learn more basic operations of Milvus: