Create a text index (Go)

Lexicographical matching is 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 text index for a text or ascii column in a table.

You must create a text index if you want to perform lexicographical matching on the column.

To index a text column for sorting and filtering other than lexicographical matching, see Create an index (Go) 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 a text index for the specified column.

A successful operation does not return anything.

Parameters

Use the CreateTextIndex method, which belongs to the Table type.

Method signature
func (t *Table) CreateTextIndex(
  ctx context.Context,
  name string,
  column string,
  opts ...options.CreateTextIndexOption
) error
Name Type Summary

ctx

context.Context

The context for the operation.

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 text index.

The column must be a text or ascii column.

opts

…​options.CreateTextIndexOption

Optional. A builder to generate options for this operation. See Methods of the CreateTextIndexOption builder for more details.

Methods of the CreateTextIndexOption builder
Method Summary

SetAnalyzer(v string)

Optional. Sets the analyzer by passing a string describing a built-in analyzer. Strings must be one of the supported built-in analyzers.

Alternatively, use SetCustomAnalyzer().

See the examples for usage.

Default: The standard Apache Lucene™ analyzer.

SetCustomAnalyzer(v map[string]any)

Optional. Sets the analyzer by passing a JSON object describing an analyzer configuration. JSON objects must follow the specifications in Configure and use SAI text analyzers with CQL.

Alternatively, use SetAnalyzer().

See the examples for usage.

Default: The standard Apache Lucene™ analyzer.

SetIfNotExists(v bool)

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

UpdateAPIOptions(v …​APIOption)

Optional. General API options for this operation, including the timeout.

Examples

The following examples demonstrate how to create a text index.

Create a text index and use the default analyzer

If you don’t specify an analyzer, the index will use the standard Apache Lucene™ analyzer.

package main

import (
	"context"
	"log"

	"github.com/datastax/astra-db-go/v2/astra"
	"github.com/datastax/astra-db-go/v2/astra/options"
)

func main() {
	ctx := context.Background()

	// Get an existing table
	client := astra.NewClient(
		options.API().SetEnvironment(options.EnvironmentHCD),
	)

	database := client.Database(
		"API_ENDPOINT",
		options.API().
			SetUsernamePasswordTokenProvider(
				"USERNAME",
				"PASSWORD",
			).
			SetKeyspace("KEYSPACE_NAME"),
	)

	table := database.Table("TABLE_NAME")

	// Create a text index
	err := table.CreateTextIndex(
		ctx,
		"INDEX_NAME",
		"TEXT_COLUMN_NAME",
	)
	if err != nil {
		log.Fatal(err)
	}
}

Create a text index and specify the analyzer as a string

You can use a string to specify the analyzer. Strings must be one of the supported built-in analyzers.

Alternatively, you can describe the analyzer configuration as a JSON object as demonstrated in Create a text index and specify the analyzer as an object.

package main

import (
	"context"
	"log"

	"github.com/datastax/astra-db-go/v2/astra"
	"github.com/datastax/astra-db-go/v2/astra/options"
)

func main() {
	ctx := context.Background()

	// Get an existing table
	client := astra.NewClient(
		options.API().SetEnvironment(options.EnvironmentHCD),
	)

	database := client.Database(
		"API_ENDPOINT",
		options.API().
			SetUsernamePasswordTokenProvider(
				"USERNAME",
				"PASSWORD",
			).
			SetKeyspace("KEYSPACE_NAME"),
	)

	table := database.Table("TABLE_NAME")

	// Create a text index
	err := table.CreateTextIndex(
		ctx,
		"INDEX_NAME",
		"TEXT_COLUMN_NAME",
		options.CreateTextIndex().SetAnalyzer("english"),
	)
	if err != nil {
		log.Fatal(err)
	}
}

Create a text index and specify the analyzer as an object

You can describe the analyzer configuration as a JSON object. JSON objects must follow the specifications in Configure and use SAI text analyzers with CQL.

The following example uses a configuration suitable for English text. Alternatively, you can use the string shorthand demonstrated in Create a text index and specify the analyzer as a string.

package main

import (
	"context"
	"log"

	"github.com/datastax/astra-db-go/v2/astra"
	"github.com/datastax/astra-db-go/v2/astra/options"
)

func main() {
	ctx := context.Background()

	// Get an existing table
	client := astra.NewClient(
		options.API().SetEnvironment(options.EnvironmentHCD),
	)

	database := client.Database(
		"API_ENDPOINT",
		options.API().
			SetUsernamePasswordTokenProvider(
				"USERNAME",
				"PASSWORD",
			).
			SetKeyspace("KEYSPACE_NAME"),
	)

	table := database.Table("TABLE_NAME")

	// Create a text index
	err := table.CreateTextIndex(
		ctx,
		"INDEX_NAME",
		"TEXT_COLUMN_NAME",
		options.CreateTextIndex().SetCustomAnalyzer(map[string]any{
			"tokenizer": map[string]any{
				"name": "standard",
				"args": map[string]any{},
			},
			"filters": []map[string]any{
				{"name": "lowercase"},
				{"name": "stop"},
				{"name": "porterstem"},
				{"name": "asciifolding"},
			},
			"charFilters": []map[string]any{},
		}),
	)
	if err != nil {
		log.Fatal(err)
	}
}

Create a text index only if the index does not exist

Use this option to silently do nothing if a text index with the specified name already exists.

This option only checks index names. It does not check index definitions.

package main

import (
	"context"
	"log"

	"github.com/datastax/astra-db-go/v2/astra"
	"github.com/datastax/astra-db-go/v2/astra/options"
)

func main() {
	ctx := context.Background()

	// Get an existing table
	client := astra.NewClient(
		options.API().SetEnvironment(options.EnvironmentHCD),
	)

	database := client.Database(
		"API_ENDPOINT",
		options.API().
			SetUsernamePasswordTokenProvider(
				"USERNAME",
				"PASSWORD",
			).
			SetKeyspace("KEYSPACE_NAME"),
	)

	table := database.Table("TABLE_NAME")

	// Create a text index
	err := table.CreateTextIndex(
		ctx,
		"INDEX_NAME",
		"TEXT_COLUMN_NAME",
		options.CreateTextIndex().SetIfNotExists(true),
	)
	if err != nil {
		log.Fatal(err)
	}
}

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