Create a text index (Python)
|
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 (Python) 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.
Does not return anything.
Parameters
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 |
|
|
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 |
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.
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"),
keyspace="KEYSPACE_NAME",
)
table = database.get_table("TABLE_NAME")
# Index a column
table.create_text_index("INDEX_NAME", 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.
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"),
keyspace="KEYSPACE_NAME",
)
table = database.get_table("TABLE_NAME")
# Index a column
table.create_text_index(
"INDEX_NAME",
column="TEXT_COLUMN_NAME",
options=TableTextIndexOptions(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.
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"),
keyspace="KEYSPACE_NAME",
)
table = database.get_table("TABLE_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": [],
}
),
)
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.
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"),
keyspace="KEYSPACE_NAME",
)
table = database.get_table("TABLE_NAME")
# Index a column
table.create_text_index(
"INDEX_NAME", column="TEXT_COLUMN_NAME", if_not_exists=True
)
Client reference
For more information, see the client reference.