Create an index (TypeScript)

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 (TypeScript) instead.

To index a text column for lexicographical matching, see Create a text index (TypeScript) 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.

Returns a promise that resolves once the operation completes.

Parameters

Use the createIndex method, which belongs to the Table class.

Method signature
async createIndex(
  name: string,
  column: keyof Schema | Partial<Record<keyof Schema, string>>,
  options?: {
    ifNotExists?: boolean,
    options?: {
      ascii?: boolean,
      normalize?: boolean,
      caseSensitive?: boolean,
      timeout?: number | TimeoutDescriptor,
    },
  },
): void
Name Type Summary

name

string

The name of the index.

Index names for tables must follow these rules:

  • Must be unique within the keyspace

  • Can contain letters, numbers, and underscores

  • Must have a length of 1 to 100 characters

column

string

The name of the table column on which to create the index.

For map columns, you can index the entries, only the keys, or only the values. For an example, see Create an index for a map column.

To create vector indexes based on vector columns, see Create a vector index (TypeScript).

options

TableCreateIndexOptions

Optional. The options for this operation. See Properties of options for more details.

Properties of options
Name Type Summary

ascii

boolean

Optional. Whether to convert non-ASCII characters to their US-ASCII equivalent before indexing.

For more information, see Index options for text and ASCII types.

For an example, see Create an index and specify ASCII conversion before indexing.

Default: false

normalize

boolean

Optional. Whether to normalize Unicode characters and diacritics before indexing.

For more information, see Index options for text and ASCII types.

For an example, see Create an index and specify Unicode normalization.

Default: false

caseSensitive

boolean

Optional. Whether the index is case sensitive.

For more information, see Index options for text and ASCII types.

For an example, see Create an index and specify case sensitivity.

Default: true

ifNotExists

boolean

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.

Default: false

timeout

number | TimeoutDescriptor

A timeout, in milliseconds, to impose on the underlying API request.

Examples

The following examples demonstrate how to create an index.

Create an index

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"),
  keyspace: "KEYSPACE_NAME",
});
const table = database.table("TABLE_NAME");

// Index a column
(async function () {
  await 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 {
  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"),
  keyspace: "KEYSPACE_NAME",
});
const table = database.table("TABLE_NAME");

// Index a column
(async function () {
  await table.createIndex("INDEX_NAME", "COLUMN_NAME", {
    options: {
      ascii: true,
    },
  });
})();

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 {
  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"),
  keyspace: "KEYSPACE_NAME",
});
const table = database.table("TABLE_NAME");

// Index a column
(async function () {
  await table.createIndex("INDEX_NAME", "COLUMN_NAME", {
    options: {
      normalize: true,
    },
  });
})();

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 {
  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"),
  keyspace: "KEYSPACE_NAME",
});
const table = database.table("TABLE_NAME");

// Index a column
(async function () {
  await table.createIndex("INDEX_NAME", "COLUMN_NAME", {
    options: {
      caseSensitive: false,
    },
  });
})();

Create an index for a map column

For map columns, you can index the entries, only the keys, or only the values.

To index the entries, specify the column name as demonstrated in the "Create an index" example.

To index only the keys, specify the column name along with $keys:

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"),
  keyspace: "KEYSPACE_NAME",
});
const table = database.table("TABLE_NAME");

// Index a column
(async function () {
  await table.createIndex("INDEX_NAME", {
    "MAP_COLUMN_NAME": "$keys",
  });
})();

To index only the values, specify the column name along with $values:

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"),
  keyspace: "KEYSPACE_NAME",
});
const table = database.table("TABLE_NAME");

// Index a column
(async function () {
  await table.createIndex("INDEX_NAME", {
    "MAP_COLUMN_NAME": "$values",
  });
})();

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 {
  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"),
  keyspace: "KEYSPACE_NAME",
});
const table = database.table("TABLE_NAME");

// Index a column
(async function () {
  await table.createIndex("INDEX_NAME", "COLUMN_NAME", {
    ifNotExists: true,
  });
})();

Client reference

For more information, see the client reference.

Was this helpful?

Give Feedback

How can we improve the documentation?

© Copyright IBM Corporation 2026 | Privacy policy | Terms of use Manage Privacy Choices

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: Contact IBM