Skip to main content

create_index()

This creates a named index for a target field, which can either be a vector field or a scalar field.

📘Notes

This operation is non-blocking. You can call utility.wait_for_index_building_complete() to block the current process.

Request Syntax​

create_index(
field_name: str,
index_params: dict | None,
timeout: float | None
)

PARAMETERS:

  • field_name (string) -

    The name of the field for which an index is to be created.

  • index_params (dict) -

    The parameters that apply to the index-building process.

    • index_type (string) -

      The algorithm used to build the index.

      You should always use AUTOINDEX as the index type. Read AUTOINDEX Explained to get more.

    • metric_type (string) -

      The similarity metric type used to build the index.

      Possible values are L2, IP, and COSINE. Read Similarity Metrics Explained to get more.

    • params (dict) -

      Index-building parameters corresponding to the selected index type.

      For details on applicable index-building parameters, refer to AUTOINDEX Explained.

  • timeout (float | None)

    The timeout duration for this operation. Setting this to None indicates that this operation timeouts when any response arrives or any error occurs.

RETURN TYPE:

Status

RETURNS:

A Status object indicating whether this operation succeeds.

EXCEPTIONS:

  • MilvusException

    This exception will be raised when any error occurs during this operation.

Examples​

from pymilvus import Collection, CollectionSchema, FieldSchema, DataType

schema = CollectionSchema([
FieldSchema("id", DataType.INT64, is_primary=True),
FieldSchema("vector", DataType.FLOAT_VECTOR, dim=5)
])

# Create a collection
collection = Collection(
name="test_collection",
schema=schema
)

# Create an index on a scalar field
collection.create_index(
field_name="id"
)

# Set the index parameters
index_params = {
"index_type": "AUTOINDEX",
"metric_type": "COSINE",
"params": {
"nprobe": 10
}
}

# Create an index on the vector field
collection.create_index(
field_name="vector",
index_params=index_params,
timeout=None
)

# Check the index
collection.has_index() # True

The following operations are related to create_index()