Create an index (Java)
|
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 column in a table in a database.
You should index any column that you want to sort or filter on that isn’t part of the primary key. Columns that are part of the primary key do not require a secondary index.
To create an index on a vector column, see Create a vector index (Java) 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 column.
Does not return anything.
Parameters
Use the createIndex method, which belongs to the com.datastax.astra.client.tables.Table class.
Method signature
void createIndex(
String indexName,
String columnName
)
void createIndex(
String indexName,
String columnName,
CreateIndexOptions indexOptions
)
void createIndex(
String indexName,
TableIndexDefinition indexDefinition,
CreateIndexOptions 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 table column on which to create the index. You cannot create indexes based on To create vector indexes based on |
|
|
Optional. Specifies index options for text and ASCII types. |
| 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 an index.
Create an index
import com.datastax.astra.client.DataAPIClients;
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 =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Index a column
table.createIndex("INDEX_NAME", "COLUMN_NAME");
}
}
Create an index and specify ASCII conversion before indexing
When you index a text or ASCII column, you can specify whether to convert non-ASCII characters to their US-ASCII equivalent before indexing.
Converting non-ASCII characters can improve search performance by limiting the range of allowed ASCII characters. This option is best for ASCII-only data or ASCII-only applications where you can safely ignore non-ASCII characters. It is not recommended for multilingual data or applications that must support non-ASCII characters.
For more information, see Index options for text and ASCII types.
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.options.CreateIndexOptions;
import com.datastax.astra.client.tables.definition.indexes.TableIndexDefinitionOptions;
import com.datastax.astra.client.tables.definition.indexes.TableRegularIndexDefinition;
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 =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Index a column
TableRegularIndexDefinition definition =
new TableRegularIndexDefinition()
.column("COLUMN_NAME")
.options(new TableIndexDefinitionOptions().ascii(true));
CreateIndexOptions options = new CreateIndexOptions();
table.createIndex("INDEX_NAME", definition, options);
}
}
Create an index and specify Unicode normalization
When you index a text or ASCII column, you can specify whether to normalize Unicode characters and diacritics before indexing.
Normalizing Unicode characters can improve search consistency and inclusivity. This option is best for datasets that can contain multiple Unicode versions of the same character, such as multilingual data or non-standardized user input.
For more information, see Index options for text and ASCII types.
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.options.CreateIndexOptions;
import com.datastax.astra.client.tables.definition.indexes.TableIndexDefinitionOptions;
import com.datastax.astra.client.tables.definition.indexes.TableRegularIndexDefinition;
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 =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Index a column
TableRegularIndexDefinition definition =
new TableRegularIndexDefinition()
.column("COLUMN_NAME")
.options(new TableIndexDefinitionOptions().normalize(true));
CreateIndexOptions options = new CreateIndexOptions();
table.createIndex("INDEX_NAME", definition, options);
}
}
Create an index and specify case sensitivity
When you index a text or ASCII column, you can specify whether the index is case sensitive.
Enforcing case sensitivity is best for datasets where you want to enforce exact matches based on capitalization.
For more information, see Index options for text and ASCII types.
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.options.CreateIndexOptions;
import com.datastax.astra.client.tables.definition.indexes.TableIndexDefinitionOptions;
import com.datastax.astra.client.tables.definition.indexes.TableRegularIndexDefinition;
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 =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Index a column
TableRegularIndexDefinition definition =
new TableRegularIndexDefinition()
.column("COLUMN_NAME")
.options(new TableIndexDefinitionOptions().caseSensitive(false));
CreateIndexOptions options = new CreateIndexOptions();
table.createIndex("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.DataAPIClients;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.options.CreateIndexOptions;
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 =
DataAPIClients.clientHCD("USERNAME", "PASSWORD")
.getDatabase("API_ENDPOINT", "KEYSPACE_NAME")
.getTable("TABLE_NAME");
// Index a column
CreateIndexOptions options = new CreateIndexOptions().ifNotExists(true);
table.createIndex("INDEX_NAME", "COLUMN_NAME", options);
}
}
Client reference
For more information, see the client reference.