Create a vector index (Python)
|
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 (Python) 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
Creates an index for the specified vector column.
Does not return anything.
Parameters
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. To create indexes on non-vector columns, see Create an index (Python). |
|
|
Optional.
Specifies the indexed column and index options.
If you use this parameter, the |
|
|
Optional. Specifies the index options:
For an example, see Create a vector index and specify the source model and similarity metric. |
|
|
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. For an example, see Create an index only if the index does not exist. Default: false |
|
|
Optional.
A timeout, in milliseconds, to impose on the underlying API request.
If not provided, the This parameter is aliased as |
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.
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"),
keyspace="KEYSPACE_NAME",
)
table = database.get_table("TABLE_NAME")
# Index a vector column
table.create_vector_index(
"INDEX_NAME", column="VECTOR_COLUMN_NAME"
)
Create a vector index and specify the source model and similarity metric
When you create a vector index, you can specify the embedding source model, the similarity metric, or both.
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment, 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"),
keyspace="KEYSPACE_NAME",
)
table = database.get_table("TABLE_NAME")
# Index a vector column
table.create_vector_index(
"INDEX_NAME",
column="VECTOR_COLUMN_NAME",
options=TableVectorIndexOptions(
metric=VectorMetric.DOT_PRODUCT, source_model="nv-qa-4"
),
)
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.
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"),
keyspace="KEYSPACE_NAME",
)
table = database.get_table("TABLE_NAME")
# Index a vector column
table.create_vector_index(
"INDEX_NAME", column="VECTOR_COLUMN_NAME", if_not_exists=True
)
Client reference
For more information, see the client reference.