Create a vector index (C#)
|
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 (C#) 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 CreateVectorIndexAsync method, which belongs to the Table class.
You can also use CreateVectorIndex, which is the synchronous version of the method.
Method signature
public Task CreateVectorIndexAsync(
string indexName,
string columnName,
CreateVectorIndexOptions options = null
);
public Task CreateVectorIndexAsync(
string indexName,
string columnName,
TableVectorIndexDefinition indexDefinition,
CreateVectorIndexOptions options = null
);
public Task CreateVectorIndexAsync<TColumn>(
string indexName,
Expression<Func<T, TColumn>> column,
CreateVectorIndexOptions options = null
);
public Task CreateVectorIndexAsync<TColumn>(
string indexName,
Expression<Func<T, TColumn>> column,
TableVectorIndexDefinition indexDefinition,
CreateVectorIndexOptions options = null
);
| 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 (C#). |
|
Definition of the index to create. You can use |
|
|
Optional.
Options for this operation.
For more information and examples for general options such as timeout, see Customize API interaction.
For options specific to this method, see Method-specific properties of the |
| Name | Type | Summary |
|---|---|---|
|
|
Optional. The similarity metric to use for vector search. Can be one of: Default: For an example, see Create a vector index and specify the source model and similarity metric. |
|
|
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: Default: For an example, see Create a vector index and specify the source model and similarity metric. |
| Name | 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. For an example, see Create an index only if the index does not exist. Default: false |
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.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
var table = database.GetTable("TABLE_NAME");
// Index a column
await table.CreateVectorIndexAsync(
"INDEX_NAME",
"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.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
var table = database.GetTable("TABLE_NAME");
// Index a column
await table.CreateVectorIndexAsync(
"INDEX_NAME",
"VECTOR_COLUMN_NAME",
Builders.TableIndex.Vector(SimilarityMetric.DotProduct, "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.
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
var table = database.GetTable("TABLE_NAME");
// Index a column
await table.CreateVectorIndexAsync(
"INDEX_NAME",
"VECTOR_COLUMN_NAME",
Builders.TableIndex.Vector(SimilarityMetric.DotProduct, "nv-qa-4"),
new CreateVectorIndexOptions() { IfNotExists = true }
);
}
}
Client reference
For more information, see the client reference.