Create a vector index
Tables with the Data API are currently in public preview. Development is ongoing, and the features and functionality are subject to change. Hyper-Converged Database (HCD), and the use of such, is subject to the DataStax Preview Terms. |
Creates a new index for a vector column in a table in a 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.
The username and password used to generate the token must be tied to a role that has sufficient permissions to perform the desired operations.
Ready to write code? See the examples for this method to get started. If you are new to the Data API, check out the quickstart. |
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
Use the create_vector_index
method, which belongs to the astrapy.table.Table
class.
Method signature
create_vector_index(
name: str,
column: str,
*,
definition: TableVectorIndexDefinition | dict[str, Any],
options: TableVectorIndexOptions | dict[str, Any],
if_not_exists: bool,
table_admin_timeout_ms: int,
request_timeout_ms: int,
timeout_ms: int,
) -> None
Name | Type | Summary |
---|---|---|
|
|
The name of the index. Index names for tables must follow these rules:
|
|
|
The name of the vector column on which to create the index. The column name must use snake case, not camel case. To create indexes on non-vector columns, see Create an index. |
|
|
Optional.
Specifies the indexed column and index options.
If you use this parameter, the |
|
|
Optional. Specifies the index options:
See Create a vector index and specify the source model and similarity metric for an example. |
|
|
Optional. Whether the command should silently succeed even if an index with the given name already exists in the keyspace and no new index was created. This option only checks index names. It does not check index definitions. See Create an index only if the index does not exist for an example. Default: false |
|
|
Optional.
A timeout, in milliseconds, to impose on the underlying API request.
If not provided, the This parameter is aliased as |
Use the createVectorIndex
method, which belongs to the Table
class.
Method signature
async createVectorIndex(
name: string,
column: keyof Schema | Partial<Record<keyof Schema, string>>,
options?: {
ifNotExists?: boolean,
options?: {
metric?: string,
sourceModel?: string,
timeout?: number | TimeoutDescriptor,
},
}
): void
Name | Type | Summary |
---|---|---|
|
|
The name of the index. Index names for tables must follow these rules:
|
|
|
The name of the vector column on which to create the index. The column name must use snake case, not camel case. To create indexes on non-vector columns, see Create an index. |
|
|
Optional.
The options for this operation. See Properties of |
Name | Type | Summary |
---|---|---|
|
|
Optional. The similarity metric to use for vector search. Can be one of: "cosine", "dot_product", "euclidean". See Create a vector index and specify the source model and similarity metric for an example. Default: "cosine" |
|
|
Optional. The model used to generate the embeddings that the indexed column stores. This enables certain vector optimizations on the index. Can be one of: "ada002", "bert", "cohere-v3", "gecko", "nv-qa-4", "openai-v3-large", "openai-v3-small", "other". See Create a vector index and specify the source model and similarity metric for an example. Default: "other" |
|
|
Optional. Whether the command should silently succeed even if an index with the given name already exists in the keyspace and no new index was created. This option only checks index names. It does not check index definitions. See Create an index only if the index does not exist for an example. Default: false |
|
|
A timeout, in milliseconds, to impose on the underlying API request. |
Use the createVectorIndex
method, which belongs to the com.datastax.astra.client.tables.Table
class.
Method signature
void createVectorIndex(
String indexName,
String columnName
)
void createVectorIndex(
String indexName,
TableVectorIndexDefinition indexDefinition
)
void createVectorIndex(
String indexName,
TableVectorIndexDefinition indexDefinition,
CreateVectorIndexOptions indexOptions
)
Name | Type | Summary |
---|---|---|
|
|
The name of the index. Index names for tables must follow these rules:
|
|
The index definition. See Methods of the |
|
|
The options for this operation. See Methods of the |
Method | Type | Summary |
---|---|---|
|
|
The name of the vector column on which to create the index. The column name must use snake case, not camel case. To create indexes on non-vector columns, see Create an index. |
|
|
Optional. The similarity metric to use for vector search. Can be one of: See Create a vector index and specify the source model and similarity metric for an example. Default: |
|
|
Optional. The model used to generate the embeddings that the indexed column stores. This enables certain vector optimizations on the index. Can be one of: "ada002", "bert", "cohere-v3", "gecko", "nv-qa-4", "openai-v3-large", "openai-v3-small", "other". See Create a vector index and specify the source model and similarity metric for an example. Default: "other" |
Method | Type | Summary |
---|---|---|
|
|
Optional. Whether the command should silently succeed even if an index with the given name already exists in the keyspace and no new index was created. This option only checks index names. It does not check index definitions. See Create an index only if the index does not exist for an example. Default: false |
Use the createVectorIndex
command.
Command signature
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"createVectorIndex": {
"name": "INDEX_NAME",
"definition": {
"column": "VECTOR_COLUMN_NAME",
"options": {
"metric": STRING,
"sourceModel": STRING
}
}
}
}'
Name | Type | Summary |
---|---|---|
|
|
The name of the index. Index names for tables must follow these rules:
|
|
|
The name of the vector column on which to create the index. The column name must use snake case, not camel case. To create indexes on non-vector columns, see Create an index. |
|
|
Optional. The similarity metric to use for vector search. Can be one of: "cosine", "dot_product", "euclidean". See Create a vector index and specify the source model and similarity metric for an example. Default: "cosine" |
|
|
Optional. The model used to generate the embeddings that the indexed column stores. This enables certain vector optimizations on the index. Can be one of: "ada002", "bert", "cohere-v3", "gecko", "nv-qa-4", "openai-v3-large", "openai-v3-small", "other". See Create a vector index and specify the source model and similarity metric for an example. Default: "other" |
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
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_NAME")
# Index a vector column
table.create_vector_index("example_index_name", column="example_vector_column")
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
const table = database.table("TABLE_NAME", {
keyspace: "KEYSPACE_NAME",
});
// Index a vector column
(async function () {
await table.createVectorIndex("example_index_name", "example_vector_column");
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
DataAPIClient client = DataAPIClients.clientHCD("USERNAME", "PASSWORD");
Database database = client.getDatabase("API_ENDPOINT", "KEYSPACE_NAME");
Table<Row> table = database.getTable("TABLE_NAME");
// Index a vector column
table.createVectorIndex("example_index_name", "example_vector_column");
}
}
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: 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.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
from astrapy.constants import VectorMetric
from astrapy.info import TableVectorIndexOptions
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_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,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
const table = database.table("TABLE_NAME", {
keyspace: "KEYSPACE_NAME",
});
// Index a vector column
(async function () {
await table.createVectorIndex("example_index_name", "example_vector_column", {
options: {
metric: "dot_product",
sourceModel: "nv-qa-4",
},
});
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.vector.SimilarityMetric;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.options.CreateVectorIndexOptions;
import com.datastax.astra.client.tables.definition.indexes.TableVectorIndexDefinition;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
DataAPIClient client = DataAPIClients.clientHCD("USERNAME", "PASSWORD");
Database database = client.getDatabase("API_ENDPOINT", "KEYSPACE_NAME");
Table<Row> table = database.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 "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: 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
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_NAME")
# Index a vector column
table.create_vector_index(
"example_index_name",
column="example_vector_column",
if_not_exists=True,
)
import {
DataAPIClient,
UsernamePasswordTokenProvider,
} from "@datastax/astra-db-ts";
// Get an existing table
const client = new DataAPIClient({ environment: "hcd" });
const database = client.db("API_ENDPOINT", {
token: new UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
});
const table = database.table("TABLE_NAME", {
keyspace: "KEYSPACE_NAME",
});
// Index a vector column
(async function () {
await table.createVectorIndex("example_index_name", "example_vector_column", {
ifNotExists: true,
});
})();
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.databases.Database;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.options.CreateVectorIndexOptions;
import com.datastax.astra.client.tables.definition.indexes.TableVectorIndexDefinition;
import com.datastax.astra.client.tables.definition.rows.Row;
public class Example {
public static void main(String[] args) {
// Get an existing table
DataAPIClient client = DataAPIClients.clientHCD("USERNAME", "PASSWORD");
Database database = client.getDatabase("API_ENDPOINT", "KEYSPACE_NAME");
Table<Row> table = database.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.