Create a vector index

This Astra DB Serverless feature is currently in public preview. Development is ongoing, and the features and functionality are subject to change. Astra DB Serverless, and the use of such, is subject to the DataStax Preview Terms.

The Data API tables commands are available through HTTP and the clients.

If you use a client, tables commands are available only in client versions 2.0-preview or later. For more information, see Data API client upgrade guide.

Creates a new index for a vector column in a table in a Serverless (Vector) database. You must create a vector index if you want to perform a vector search on vector embeddings stored in a column.

To create an index on a non-vector column, see Create an index instead.

To manage indexes, your application token must have the same level of permissions that you need to manage tables.

Method signature

  • Python

  • TypeScript

  • Java

  • curl

The following method belongs to the astrapy.table.Table class.

create_vector_index(
  name: str,
  *,
  column: str,
  options: TableVectorIndexOptions | dict[str, Any],
  if_not_exists: bool,
  table_admin_timeout_ms: int,
  request_timeout_ms: int,
  timeout_ms: int,
) -> None

The following method belongs to the Table class.

async createVectorIndex(
  name: string,
  column: WSchema | string,
  options?: {
    ifNotExists?: boolean,
    options?: {
      metric?: string,
      sourceModel?: string,
      timeout?: number | TimeoutDescriptor,
    },
  }
): void

The following methods belong to the com.datastax.astra.client.tables.Table class.

void createVectorIndex(
  String indexName,
  String columnName
)
void createVectorIndex(
  String indexName,
  TableVectorIndexDefinition indexDefinition
)
void createVectorIndex(
  String indexName,
  TableVectorIndexDefinition indexDefinition,
  CreateVectorIndexOptions indexOptions
)
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_TABLE" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
  "createVectorIndex": {
    "name": "INDEX_NAME",
    "definition": {
      "column": "VECTOR_COLUMN_NAME",
      "options": {
        "metric": STRING,
        "sourceModel": STRING
      }
    }
  }
}'

Result

  • Python

  • TypeScript

  • Java

  • curl

Creates an index for the specified vector column.

Does not return anything.

Creates an index for the specified vector column.

Returns a promise that resolves once the operation completes.

Creates an index for the specified vector column.

Does not return anything.

Creates an index for the specified vector column.

If the command succeeds, the response indicates the success.

Example response:

{
  "status": {
    "ok": 1
  }
}

Parameters

  • Python

  • TypeScript

  • Java

  • curl

Name Type Summary

name

str

The name of the index.

Index names must be unique within a keyspace.

column

str

The name of the table column on which to create the index. The column must be of type vector.

To create indexes on non-vector columns, see Create an index.

options

TableVectorIndexOptions | dict | None

Specifies index options:

  • metric: One of the values in astrapy.constants.VectorMetric selects a similarity metric, such as COSINE (default), DOT_PRODUCT, or EUCLIDEAN.

  • source_model: Enable certain vector optimizations on the index by specifying the source model for your vectors, such as 'openai_v3_large', 'openai_v3_small', 'ada002', 'gecko', 'bert', or 'other' (default). If a model is specified, the index automatically uses the parameters best suited to that embedding model.

If passed, it must be an instance of TableVectorIndexOptions or an equivalent dictionary.

if_not_exists

bool | None

If True, and an index with the given name already exists in the keyspace, then the command succeeds and silently does nothing. In this case, no actual index creation takes place on the database.

If False (default), an error occurs if an index with the specified name already exists.

ifNotExists: True, does not check the type or content of any existing indexes. This parameter checks index names only.

This means that the command succeeds if the given index name is already in use, even if the type or indexed column is different.

table_admin_timeout_ms

int | None

A timeout, in milliseconds, to impose on the underlying API request. If not provided, the Table defaults apply. This parameter is aliased as request_timeout_ms and timeout_ms for convenience.

Name Type Summary

name

string

The name of the index.

Index names must be unique within a keyspace.

column

string

The name of the table column on which to create the index. The column must be of type vector.

To create indexes on non-vector columns, see Create an index.

options?

TableCreateVectorIndexOptions

The options for this operation.

Options (TableCreateVectorIndexOptions):

Name Type Summary

ifNotExists?

boolean

If true, and an index with the given name already exists in the keyspace, then the command succeeds and silently does nothing. In this case, no actual index creation takes place on the database.

If false (default), an error occurs if an index with the specified name already exists.

ifNotExists: true, does not check the type or content of any existing indexes. This parameter checks index names only.

This means that the command succeeds if the given index name is already in use, even if the type or indexed column is different.

options.metric?

string

The similarity metric to use for vector search, one of 'cosine' (default), 'dot_product', or 'euclidean'.

options.sourceModel?

string

Enable certain vector optimizations on the index by specifying the source model for your vectors, such as 'openai_v3_large', 'openai_v3_small', 'ada002', 'gecko', 'bert', or 'other' (default).

timeout?

number | TimeoutDescriptor

The client-side timeout for this operation.

Name Type Summary

name

str

The name of the index.

Index names must be unique within a keyspace.

definition

TableVectorIndexDefinition

Definition of the index to create.

Requires the name of the column to index. The column must be of type vector. To create indexes on non-vector columns, see Create an index.

Optionally, you can specify the similarity metric and source model:

  • metric(SimilarityMetric.METRIC): The similarity metric to use for vector search, one of COSINE (default), DOT_PRODUCT, or EUCLIDEAN.

  • sourceModel(): Enable certain vector optimizations on the index by specifying the source model for your vectors, such as "openai_v3_large", "openai_v3_small", "ada002", "gecko", "bert", or "other" (default).

options

CreateVectorIndexOptions

A specialization of index creation options, including ifNotExists and timeout.

If ifNotExists(true), and an index with the given name already exists in the keyspace, then the command succeeds and silently does nothing. In this case, no actual index creation takes place on the database.

If ifNotExists(false) (default), an error occurs if an index with the specified name already exists.

ifNotExists(true), does not check the type or content of any existing indexes. This parameter checks index names only.

This means that the command succeeds if the given index name is already in use, even if the type or indexed column is different.

Name Type Summary

createVectorIndex

command

The Data API command to create a vector index for a table in a Serverless (Vector) database. It acts as a container for all the attributes and settings required to create the index.

name

string

The name of the index.

Index names must be unique within a keyspace.

definition

object

Contains column and options for the vector index.

definition.column

string

The name of the table column on which to create the index. The column must be of type vector.

To create indexes on non-vector columns, see Create an index.

definition.options

object

Contains either, both, or none of the vector index options:

  • metric: The similarity metric to use for vector searches on this index, one of cosine (default), dot_product, or euclidean.

  • sourceModel: Enable certain vector optimizations on the index by specifying the source model for your vectors, such as openai_v3_large, openai_v3_small, ada002, gecko, bert, or other (default).

Examples

The following examples demonstrate how to create a vector index.

Create a vector index with the default source model and similarity metric

If you do not specify the source model and similarity metric, the default values are used. For more information, see Parameters.

  • Python

  • TypeScript

  • Java

  • curl

from astrapy import DataAPIClient

# Get an existing table
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
table = database.get_table("TABLE_NAME")

# Index a vector column
table.create_vector_index(
    "example_index_name",
    column="example_vector_column"
)
import { DataAPIClient } from '@datastax/astra-db-ts';

// Get an existing table
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const table = database.table('TABLE_NAME');

// Index a vector column
(async function () {
  await table.createVectorIndex(
    "example_index_name",
    "example_vector_column",
  );
})();
package com.example;

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;

public class CreateIndex {

    public static void main(String[] args) {
        // Get an existing table
        Table<Row> table = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
            .getDatabase("ASTRA_DB_API_ENDPOINT")
            .getTable("TABLE_NAME");

        // Index a vector column
        table.createVectorIndex("example_index_name", "example_vector_column");
    }
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_TABLE" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
  "createVectorIndex": {
    "name": example_index_name",
    "definition": {
      "column": "example_vector_column"
    }
  }
}'

Create a vector index and specify the source model and similarity metric

You can specify the embedding source model and the similarity metric when you create a vector index.

  • Python

  • TypeScript

  • Java

  • curl

from astrapy import DataAPIClient
from astrapy.constants import VectorMetric
from astrapy.info import TableVectorIndexOptions

# Get an existing table
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
table = database.get_table("TABLE_NAME")

# Index a vector column
table.create_vector_index(
    "example_index_name",
    column="example_vector_column",
    options=TableVectorIndexOptions(
        metric=VectorMetric.DOT_PRODUCT,
        source_model="nv-qa-4",
    ),
)
import { DataAPIClient } from '@datastax/astra-db-ts';

// Get an existing table
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const table = database.table('TABLE_NAME');

// Index a vector column
(async function () {
  await table.createVectorIndex(
    "example_index_name",
    "example_vector_column",
    {
      options: {
        metric: 'dot_product',
        sourceModel: 'nv-qa-4',
      },
    }
  );
})();
package com.example;

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
import com.datastax.astra.client.tables.commands.options.CreateVectorIndexOptions;
import com.datastax.astra.client.tables.definition.indexes.TableVectorIndexDefinition;
import com.datastax.astra.client.core.vector.SimilarityMetric;

public class CreateIndex {

    public static void main(String[] args) {
        // Get an existing table
        Table<Row> table = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
            .getDatabase("ASTRA_DB_API_ENDPOINT")
            .getTable("TABLE_NAME");

        // Index a vector column
        TableVectorIndexDefinition definition = new TableVectorIndexDefinition()
            .column("example_vector_column")
            .metric(SimilarityMetric.COSINE)
            .sourceModel("openai-v3-large");
        CreateVectorIndexOptions options = new CreateVectorIndexOptions();
        table.createVectorIndex("example_index_name", definition, options);
    }
}
curl -sS -L -X POST "ASTRA_DB_API_ENDPOINT/api/json/v1/ASTRA_DB_KEYSPACE/ASTRA_DB_TABLE" \
--header "Token: ASTRA_DB_APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
  "createVectorIndex": {
    "name": example_index_name",
    "definition": {
      "column": "example_vector_column",
      "options": {
        "metric": "dot_product",
        "sourceModel": "ada002"
      }
    }
  }
}'

Create an index only if the index does not exist

Use this option to silently do nothing if an index with the specified name already exists.

This option only checks index names. It doesn’t check the type or content of any existing indexes.

  • Python

  • TypeScript

  • Java

  • curl

from astrapy import DataAPIClient

# Get an existing table
client = DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
database = client.get_database("ASTRA_DB_API_ENDPOINT")
table = database.get_table("TABLE_NAME")

# Index a vector column
table.create_vector_index(
    "example_index_name",
    column="example_vector_column",
    if_not_exists=True,
)
import { DataAPIClient } from '@datastax/astra-db-ts';

// Get an existing table
const client = new DataAPIClient('ASTRA_DB_APPLICATION_TOKEN');
const database = client.db('ASTRA_DB_API_ENDPOINT');
const table = database.table('TABLE_NAME');

// Index a vector column
(async function () {
  await table.createVectorIndex(
    "example_index_name",
    "example_vector_column",
    {
      ifNotExists: true,
    },
  );
})();
package com.example;

import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
import com.datastax.astra.client.tables.commands.options.CreateVectorIndexOptions;
import com.datastax.astra.client.tables.definition.indexes.TableVectorIndexDefinition;

public class CreateIndex {

    public static void main(String[] args) {
        // Get an existing table
        Table<Row> table = new DataAPIClient("ASTRA_DB_APPLICATION_TOKEN")
            .getDatabase("ASTRA_DB_API_ENDPOINT")
            .getTable("TABLE_NAME");

        // Index a vector column
        TableVectorIndexDefinition definition = new TableVectorIndexDefinition()
            .column("example_vector_column");
        CreateVectorIndexOptions options = new CreateVectorIndexOptions()
            .ifNotExists(true);
        table.createVectorIndex("example_index_name", definition, options);
    }
}

This option has no literal equivalent in HTTP. Instead, you can list the index names to see if an index with the name already exists.

Client reference

  • Python

  • TypeScript

  • Java

  • curl

For more information, see the client reference.

For more information, see the client reference.

For more information, see the client reference.

Client reference documentation is not applicable for HTTP.

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2025 DataStax | Privacy policy | Terms of use | Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com