milvus-logo
LFAI
Home
  • User Guide

Index Scalar Fields

In Milvus, a scalar index is used to speed up metafiltering by a specific non-vector field value, similar to a traditional database index. This guide will walk you through creating and configuring scalar indexes for fields such as integers, strings, etc.

Types of scalar indexing

  • Auto indexing: Milvus automatically decides the index type based on the data type of the scalar field. This is suitable when you do not need to control the specific index type.

  • Custom indexing: You specify the exact index type, such as an inverted index. This provides more control over the index type selection.

Auto indexing

To use auto indexing, omit the index_type parameter in add_index(), so that Milvus can infer the index type based on the scalar field type.

To use auto indexing, omit the indexType parameter in IndexParam, so that Milvus can infer the index type based on the scalar field type.

To use auto indexing, omit the index_type parameter in createIndex(), so that Milvus can infer the index type based on the scalar field type.

For mappings between scalar data types and default indexing algorithms, refer to Scalar field indexing algorithms.

# Auto indexing
client = MilvusClient(
    uri="http://localhost:19530"
)

index_params = MilvusClient.prepare_index_params() # Prepare an empty IndexParams object, without having to specify any index parameters

index_params.add_index(
    field_name="scalar_1", # Name of the scalar field to be indexed
    index_type="", # Type of index to be created. For auto indexing, leave it empty or omit this parameter.
    index_name="default_index" # Name of the index to be created
)

client.create_index(
  collection_name="test_scalar_index", # Specify the collection name
  index_params=index_params
)
import io.milvus.v2.common.IndexParam;
import io.milvus.v2.service.index.request.CreateIndexReq;

IndexParam indexParamForScalarField = IndexParam.builder()
    .fieldName("scalar_1") // Name of the scalar field to be indexed
    .indexName("default_index") // Name of the index to be created
    .indexType("") // Type of index to be created. For auto indexing, leave it empty or omit this parameter.
    .build();

List<IndexParam> indexParams = new ArrayList<>();
indexParams.add(indexParamForVectorField);

CreateIndexReq createIndexReq = CreateIndexReq.builder()
    .collectionName("test_scalar_index") // Specify the collection name
    .indexParams(indexParams)
    .build();

client.createIndex(createIndexReq);
await client.createIndex({
    collection_name: "test_scalar_index", // Specify the collection name
    field_name: "scalar_1", // Name of the scalar field to be indexed
    index_name: "default_index", // Name of the index to be created
    index_type: "" // Type of index to be created. For auto indexing, leave it empty or omit this parameter.
})

Custom indexing

To use custom indexing, specify a particular index type using the index_type parameter in add_index().

To use custom indexing, specify a particular index type using the indexType parameter in IndexParam.

To use custom indexing, specify a particular index type using the index_type parameter in createIndex().

The example below creates an inverted index for the scalar field scalar_2.

index_params = MilvusClient.prepare_index_params() #  Prepare an IndexParams object

index_params.add_index(
    field_name="scalar_2", # Name of the scalar field to be indexed
    index_type="INVERTED", # Type of index to be created
    index_name="inverted_index" # Name of the index to be created
)

client.create_index(
  collection_name="test_scalar_index", # Specify the collection name
  index_params=index_params
)
import io.milvus.v2.common.IndexParam;
import io.milvus.v2.service.index.request.CreateIndexReq;

IndexParam indexParamForScalarField = IndexParam.builder()
    .fieldName("scalar_1") // Name of the scalar field to be indexed
    .indexName("inverted_index") // Name of the index to be created
    .indexType("INVERTED") // Type of index to be created
    .build();

List<IndexParam> indexParams = new ArrayList<>();
indexParams.add(indexParamForVectorField);

CreateIndexReq createIndexReq = CreateIndexReq.builder()
    .collectionName("test_scalar_index") // Specify the collection name
    .indexParams(indexParams)
    .build();

client.createIndex(createIndexReq);
await client.createIndex({
    collection_name: "test_scalar_index", // Specify the collection name
    field_name: "scalar_1", // Name of the scalar field to be indexed
    index_name: "inverted_index", // Name of the index to be created
    index_type: "INVERTED" // Type of index to be created
})

Methods and Parameters

  • prepare_index_params()

    Prepares an IndexParams object.

  • add_index()

    Adds index configurations to the IndexParams object.

    • field_name (string)

      The name of the scalar field to index.

    • index_type (string):

      The type of the scalar index to create. For implicit indexing, leave it empty or omit this parameter.

      For custom indexing, valid values are:

      • INVERTED: (Recommended) An inverted index consists of a term dictionary containing all tokenized words sorted alphabetically. For details, refer to Scalar Index.

      • STL_SORT: Sorts scalar fields using the standard template library sort algorithm. Only supports numeric fields (e.g., INT8, INT16, INT32, INT64, FLOAT, DOUBLE).

      • Trie: A tree data structure for fast prefix searches and retrievals. Supports VARCHAR fields.

    • index_name (string)

      The name of the scalar index to create. Each scalar field supports one index.

  • create_index()

    Creates the index in the specified collection.

    • collection_name (string)

      The name of the collection for which the index is created.

    • index_params

      The IndexParams object that contains index configurations.

Methods and Parameters

  • IndexParam Prepares an IndexParam object.
    • fieldName (String) The name of the scalar field to index.
    • indexName (String) The name of the scalar index to create. Each scalar field supports one index.
    • indexType (String) The type of the scalar index to create. For implicit indexing, leave it empty or omit this parameter. For custom indexing, valid values are:
      • INVERTED: (Recommended) An inverted index consists of a term dictionary containing all tokenized words sorted alphabetically. For details, refer to Scalar Index.
      • STL_SORT: Sorts scalar fields using the standard template library sort algorithm. Supports Boolean and numeric fields (e.g., INT8, INT16, INT32, INT64, FLOAT, DOUBLE).
      • Trie: A tree data structure for fast prefix searches and retrievals. Supports VARCHAR fields.
  • CreateIndexReq Creates the index in the specified collection.
    • collectionName (String) The name of the collection for which the index is created.
    • indexParams (List) A list of IndexParam objects that contain index configurations.

Methods and Parameters

  • createIndex

    Creates the index in the specified collection.

    • collection_name (string) The name of the collection for which the index is created.
    • field_name (string) The name of the scalar field to index.
    • index_name (string) The name of the scalar index to create. Each scalar field supports one index.
    • index_type (string) The type of the scalar index to create. For implicit indexing, leave it empty or omit this parameter. For custom indexing, valid values are:
      • INVERTED: (Recommended) An inverted index consists of a term dictionary containing all tokenized words sorted alphabetically. For details, refer to Scalar Index.
      • STL_SORT: Sorts scalar fields using the standard template library sort algorithm. Supports Boolean and numeric fields (e.g., INT8, INT16, INT32, INT64, FLOAT, DOUBLE).
      • Trie: A tree data structure for fast prefix searches and retrievals. Supports VARCHAR fields.

Verifying the result

Use the list_indexes() method to verify the creation of scalar indexes:

Use the listIndexes() method to verify the creation of scalar indexes:

Use the listIndexes() method to verify the creation of scalar indexes:

client.list_indexes(
    collection_name="test_scalar_index"  # Specify the collection name
)

# Output:
# ['default_index','inverted_index']
import java.util.List;
import io.milvus.v2.service.index.request.ListIndexesReq;

ListIndexesReq listIndexesReq = ListIndexesReq.builder()
    .collectionName("test_scalar_index")  // Specify the collection name
    .build();

List<String> indexNames = client.listIndexes(listIndexesReq);

System.out.println(indexNames);

// Output:
// [
//     "default_index",
//     "inverted_index"
// ]
res = await client.listIndexes({
    collection_name: 'test_scalar_index'
})

console.log(res.indexes)

// Output:
// [
//     "default_index",
//     "inverted_index"
// ]   

Limits

  • Currently, scalar indexing supports INT8, INT16, INT32, INT64, FLOAT, DOUBLE, BOOL, VARCHAR, and ARRAY data types, but not the JSON data type.

Try Managed Milvus for Free

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

Get Started
Feedback

Was this page helpful?