Create a text index (Go)
|
Lexicographical matching is currently in public preview. Development is ongoing, and the features and functionality are subject to change. Astra DB Serverless, 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 |
|---|---|---|
|
|
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 text index. The column must be a |
|
Optional.
A builder to generate options for this operation.
See Methods of the |
| Method | Summary |
|---|---|
|
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 See the examples for usage. Default: The standard Apache Lucene™ analyzer. |
|
Optional. Sets the analyzer by passing a JSON object describing an analyzer configuration. JSON objects must follow the specifications in Find data with CQL analyzers. Alternatively, use See the examples for usage. Default: The standard Apache Lucene™ analyzer. |
|
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 a text index only if the index does not exist. Default: false |
|
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()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
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()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
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 Find data with CQL analyzers.
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()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
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()
database := client.Database(
"API_ENDPOINT",
options.API().SetToken("APPLICATION_TOKEN"),
)
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.