Create an index (Go)
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 (Go) instead.
To index a text column for lexicographical matching, see Create a text index (Go) 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.
A successful operation does not return anything.
Parameters
Use the CreateIndex method, which belongs to the Table type.
Method signature
func (t *Table) CreateIndex(
ctx context.Context,
name string,
column any,
opts ...options.CreateIndexOption
) error
| Name | Type | Summary |
|---|---|---|
|
|
The context for the operation. |
|
|
The name of the index. Index names for tables must follow these rules:
|
|
|
The name of the table column on which to create the index. For To create vector indexes based on |
|
Optional.
A builder to generate options for this operation.
See Methods of the |
| Method | Summary |
|---|---|
|
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 |
|
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 |
|
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 |
|
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. |
|
Optional. General API options for this operation, including the timeout. |
Examples
The following examples demonstrate how to create an index.
Create an index
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")
// Index a column
err := table.CreateIndex(
ctx,
"INDEX_NAME",
"COLUMN_NAME",
)
if err != nil {
log.Fatal(err)
}
}
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.
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")
// Index a column
err := table.CreateIndex(
ctx,
"INDEX_NAME",
"COLUMN_NAME",
options.CreateIndex().SetAscii(true),
)
if err != nil {
log.Fatal(err)
}
}
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.
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")
// Index a column
err := table.CreateIndex(
ctx,
"INDEX_NAME",
"COLUMN_NAME",
options.CreateIndex().SetNormalize(true),
)
if err != nil {
log.Fatal(err)
}
}
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.
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")
// Index a column
err := table.CreateIndex(
ctx,
"INDEX_NAME",
"COLUMN_NAME",
options.CreateIndex().SetCaseSensitive(false),
)
if err != nil {
log.Fatal(err)
}
}
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:
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")
// Index a the keys of a map column
err := table.CreateIndex(
ctx,
"INDEX_NAME",
map[string]string{"MAP_COLUMN_NAME": "$keys"},
)
if err != nil {
log.Fatal(err)
}
}
To index only the values, specify the column name along with $values:
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")
// Index a the values of a map column
err := table.CreateIndex(
ctx,
"INDEX_NAME",
map[string]string{"MAP_COLUMN_NAME": "$values"},
)
if err != nil {
log.Fatal(err)
}
}
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.
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")
// Index a column
err := table.CreateIndex(
ctx,
"INDEX_NAME",
"COLUMN_NAME",
options.CreateIndex().SetIfNotExists(true),
)
if err != nil {
log.Fatal(err)
}
}
Client reference
For more information, see the client reference.