Create a vector index (Java)
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 (Java) instead.
To manage indexes, your application token must have the same level of permissions that you need to manage tables.
|
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 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. To create indexes on non-vector columns, see Create an index (Java). |
|
|
Optional. The similarity metric to use for vector search. Can be one of: For an example, see Create a vector index and specify the source model and similarity metric. 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: For an example, see Create a vector index and specify the source model and similarity metric. Default: |
| 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. 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.
import com.datastax.astra.client.DataAPIClient;
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
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Index a vector column
table.createVectorIndex("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.
import com.datastax.astra.client.DataAPIClient;
import com.datastax.astra.client.core.vector.SimilarityMetric;
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
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Index a vector column
TableVectorIndexDefinition definition =
new TableVectorIndexDefinition()
.column("VECTOR_COLUMN_NAME")
.metric(SimilarityMetric.DOT_PRODUCT)
.sourceModel("nv-qa-4");
CreateVectorIndexOptions options = new CreateVectorIndexOptions();
table.createVectorIndex("INDEX_NAME", definition, options);
}
}
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.
import com.datastax.astra.client.DataAPIClient;
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
Table<Row> table =
new DataAPIClient("APPLICATION_TOKEN")
.getDatabase("API_ENDPOINT")
.getTable("TABLE_NAME");
// Index a vector column
TableVectorIndexDefinition definition =
new TableVectorIndexDefinition().column("VECTOR_COLUMN_NAME");
CreateVectorIndexOptions options = new CreateVectorIndexOptions().ifNotExists(true);
table.createVectorIndex("INDEX_NAME", definition, options);
}
}
Client reference
For more information, see the client reference.