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 | ||
---|---|---|---|---|
|
|
The name of the index. Index names must be unique within a keyspace. |
||
|
|
The name of the table column on which to create the index.
The column must be of type To create indexes on non- |
||
|
|
Specifies index options:
If passed, it must be an instance of |
||
|
|
If If
|
||
|
|
A timeout, in milliseconds, to impose on the underlying API request.
If not provided, the |
Name | Type | Summary |
---|---|---|
|
|
The name of the index. Index names must be unique within a keyspace. |
|
|
The name of the table column on which to create the index.
The column must be of type To create indexes on non- |
|
|
The options for this operation. |
Options (TableCreateVectorIndexOptions
):
Name | Type | Summary | ||
---|---|---|---|---|
|
|
If If
|
||
|
|
The similarity metric to use for vector search, one of |
||
|
|
Enable certain vector optimizations on the index by specifying the source model for your vectors, such as |
||
|
|
The client-side timeout for this operation. |
Name | Type | Summary | ||
---|---|---|---|---|
|
|
The name of the index. Index names must be unique within a keyspace. |
||
|
Definition of the index to create. Requires the name of the column to index.
The column must be of type Optionally, you can specify the similarity metric and source model:
|
|||
|
A specialization of index creation options, including If If
|
Name | Type | Summary |
---|---|---|
|
|
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. |
|
|
The name of the index. Index names must be unique within a keyspace. |
|
|
Contains |
|
|
The name of the table column on which to create the index.
The column must be of type To create indexes on non- |
|
|
Contains either, both, or none of the vector index options:
|
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.