# VSIM.CREATEINDEX

> Create a named vector index for nearest-neighbor search.

URL: https://docs.hotkv.com/commands/vsim-createindex/

```
VSIM.CREATEINDEX index dims [METRIC COSINE | L2 | IP] [M m] [EF_CONSTRUCTION n] [EF_SEARCH n]
```

- Group: Vector search
- Complexity: O(1)
- HotKV Cloud: all plans
- On-Premise: no license needed

`VSIM.CREATEINDEX` creates an empty vector index called `index` for vectors
of `dims` dimensions. You then store vectors under key names with
[VSIM.SET](https://docs.hotkv.com/commands/vsim-set/index.md) and find the closest ones to a query vector
with [VSIM.SEARCH](https://docs.hotkv.com/commands/vsim-search/index.md). Searches are approximate
(HNSW-indexed), which keeps them fast on large indexes.

Indexes and their vectors live beside the keyspace: [KEYS](https://docs.hotkv.com/commands/keys/index.md),
[EXISTS](https://docs.hotkv.com/commands/exists/index.md) and [DEL](https://docs.hotkv.com/commands/del/index.md) do not see them, and an
index name does not clash with a key of the same name. They are kept in
snapshots. In cluster mode, an index lives on the node that owns the hash
slot of its name.

## Options

| Option | Effect |
| --- | --- |
| `METRIC COSINE` | Compare vectors by angle (the default). Good for text embeddings. |
| `METRIC L2` | Compare vectors by squared Euclidean distance. |
| `METRIC IP` | Compare vectors by inner product (higher product means closer). |
| `M m` | Index connectivity, 2 to 256 (a general-purpose default applies). Higher values improve search accuracy and use more memory. |
| `EF_CONSTRUCTION n` | Build effort, at least 1 (a general-purpose default applies). Higher values build a more accurate index, more slowly. |
| `EF_SEARCH n` | Default search effort, at least 1 (a general-purpose default applies). Higher values make searches more accurate and slower. Can be overridden per search with `EF`. |

Metric names are case-insensitive. `dims` must be 1 to 65536.

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| Created | Simple string `OK` | Simple string `OK` |

Error replies:

- `ERR index 'name' already exists`
- `ERR invalid dimensions (must be 1-65536)`
- `ERR invalid metric (use cosine, l2, or ip)`
- `ERR invalid M parameter (must be 2-256)`
- `ERR invalid EF_CONSTRUCTION (must be >= 1)` and `ERR invalid EF_SEARCH (must be >= 1)`
- `ERR unknown option 'name'`

An option name given last without a value is ignored.

## Examples

```
127.0.0.1:6379> VSIM.CREATEINDEX docs 384
OK
127.0.0.1:6379> VSIM.CREATEINDEX points 3 METRIC L2 M 32 EF_SEARCH 100
OK
127.0.0.1:6379> VSIM.CREATEINDEX docs 384
(error) ERR index 'docs' already exists
127.0.0.1:6379> VSIM.INFO
1) "docs"
2) "points"
```

The order of the names VSIM.INFO lists can differ.

## Compatibility

HotKV-only command. It is not related to the vector set commands (`VADD`,
`VSIM`) of Redis 8.

## See also

[VSIM.SET](https://docs.hotkv.com/commands/vsim-set/index.md), [VSIM.SEARCH](https://docs.hotkv.com/commands/vsim-search/index.md), [VSIM.INFO](https://docs.hotkv.com/commands/vsim-info/index.md),
[VSIM.DROPINDEX](https://docs.hotkv.com/commands/vsim-dropindex/index.md)