Create a text index
|
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 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
-
Python
-
TypeScript
-
Java
-
C#
-
curl
Creates a text index for the specified column.
Does not return anything.
Creates a text index for the specified column.
Returns a promise that resolves once the operation completes.
Creates a text index for the specified column.
Does not return anything.
Creates a text index for the specified column.
Does not return anything.
Creates a text index for the specified column.
If the command succeeds, the response indicates the success.
Example response:
{
"status": {
"ok": 1
}
}
Parameters
-
Python
-
TypeScript
-
Java
-
C#
-
curl
Use the create_text_index method, which belongs to the astrapy.table.Table class.
Method signature
create_text_index(
name: str,
column: str,
*,
definition: TableTextIndexDefinition | dict[str, Any],
options: TableTextIndexOptions | dict[str, Any],
if_not_exists: bool,
table_admin_timeout_ms: int,
request_timeout_ms: int,
timeout_ms: int,
) -> None
| Name | Type | Summary |
|---|---|---|
|
|
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 The column name must use snake case, not camel case. |
|
|
Optional.
Specifies the indexed column and index options.
If you use this parameter, the |
|
|
Optional. Specifies index options. If passed, it must be an instance of The only attribute of Strings must be one of the supported built-in analyzers. JSON objects must follow the specifications in Configure and use SAI text analyzers with CQL. See the examples for usage. Default: |
|
|
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.
A timeout, in milliseconds, to impose on the underlying API request.
If not provided, the This parameter is aliased as |
Use the createTextIndex method, which belongs to the Table class.
Method signature
async createTextIndex(
name: string,
column: keyof Schema | Partial<Record<keyof Schema, string>>,
options?: {
options?: {
analyzer?: string | Record<string, unknown>,
},
ifNotExists?: boolean,
timeout?: number | TimeoutDescriptor,
},
): void
| Name | Type | Summary |
|---|---|---|
|
|
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 The column name must use snake case, not camel case. |
|
|
Optional.
The options for this operation. See Properties of |
| Name | Type | Summary |
|---|---|---|
|
|
Optional. A string describing a built-in analyzer, or a JSON object describing an analyzer configuration. Strings must be one of the supported built-in analyzers. JSON objects must follow the specifications in Configure and use SAI text analyzers with CQL. See the examples for usage. Default: |
|
|
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 |
|
|
A timeout, in milliseconds, to impose on the underlying API request. |
Use the createTextIndex method, which belongs to the com.datastax.astra.client.tables.Table class.
Method signature
void createTextIndex(
String indexName,
String columnName
)
void createTextIndex(
String indexName,
TableTextIndexDefinition indexDefinition
)
void createIndex(
String indexName,
TableTextIndexDefinition indexDefinition,
CreateTextIndexOptions 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 text index. The column must be a The column name must use snake case, not camel case. |
|
|
Optional.
A string describing a built-in analyzer, or a JSON object or Strings must be one of the supported built-in analyzers. JSON objects must follow the specifications in Configure and use SAI text analyzers with CQL. See the examples for usage. Default: |
| 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 a text index only if the index does not exist. Default: false |
Use the CreateTextIndexAsync method, which belongs to the Table class.
You can also use CreateTextIndex, which is the synchronous version of the method.
Method signature
public Task CreateTextIndexAsync(
string indexName,
string columnName,
CreateTextIndexOptions options = null
);
public Task CreateTextIndexAsync<TColumn>(
string indexName,
Expression<Func<T, TColumn>> column,
CreateTextIndexOptions options = null
);
public Task CreateTextIndexAsync(
string indexName,
string columnName,
TableTextIndexDefinition indexDefinition,
CreateTextIndexOptions options = null
);
public Task CreateTextIndexAsync<TColumn>(
string indexName,
Expression<Func<T, TColumn>> column,
TableTextIndexDefinition indexDefinition,
CreateTextIndexOptions options = null
);
| Name | Type | Summary |
|---|---|---|
|
|
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 The column name must use snake case, not camel case. |
|
Definition of the index to create. You can use |
|
|
Optional.
Options for this operation.
For more information and examples for general options such as timeout, see Customize API interaction.
For options specific to this method, see Method-specific properties of the |
| Name | Type | Summary |
|---|---|---|
|
|
Optional.
A string describing a built-in analyzer, a JSON object describing an analyzer configuration, or a Strings must be one of the supported built-in analyzers. JSON objects must follow the specifications in Configure and use SAI text analyzers with CQL. See the examples for usage. Default: |
|
|
Optional.
An alternative to the The See the examples for usage. |
| Name | 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 a text index only if the index does not exist. Default: false |
Use the createTextIndex command.
Command signature
curl -sS -L -X POST "API_ENDPOINT/api/json/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"createTextIndex": {
"name": "INDEX_NAME",
"definition": {
"column": "COLUMN_NAME",
"options": {
"analyzer": ANALYZER
}
},
"options": {
"ifNotExists": BOOLEAN
}
}
}'
| Name | Type | Summary |
|---|---|---|
|
|
The name of the index. Index names for tables must follow these rules:
|
|
|
Optional.
The index definition. See Properties of |
|
|
Optional.
The options for this operation. See Properties of |
| Name | Type | Summary |
|---|---|---|
|
|
The name of the table column on which to create the text index. The column must be a The column name must use snake case, not camel case. |
|
|
Optional. A string describing a built-in analyzer, or a JSON object describing an analyzer configuration. Strings must be one of the supported built-in analyzers. JSON objects must follow the specifications in Configure and use SAI text analyzers with CQL. See the examples for usage. Default: |
| Name | 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 a text index only if the index does not exist. Default: false |
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.
-
Python
-
TypeScript
-
Java
-
C#
-
curl
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_NAME")
# Index a column
table.create_text_index("INDEX_NAME", column="TEXT_COLUMN_NAME")
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"),
});
const table = database.table("TABLE_NAME", {
keyspace: "KEYSPACE_NAME",
});
// Index a column
(async function () {
await table.createTextIndex("INDEX_NAME", "TEXT_COLUMN_NAME");
})();
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.createTextIndex("INDEX_NAME", "TEXT_COLUMN_NAME");
}
}
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
var table = database.GetTable("TABLE_NAME");
// Index a column
await table.CreateTextIndexAsync(
"INDEX_NAME",
"TEXT_COLUMN_NAME"
);
}
}
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"createTextIndex": {
"name": "INDEX_NAME",
"definition": {
"column": "TEXT_COLUMN_NAME"
}
}
}'
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.
-
Python
-
TypeScript
-
Java
-
C#
-
curl
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
from astrapy.info import TableTextIndexOptions
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_NAME")
# Index a column
table.create_text_index(
"INDEX_NAME",
column="TEXT_COLUMN_NAME",
options=TableTextIndexOptions(
analyzer="english",
),
)
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"),
});
const table = database.table("TABLE_NAME", {
keyspace: "KEYSPACE_NAME",
});
// Index a column
(async function () {
await table.createTextIndex("INDEX_NAME", "TEXT_COLUMN_NAME", {
options: {
analyzer: "english",
},
});
})();
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.lexical.AnalyzerTypes;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.indexes.TableTextIndexDefinition;
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
TableTextIndexDefinition definition =
new TableTextIndexDefinition()
.column("TEXT_COLUMN_NAME")
.analyzer(AnalyzerTypes.ENGLISH);
table.createTextIndex("INDEX_NAME", definition);
}
}
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
var table = database.GetTable("TABLE_NAME");
// Index a column
await table.CreateTextIndexAsync(
"INDEX_NAME",
"TEXT_COLUMN_NAME",
Builders.TableIndex.Text("english")
);
}
}
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"createTextIndex": {
"name": "INDEX_NAME",
"definition": {
"column": "TEXT_COLUMN_NAME",
"options": {
"analyzer": "english"
}
}
}
}'
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.
-
Python
-
TypeScript
-
Java
-
C#
-
curl
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
from astrapy.info import TableTextIndexOptions
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_NAME")
# Index a column
table.create_text_index(
"INDEX_NAME",
column="TEXT_COLUMN_NAME",
options=TableTextIndexOptions(
analyzer={
"tokenizer": {"name": "standard", "args": {}},
"filters": [
{"name": "lowercase"},
{"name": "stop"},
{"name": "porterstem"},
{"name": "asciifolding"},
],
"charFilters": [],
},
),
)
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"),
});
const table = database.table("TABLE_NAME", {
keyspace: "KEYSPACE_NAME",
});
// Index a column
(async function () {
await table.createTextIndex("INDEX_NAME", "TEXT_COLUMN_NAME", {
options: {
analyzer: {
tokenizer: {
name: "standard",
args: {},
},
filters: [
{
name: "lowercase",
},
{
name: "stop",
},
{
name: "porterstem",
},
{
name: "asciifolding",
},
],
charFilters: [],
},
},
});
})();
import static com.datastax.astra.client.core.lexical.AnalyzerTypes.STANDARD;
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.core.lexical.Analyzer;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.definition.indexes.TableTextIndexDefinition;
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
Analyzer analyzer =
new Analyzer()
.tokenizer(STANDARD.getValue())
.addFilter("lowercase")
.addFilter("stop")
.addFilter("porterstem")
.addFilter("asciifolding");
TableTextIndexDefinition definition =
new TableTextIndexDefinition().column("TEXT_COLUMN_NAME").analyzer(analyzer);
table.createTextIndex("INDEX_NAME", definition);
}
}
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
var table = database.GetTable("TABLE_NAME");
// Index a column
await table.CreateTextIndexAsync(
"INDEX_NAME",
"TEXT_COLUMN_NAME",
Builders.TableIndex.Text(
new AnalyzerOptions
{
Tokenizer = new TokenizerOptions
{
Name = "standard",
Arguments = new Dictionary<string, object>(),
},
Filters = { "lowercase", "stop", "porterstem", "asciifolding" },
CharacterFilters = new List<string>(),
}
)
);
}
}
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"createTextIndex": {
"name": "INDEX_NAME",
"definition": {
"column": "TEXT_COLUMN_NAME",
"options": {
"analyzer": {
"tokenizer": {
"name": "standard",
"args": {}
},
"filters": [
{
"name": "lowercase"
},
{
"name": "stop"
},
{
"name": "porterstem"
},
{
"name": "asciifolding"
}
],
"charFilters": []
}
}
}
}
}'
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.
-
Python
-
TypeScript
-
Java
-
C#
-
curl
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
# Get an existing table
client = DataAPIClient(environment=Environment.HCD)
database = client.get_database(
"API_ENDPOINT",
token=UsernamePasswordTokenProvider("USERNAME", "PASSWORD"),
)
table = database.get_table("TABLE_NAME", keyspace="KEYSPACE_NAME")
# Index a column
table.create_text_index(
"INDEX_NAME",
column="TEXT_COLUMN_NAME",
if_not_exists=True,
)
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"),
});
const table = database.table("TABLE_NAME", {
keyspace: "KEYSPACE_NAME",
});
// Index a column
(async function () {
await table.createTextIndex("INDEX_NAME", "TEXT_COLUMN_NAME", {
ifNotExists: true,
});
})();
import com.datastax.astra.client.DataAPIClients;
import com.datastax.astra.client.tables.Table;
import com.datastax.astra.client.tables.commands.options.CreateTextIndexOptions;
import com.datastax.astra.client.tables.definition.indexes.TableTextIndexDefinition;
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
CreateTextIndexOptions options = new CreateTextIndexOptions().ifNotExists(true);
TableTextIndexDefinition definition =
new TableTextIndexDefinition().column("TEXT_COLUMN_NAME");
table.createTextIndex("INDEX_NAME", definition, options);
}
}
using DataStax.AstraDB.DataApi;
using DataStax.AstraDB.DataApi.Core;
using DataStax.AstraDB.DataApi.Tables;
namespace Examples;
public class Program
{
static async Task Main()
{
// Get an existing table
var client = new DataAPIClient(
new CommandOptions() { Destination = DataAPIDestination.HCD }
);
var database = client.GetDatabase(
"API_ENDPOINT",
DataAPIClient.UsernamePasswordTokenProvider(
"USERNAME",
"PASSWORD"
),
"KEYSPACE_NAME"
);
var table = database.GetTable("TABLE_NAME");
// Index a column
await table.CreateTextIndexAsync(
"INDEX_NAME",
"TEXT_COLUMN_NAME",
new CreateTextIndexOptions() { IfNotExists = true }
);
}
}
curl -sS -L -X POST "API_ENDPOINT/v1/KEYSPACE_NAME/TABLE_NAME" \
--header "Token: APPLICATION_TOKEN" \
--header "Content-Type: application/json" \
--data '{
"createTextIndex": {
"name": "INDEX_NAME",
"definition": {
"column": "TEXT_COLUMN_NAME"
},
"options": {
"ifNotExists": true
}
}
}'
Client reference
-
Python
-
TypeScript
-
Java
-
C#
-
curl
For more information, see the client reference.
For more information, see the client reference.
For more information, see the client reference.
For more information, see the client reference.
Client reference documentation is not applicable for HTTP.