milvus-logo
LFAI
Home
  • User Guide

Build an Index on Scalars

This guide describes how to build an index on scalar fields.

Overview

Unlike vectors, which have both magnitude and direction, scalars have only magnitude. Milvus regards single numbers and strings as scalars. Here is a list of the available data types for scalar fields in Milvus.

To speed up attribute filtering in hybrid searches, you can build indexes on scalar fields since Milvus v2.1.0. You can read more about scalar field indexing here.

Build index

To build an index on scalar fields, you do not need to set any index parameters. The default value of a scalar field index name is default_idx followed by the name of the indexed field. You can set it to another value that seems fit.

The following code snippet assumes that a collection named book already exists and an index is to be created on the string field book_name.

from pymilvus import Collection

collection = Collection("book")   
collection.create_index(
  field_name="book_name", 
  index_name="scalar_index",
)
collection.load()
var collection = milvusClient.GetCollection("book");
await collection.CreateIndexAsync("book_name", indexName: "scalar_index");
await collection.LoadAsync();

Once the index has been created, you can include a boolean expression on this string field in a vector similarity search as follows:

search_param = {
  "data": [[0.1, 0.2]],
  "anns_field": "book_intro",
  "param": {"metric_type": "L2", "params": {"nprobe": 10}},
  "limit": 2,
  "expr": "book_name like \"Hello%\"", 
}
res = collection.search(**search_param)

What’s next

Feedback

Was this page helpful?