CREATE INDEX
Defines a new index for a single column of a table.
CQL supports creating an index on most columns, including the partition and cluster columns of a PRIMARY KEY
, collections, and static columns.
The one exception is that an index cannot be defined based on a single-column partition key.
All column date types except the following are supported for indexes:
-
counter
-
geospatial types:
PointType
,LineStringType
,PolygonType
-
non-frozen user-defined type (UDT)
For maps, you can create indexes using the key, value, or entry (a key:value pair). You can create multiple secondary indexes on the same database table, with each index based on any column in the table.
You can define an index on any single column in a table’s composite partition key, i.e., a partition key comprised of multiple columns. If you need to query based on one of those columns, an index is a helpful option. In fact, you can define an index on each column in a composite partition key, if needed.
Defining one or more indexes based on any column in a database table (with the rules noted above) subsequently enables performant queries that use the indexed column to filter results.
See also: DROP INDEX, SAI section
Syntax
CREATE [CUSTOM] INDEX [ IF NOT EXISTS ] [ <index_name> ]
ON [<keyspace_name>.]<table_name>
([ KEYS | VALUES | ENTRIES | FULL] <column_name>)
USING <index_type>
[ WITH OPTIONS = { <option_map> } ] ;
Syntax legend
Syntax conventions | Description |
---|---|
UPPERCASE |
Literal keyword. |
Lowercase |
Not literal. |
|
Variable value. Replace with a user-defined value. |
|
Optional.
Square brackets ( |
|
Group.
Parentheses ( |
|
Or.
A vertical bar ( |
|
Repeatable.
An ellipsis ( |
|
Single quotation ( |
|
Map collection.
Braces ( |
Set, list, map, or tuple.
Angle brackets ( |
|
|
End CQL statement.
A semicolon ( |
|
Separate the command line options from the command arguments with two hyphens ( |
|
Search CQL only: Single quotation marks ( |
|
Search CQL only: Identify the entity and literal value to overwrite the XML element in the schema and solrConfig files. |
Parameters
Parameter | Description | Default |
---|---|---|
|
Optional. Identifier of the index. Enclose in quotes to use special characters or preserve capitalization. |
If no name is specified, CQL names the index: |
|
Optional. Name of the keyspace that contains the table to index. |
If no name is specified, the current keyspace is used. |
|
Name of the table to index. |
|
|
Name of the column to index. If used with a map, the column name is the map name. For maps, you can create indexes using the key, value, entry (a key:value pair), or full content of the collection. |
1.
Index names are unique per keyspace. The index name must be a unique identifier for the index for each table within a keyspace. This requirement is true for both vector and non-vector indexes. |
Index types
Option | Description |
---|---|
|
A feature that is not available in all CQL distributions, but is recommended for production use in the distributions where it is available.
Not required for Astra.
ifdev:cass50,hcd10[]
Define with ` USING
endif:cass50,hcd10[] |
Options
SAI options
The options are specific to the index type and are not required.
The <option_map>
is a map of options and their values defined in JSON simple format.
Option | Description | Default |
---|---|---|
|
Vector search relies on computing the similarity between vectors to identify relevant matches.
Choices are |
|
|
Configures the index for optimal performance for your vectors.
Choices are: |
|
|
Allows case-insensitive searches. |
|
|
Allow searches to be normalized for Unicode characters.
SAI supports Normalization Form C (NFC) Unicode.
When set to |
|
|
Allow searches to be limited to ASCII characters.
When set to |
|
|
The class that implements the analyzer.
Choices are |
|
|
The tokenizer for the index. Choices are standard, whitespace, ngram, keyword, simplepattern, stop, and mapping. |
|
|
The filters for the index. Choices are: porterstem, lowercase, synonym, and languages, including stem definitions like czechstem. |
None. |
|
The character filters for the index. Choices are htmlstrip, mapping, and patternreplace. |
None. |
SASI options
Option | Description | Default |
---|---|---|
|
Allows case-insensitive searches. |
|
|
Allow searches to be normalized for Unicode characters.
SAI supports Normalization Form C (NFC) Unicode.
When set to |
|
|
Allow searches to be limited to ASCII characters.
When set to |
|
|
The mode of the index.
Choices are |
|
|
The delimiter for the index. |
` ` (whitespace) |
|
The class that implements the analyzer.
Choices are |
|
|
Whether the index is analyzed. |
|
|
Whether tokenization stemming is enabled. |
|
|
The locale for tokenization. |
|
|
Whether tokenization normalization is enabled. |
|
|
Whether tokenization stop words are skipped. |
|
Usage notes
If the column already contains data, it is indexed during the execution of this statement. After an index has been created, it is automatically updated when data in the column changes.
Indexing with the CREATE INDEX
command can impact performance.
Before creating an index, be aware of when and when not to create an index.
Restriction: Indexing counter columns is not supported.
Supported databases:
-
DataStax DataStax Astra DB (Vector)
-
DataStax DataStax Astra DB DataStax Astra DB DataStax Astra DB Serverless
-
DataStax DataStax Astra DB DataStax Astra DB DataStax Astra DB Classic
-
HCD 1.0
-
DataStax Enterprise 6.9.0 and later
-
DataStax Enterprise 6.8.3 and later
Since indexes are created at the keyspace level and not at the table level.
If you use the |
SAI supports the following query operators for tables with SAI indexes:
-
Numerics:
=
,<
,>
,⇐
,>=
,AND
,IN
,OR
(for vector databases) -
Strings:
=
,AND
,IN
-
Collections:
=
CONTAINS
,CONTAINS KEY
SAI does not supports the following query operators for tables with SAI indexes:
-
Strings or Numerics:
LIKE
,OR
(for non-vector databases)
Supported databases:
-
DataStax DataStax Astra DB DataStax Astra DB DataStax Astra DB Classic
-
HCD 1.0
-
DataStax Enterprise 6.9.0 and later
-
DataStax Enterprise 6.8.3 and later
SASI supports the following query operators for tables with SASI indexes:
-
Numerics:
=
,<
,>
,⇐
,>=
,AND
,IN
-
Strings:
=
AND
,IN
,LIKE
Examples
Detailed examples can be found for each type of indexing:
Index type | Example links |
---|---|
SAI |