Skip to content

BUCKET.INDEX

Manages indexes on bucket fields. Indexes speed up queries by allowing efficient lookups on specific fields.

SubcommandDescription
CREATECreate a new index on one or more fields.
LISTList all indexes on a bucket.
DESCRIBEGet detailed information about an index.
DROPDrop an existing index.
TASKSList background maintenance tasks for an index.
ANALYZETrigger index statistics analysis.

Creates one or more indexes on bucket fields.

BUCKET.INDEX CREATE <bucket> <schema>
ParameterTypeRequiredDescription
bucketstringYesName of the target bucket. The bucket must already exist.
schemaJSONYesIndex schema defining the fields to index.

The schema is a JSON object that can contain single-field indexes, compound indexes, or both:

{
"<field>": { "bson_type": "<type>" [, "multi_key": true] [, "unique": true] [, "name": "<name>"] },
"$compound": [ { "fields": [ { "selector": "<field>", "bson_type": "<type>" }, ... ] [, "unique": true] [, "name": "<name>"] } ]
}

Field selectors use dot notation to address nested fields inside documents. Arrays are traversed automatically. When a selector crosses an array, each element is evaluated independently.

SelectorTargets
nameTop-level field name.
address.citycity inside the nested object address.
tagsThe tags field itself (use with multi_key for arrays).
orders.totaltotal inside each element of the orders array.

Example document:

{
"username": "alice",
"address": { "city": "Istanbul", "zip": "34000" },
"tags": ["admin", "editor"],
"orders": [
{ "total": 120, "status": "shipped" },
{ "total": 45, "status": "pending" }
]
}
  • Selector address.city reaches "Istanbul".
  • Selector tags with multi_key: true indexes each element ("admin", "editor") separately.
  • Selector orders.total with multi_key: true indexes each order’s total (120, 45) separately.

Each top-level key (other than $compound) is a field selector. The value defines the index properties:

{
"field_name": {
"bson_type": "type",
"multi_key": true|false,
"name": "optional_custom_name"
}
}
PropertyTypeRequiredDescription
bson_typestringYesThe BSON type of the field values. See Supported BSON Types.
multi_keybooleanNoWhen true, creates a multi-key index for array fields. Each array element generates a separate index entry. Default: false.
uniquebooleanNoWhen true, rejects any document whose value on this field already exists. Cannot be combined with multi_key. Default: false.
namestringNoCustom name for the index. If omitted, a name is auto-generated from the selector and type.
collationobjectNoCollation spec for locale-aware string ordering. Only valid for string type. See Collation.

The $compound key holds an array of compound index definitions. Each definition specifies an ordered list of fields:

{
"$compound": [
{
"name": "optional_custom_name",
"fields": [
{ "selector": "field_a", "bson_type": "string" },
{ "selector": "field_b", "bson_type": "int32", "multi_key": false }
]
}
]
}

Each field in the fields array supports:

PropertyTypeRequiredDescription
selectorstringYesDot-notation path to the field.
bson_typestringYesThe BSON type of the field values. See Supported BSON Types.
multi_keybooleanNoWhen true, creates a multi-key index entry per array element. Default: false.

The compound index definition also supports:

PropertyTypeRequiredDescription
namestringNoCustom name for the compound index. If omitted, a name is auto-generated.
uniquebooleanNoWhen true, the combination of all field values must be unique across documents. Cannot be combined with a multi-key field. Default: false.
collationobjectNoCollation spec for locale-aware string ordering. Requires at least one string field. See Collation.

Constraints:

  • A compound index must have at least two fields.
  • A compound index supports at most 32 fields.
  • At most one field can have multi_key enabled.
  • A unique compound index cannot contain a multi-key field.
  • Each field selector must appear exactly once within a compound index.

See Compound Indexes for detailed rules including the prefix rule and supported operators.

When multi_key is set to true, the index is designed for array fields. Each element in the array creates a separate index entry, allowing queries to match documents where any array element satisfies the condition.

Limitations:

  • Undefined ordering: Result ordering is undefined with multi-key indexes. Since each document can have multiple index entries (one per array element), the order in which documents are returned cannot be guaranteed.
  • Index size: Multi-key indexes can be significantly larger than regular indexes because each array element creates a separate index entry.
  • Type matching: Only array elements matching the specified bson_type are indexed.

Returns OK on success.

Bucket errors:

Error CodeError messageCause
NOSUCHBUCKETNo such bucket: '<bucket>'-
BUCKETBEINGREMOVEDBucket '<bucket>' is being removed-

Index errors:

Error CodeError messageCause
ERRAn index on field '<field>' already exists-
ERRInvalid index schema-
ERRUnknown BSON type: <type>-
ERRA unique index cannot be multi-key-

Create a single-field index:

> BUCKET.INDEX CREATE users '{"username": {"bson_type": "string"}}'
OK

Create multiple indexes at once:

> BUCKET.INDEX CREATE users '{"age": {"bson_type": "int32"}, "email": {"bson_type": "string"}}'
OK

Create an index with a custom name:

> BUCKET.INDEX CREATE users '{"username": {"bson_type": "string", "name": "idx_username"}}'
OK

Create a multi-key index for array fields:

> BUCKET.INDEX CREATE products '{"tags": {"bson_type": "string", "multi_key": true}}'
OK

Create a unique index:

> BUCKET.INDEX CREATE users '{"email": {"bson_type": "string", "unique": true}}'
OK

Index a nested field using dot notation:

> BUCKET.INDEX CREATE users '{"address.city": {"bson_type": "string"}}'
OK

Index elements inside an array of objects:

> BUCKET.INDEX CREATE users '{"orders.total": {"bson_type": "int32", "multi_key": true}}'
OK

Create a single-field index with collation:

> BUCKET.INDEX CREATE users '{
"username": {
"bson_type": "string",
"collation": {"locale": "tr", "strength": 2}
}
}'
OK

Create a compound index:

> BUCKET.INDEX CREATE products '{
"$compound": [{
"name": "idx_cat_price",
"fields": [
{"selector": "category", "bson_type": "string"},
{"selector": "price", "bson_type": "double"}
]
}]
}'
OK

Create a unique compound index:

> BUCKET.INDEX CREATE users '{
"$compound": [{
"unique": true,
"fields": [
{"selector": "name", "bson_type": "string"},
{"selector": "age", "bson_type": "int32"}
]
}]
}'
OK

Create a compound index with collation:

> BUCKET.INDEX CREATE products '{
"$compound": [{
"fields": [
{"selector": "category", "bson_type": "string"},
{"selector": "price", "bson_type": "double"}
],
"collation": {"locale": "en"}
}]
}'
OK

Create single-field and compound indexes together:

> BUCKET.INDEX CREATE products '{
"email": {"bson_type": "string"},
"$compound": [{
"fields": [
{"selector": "status", "bson_type": "string"},
{"selector": "region", "bson_type": "string"}
]
}]
}'
OK

Lists all indexes defined on a bucket.

BUCKET.INDEX LIST <bucket>
ParameterTypeRequiredDescription
bucketstringYesName of the bucket.

Returns an array of index names.

Bucket errors:

Error CodeError messageCause
NOSUCHBUCKETNo such bucket: '<bucket>'-
BUCKETBEINGREMOVEDBucket '<bucket>' is being removed-
> BUCKET.INDEX LIST users
1) "primary-index"
2) "selector:username.bsonType:STRING"
3) "selector:age.bsonType:INT32"

Gets detailed information about a specific index.

BUCKET.INDEX DESCRIBE <bucket> <index>
ParameterTypeRequiredDescription
bucketstringYesName of the bucket.
indexstringYesName of the index to describe.

Returns a map. The fields depend on the index type.

Single-field index (index_type is single_field):

FieldTypeDescription
index_typestringsingle_field.
idintegerIndex identifier.
selectorstringThe field selector the index is built on.
bson_typestringThe BSON type of indexed values.
statusstringCurrent index status. See Index Lifecycle.
uniquebooleanWhether the index enforces value uniqueness.
collationmapCollation configuration (see below). Empty when no collation is set.
statisticsmapIndex statistics including cardinality.

Compound index (index_type is compound):

FieldTypeDescription
index_typestringcompound.
idintegerIndex identifier.
fieldsarrayThe indexed fields in index order. Each entry is a map with selector and bson_type.
statusstringCurrent index status. See Index Lifecycle.
uniquebooleanWhether the index enforces value uniqueness.
collationmapCollation configuration (see below). Empty when no collation is set.
statisticsmapIndex statistics including cardinality.

Vector index (index_type is vector):

FieldTypeDescription
index_typestringvector.
idintegerIndex identifier.
selectorstringThe field selector the index is built on.
dimensionsintegerThe vector length the index accepts.
distancestringThe distance function of the index.
statusstringCurrent index status. See Index Lifecycle.
statisticsmapIndex statistics including cardinality.
FieldTypeDescription
localestringICU locale identifier (e.g., "en", "tr").
strengthintegerComparison strength level (1-5).
case_levelbooleanWhether to include case-level comparisons.
case_firststringSort order of case differences ("upper", "lower", "off").
numeric_orderingbooleanWhether to compare numeric strings as numbers.
alternatestringHandling of variable-weight characters ("non-ignorable", "shifted").
backwardsbooleanWhether to reverse secondary-level comparisons (for French).
normalizationbooleanWhether to perform Unicode normalization.
max_variablestringWhich characters are ignorable when alternate="shifted" ("punct", "space").

Bucket errors:

Error CodeError messageCause
NOSUCHBUCKETNo such bucket: '<bucket>'-
BUCKETBEINGREMOVEDBucket '<bucket>' is being removed-

Index errors:

Error CodeError messageCause
NOSUCHINDEXNo such index: '<index>'-
> BUCKET.INDEX DESCRIBE users "selector:username.bsonType:STRING"
index_type -> "single_field"
id -> 2
selector -> "username"
bson_type -> "STRING"
status -> "WAITING"
unique -> (false)
collation -> {}
statistics -> {cardinality -> 0}

RESP2 clients

RESP2 has no map and no boolean type. On a RESP2 connection the reply is a flat array where each field name is followed by its value, unique and the collation flags are sent as 1 or 0, and the nested collation, statistics and compound fields entries become flat arrays too:

> BUCKET.INDEX DESCRIBE users "selector:username.bsonType:STRING"
1) "index_type"
2) "single_field"
3) "id"
4) (integer) 2
5) "selector"
6) "username"
7) "bson_type"
8) "STRING"
9) "status"
10) "WAITING"
11) "unique"
12) (integer) 0
13) "collation"
14) (empty array)
15) "statistics"
16) 1) "cardinality"
2) (integer) 0

Drops an existing index from a bucket.

BUCKET.INDEX DROP <bucket> <index>
ParameterTypeRequiredDescription
bucketstringYesName of the bucket.
indexstringYesName of the index to drop.

Returns OK on success. The index is marked as DROPPED and a background task is created to clean up the index data.

Bucket errors:

Error CodeError messageCause
NOSUCHBUCKETNo such bucket: '<bucket>'-
BUCKETBEINGREMOVEDBucket '<bucket>' is being removed-

Index errors:

Error CodeError messageCause
NOSUCHINDEXNo such index: '<index>'-
ERRCannot drop the primary index-
ERRIndex is already in the 'DROPPED' status-
ERRIndex has active tasks-
> BUCKET.INDEX DROP users "selector:username.bsonType:STRING"
OK

Attempting to drop the primary index:

> BUCKET.INDEX DROP users "primary-index"
(error) ERR Cannot drop the primary index

Lists background maintenance tasks associated with an index.

BUCKET.INDEX TASKS <bucket> <index>
ParameterTypeRequiredDescription
bucketstringYesName of the bucket.
indexstringYesName of the index.

Returns a map where each key is a task ID and the value contains task details:

For BUILD tasks:

FieldTypeDescription
kindstringTask type (BUILD).
cursorstringCurrent position in the build process.
lowerstringLower bound position.
upperstringUpper bound position.
statusstringTask status.
errorstringError message if failed.

For DROP tasks:

FieldTypeDescription
kindstringTask type (DROP).
statusstringTask status.
errorstringError message if failed.

For BOUNDARY tasks:

FieldTypeDescription
kindstringTask type (BOUNDARY).
statusstringTask status.
errorstringError message if failed.

For ANALYZE tasks:

FieldTypeDescription
kindstringTask type (ANALYZE).
statusstringTask status.
errorstringError message if failed.

Bucket errors:

Error CodeError messageCause
NOSUCHBUCKETNo such bucket: '<bucket>'-
BUCKETBEINGREMOVEDBucket '<bucket>' is being removed-
> BUCKET.INDEX TASKS users "selector:username.bsonType:STRING"
"5K8G4R000000000000000000" -> {
kind -> "BUILD"
cursor -> "5K8G4R000000000000000500"
lower -> "0000000000000000000000000"
upper -> "5K8G4R000000000000001000"
status -> "RUNNING"
error -> ""
}

RESP2 clients

RESP2 has no map type. On a RESP2 connection the reply is a flat array where each task ID is followed by its field list:

> BUCKET.INDEX TASKS users "selector:username.bsonType:STRING"
1) "5K8G4R000000000000000000"
2) 1) "kind"
2) "BUILD"
3) "cursor"
4) "5K8G4R000000000000000500"
5) "lower"
6) "0000000000000000000000000"
7) "upper"
8) "5K8G4R000000000000001000"
9) "status"
10) "RUNNING"
11) "error"
12) ""

Trigger index statistics analysis. Statistics help the query optimizer make better decisions.

BUCKET.INDEX ANALYZE <bucket> <index>
ParameterTypeRequiredDescription
bucketstringYesName of the bucket.
indexstringYesName of the index to analyze.

Returns OK on success. A background task is created to compute index statistics.

Bucket errors:

Error CodeError messageCause
NOSUCHBUCKETNo such bucket: '<bucket>'-
BUCKETBEINGREMOVEDBucket '<bucket>' is being removed-

Index errors:

Error CodeError messageCause
NOSUCHINDEXNo such index: '<index>'-
ERRAn analyze task has already exist-
ERRCannot analyze index: index is not in READY state (current=<status>)-
> BUCKET.INDEX ANALYZE users "selector:username.bsonType:STRING"
OK

Argument errors:

Error CodeError messageCause
ERRunknown subcommand: '<subcommand>'-

Indexes go through the following states:

StatusDescription
WAITINGIndex is created but background building has not started yet.
BUILDINGIndex is being built by a background task.
READYIndex is fully built and available for queries.
DROPPEDIndex is marked for deletion; background cleanup is in progress.
FAILEDIndex building failed due to an error.

The following BSON types can be indexed:

TypeDescription
stringUTF-8 string values.
int3232-bit signed integers.
int6464-bit signed integers.
double64-bit IEEE 754 floating point.
booleanBoolean values (true / false).
datetimeUTC datetime (milliseconds since Unix epoch).
timestampInternal timestamp type.
binaryBinary data.
objectidObjectId values.
decimal128128-bit decimal floating point. Not yet fully supported for indexing.