milvus-logo
LFAI
< Docs
  • Python

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.

      Possible values FLAT, IVF_FLAT, GPU_IVF_FLAT, IVF_SQ8, IVF_PQ, GPU_IVF_PQ, HNSW, SCANN, BIN_FLAT, BIN_IVF_FLAT, and DISKANN. For details on these index types, refer to In-memory Index and On-disk Index.

    • metric_type (string) -

      The similarity metric type used to build the index.

      Possible values for float vector embeddings are L2, IP, and COSINE, and those for binary vector embeddings are Jaccard and Hamming. Read Similarity Metrics to get more.

    • params (dict) -

      Index-building parameters corresponding to the selected index type.

      For details on applicable index-building parameters, refer to In-memory Index and On-disk Index.

  • 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": "IVF_FLAT",
    "metric_type": "COSINE",
    "params": {
        "nlist": 128
    }
}

# 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

Try Managed Milvus for Free

Zilliz Cloud is hassle-free, powered by Milvus and 10x faster.

Get Started
Feedback

Was this page helpful?