# createIndexes

Creates indexes on MongoDB® collections. To create vector indexes, use the `kaiIndexOptions` parameter. Refer to [Vector Indexing](https://docs.singlestore.com/cloud/reference/sql-reference/vector-functions/vector-indexing.md) for information on vector indexes supported in SingleStore. This command is an extension of the MongoDB® [createIndexes](https://www.mongodb.com/docs/manual/reference/command/createIndexes/) command.&#x20;

To create multikey indexes, refer to [Multikey Indexes](https://docs.singlestore.com/#section-idm234884453468667.md).

## Syntax

```MongoDB
{
  "createIndexes": "<collection_name>",
  "indexes": [
    { "name": "<index_name>",
      "key": { "<path_to_property>": "vector" },
      "kaiIndexOptions": {
        "index_type": "string"
        "dimensions": <integer_value>,
        // other fields relevant to each index type }
    } ] }
```

## Arguments

| Field             | Type        | Requirement | Description                                                                                         |
| ----------------- | ----------- | ----------- | --------------------------------------------------------------------------------------------------- |
| `createIndexes`   | String      | Required    | The collection for which the index is created.                                                      |
| `indexes`         | JSON array  | Required    | An array that specifies the index to create. Each document in the array specifies a separate index. |
| `name`            | String      | Required    | A name for the vector index.                                                                        |
| `kaiIndexOptions` | JSON object | Required    | Specifies index options for the vector index.                                                       |

## Remarks

* The `createIndexes` command creates an index on the specified field in a collection using a [persisted computed column](https://docs.singlestore.com/cloud/create-a-database/using-persistent-computed-columns.md) within the same collection.
* SingleStore recommends creating collections (or tables) before creating a vector index. You must specify the field that holds the vector embeddings while creating the collection. Here's a sample syntax:
  ```mongodb
  db.createCollection(
    "<collection_name>",
    columns= [{'id': '<vector_field_name>', 'type': "VECTOR(<vector_dimension>, <type>) NOT NULL"}]
  )
  ```
  Refer to [createCollection](https://docs.singlestore.com/cloud/reference/singlestore-kai/singlestore-extension-commands/createcollection.md) for more information.
* If the specified collection does not exist, it is created along with the required structures. For example:
  ```mongodb
  db.runCommand({
    createIndexes: "sampleCollection", 
    indexes: [
       {  key: { "sampleCollection.vectorField": "vector" }, 
       name: "searchVector_IVF_EUC", 
       kaiIndexOptions: 
         { "index_type":"IVF_FLAT", 
           "nlist":5, 
           "nprobe":1, 
           "metric_type": "EUCLIDEAN_DISTANCE", 
           "dimensions": 2 } }
    ]
  } )
  ```
  ```sql
  DESC sampleCollection;

  ```
  ```output

  +-------------------------------+----------------+------+-----+---------+----------+
  | Field                         | Type           | Null | Key | Default | Extra    |
  +-------------------------------+----------------+------+-----+---------+----------+
  | _id                           | bson           | NO   |     | NULL    |          |
  | _more                         | bson           | NO   |     | NULL    |          |
  | $_id                          | longblob       | YES  | UNI | NULL    | computed |
  | $sampleCollection.vectorField | vector(2, F32) | NO   | MUL | NULL    | computed |
  +-------------------------------+----------------+------+-----+---------+----------+
  ```
* Use the `index_type` and `dimensions` fields in the `kaiIndexOptions` parameter to specify the type of vector index and the dimension of the vector, respectively.
* You can specify supported [Index Options](https://docs.singlestore.com/cloud/reference/sql-reference/vector-functions/vector-indexing/#section-idm457710817120003408966071884.md) in the `kaiIndexOptions` parameter for each index type. The specified values are set as defaults for the respective index search parameters.

## Multikey Indexes

SingleStore implements multikey indexes as Multi-Value Indexes. SingleStore recommends using multi-value index when the property at the path to search in a BSON document is an array. For more information on multi-value indexes, refer to [Multi-Value Hash Index (BSON)](https://docs.singlestore.com/cloud/reference/sql-reference/bson-functions/multi-value-hash-index-bson.md).

To create a multi-value index, use the `createIndex` command as:

```mongodb
db.<collection>.createIndex({"<path>":"multi_value"})
```

where `path` is the path to a property in the BSON document to index. For example,

```mongodb
db.users.createIndex({"addresses.city":"multi_value"})
```

## Remarks

* Multi-value index is supported with `$eq` and `$in` within `find` or `$match`, and optionally within a tree of `$and`, `$or`, or `$elemMatch` operation.
* A multi-value index can neither be a unique index nor have a compound key.

## Example

The following example shows how to create a vector index using the `createIndexes` command.

```MongoDB
db.runCommand({
  createIndexes: "exampleCollection", 
  indexes: [
     {  key: { "exampleCollection.vectorField": "vector" }, 
     name: "searchVector_IVF_EUC", 
     kaiIndexOptions: 
       { "index_type":"IVF_FLAT", 
         "nlist":5, 
         "nprobe":1, 
         "metric_type": "EUCLIDEAN_DISTANCE", 
         "dimensions": 2 } }
  ]
} )
```

Use the `db.<collection_name>.getIndexes()` command to view existing indexes on a collection. For example:

```MongoDB
db.exampleCollection.getIndexes()

```

```output

[ { v: 2, key: { _id: 1 }, name: '_id_' },
  { v: 2,
    key: { 'exampleCollection.vectorField': 'vector' },
    name: 'searchVector_IVF_EUC',
    kaiSearchSpec: 
     { nlist: 5,
       nprobe: 1,
       index_type: 'IVF_FLAT',
       metric_type: 'EUCLIDEAN_DISTANCE',
       dimensions: 2 } } ]
```

## References

* [Tuning Vector Indexes and Queries](https://docs.singlestore.com/cloud/developer-resources/functional-extensions/tuning-vector-indexes-and-queries.md)
* [Vector Indexing](https://docs.singlestore.com/cloud/reference/sql-reference/vector-functions/vector-indexing.md)
* [Vector Type](https://docs.singlestore.com/cloud/reference/sql-reference/data-types/vector-type.md)
* [Working with Vector Data](https://docs.singlestore.com/cloud/developer-resources/functional-extensions/working-with-vector-data.md)

***

Modified at: June 5, 2025

Source: [/cloud/reference/singlestore-kai/singlestore-extension-commands/createindexes/](https://docs.singlestore.com/cloud/reference/singlestore-kai/singlestore-extension-commands/createindexes/)

(An index of the documentation is available at /llms.txt)
