Create an index (Python)
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 (Python) instead.
To index a text column for lexicographical matching, see Create a text index (Python) 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.
Does not return anything.
Parameters
Use the create_index method, which belongs to the astrapy.Table class.
Method signature
create_index(
name: str,
column: str | dict[str, str],
*,
definition: TableIndexDefinition | dict[str, Any],
options: TableIndexOptions | 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 index. For To create vector indexes based on |
|
|
Optional.
Specifies the indexed column and index options.
If you use this parameter, the |
|
|
Optional. Specifies index options for text and ASCII types. |
|
|
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. 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 an index.
Create an index
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_index("INDEX_NAME", column="COLUMN_NAME")
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.
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
from astrapy.info import TableIndexOptions
# 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_index(
"INDEX_NAME",
column="COLUMN_NAME",
options=TableIndexOptions(ascii=True),
)
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.
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
from astrapy.info import TableIndexOptions
# 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_index(
"INDEX_NAME",
column="COLUMN_NAME",
options=TableIndexOptions(normalize=True),
)
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.
from astrapy import DataAPIClient
from astrapy.authentication import UsernamePasswordTokenProvider
from astrapy.constants import Environment
from astrapy.info import TableIndexOptions
# 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_index(
"INDEX_NAME",
column="COLUMN_NAME",
options=TableIndexOptions(case_sensitive=False),
)
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:
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 the keys of a map column
table.create_index(
name="INDEX_NAME", column={"MAP_COLUMN_NAME": "$keys"}
)
To index only the values, specify the column name along with $values:
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 the keys of a map column
table.create_index(
name="INDEX_NAME", column={"MAP_COLUMN_NAME": "$values"}
)
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.
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_index(
"INDEX_NAME", column="COLUMN_NAME", if_not_exists=True
)
Client reference
For more information, see the client reference.