Index

An index provides a means of expanding the query capabilities of a table. SchemaBuilder offers API methods for creating and dropping indices. Unlike other schema members, there is no mechanism to alter an index.

Creating an Index (CREATE INDEX)

To start a CREATE INDEX query, use createIndex in SchemaBuilder:

import static com.datastax.oss.driver.api.querybuilder.SchemaBuilder.*;

// an index name is not required
CreateIndexStart create = createIndex();

create = createIndex("my_idx");

Unlike other keyspace elements, there is not option to provide the keyspace name, instead it is implied from the indexed table’s keyspace.

Like all other CREATE queries, one may supply ifNotExists() to require that the index should only be created if it doesn’t already exist, i.e.:

CreateIndexStart create = createIndex("my_idx").ifNotExists();

Note one small difference with IF NOT EXISTS with indices is that the criteria also applies to whether or not the table and column specification has an index already, not just the name of the index.

At this stage, the query cannot be completed yet. You need to provide at least:

  • The table the index applies to using onTable
  • The column the index applies to using an andColumn* implementation.

For example:

createIndex().onTable("tbl").andColumnKeys("addresses");
// CREATE INDEX ON tbl (KEYS(addresses))

Custom Indices

Cassandra supports indices with a custom implementation, specified by an input class name. The class implementation may be specified using custom(className), for example:

createIndex()
    .custom("org.apache.cassandra.index.MyCustomIndex")
    .onTable("tbl")
    .andColumn("x");
// CREATE CUSTOM INDEX ON tbl (x) USING 'org.apache.cassandra.index.MyCustomIndex'

One popular custom index implementation is SASI (SSTable Attached Secondary Index). To use SASI, use usingSASI and optionally withSASIOptions:

createIndex()
    .usingSASI()
    .onTable("tbl")
    .andColumn("x")
    .withSASIOptions(ImmutableMap.of("mode", "CONTAINS", "tokenization_locale", "en"));
// CREATE CUSTOM INDEX ON tbl (x) USING 'org.apache.cassandra.index.sasi.SASIIndex' WITH OPTIONS={'mode':'CONTAINS','tokenization_locale':'en'}

Column Index Types

When indexing columns, one may simply use andColumn. However, when indexing collection columns there are several additional options available:

  • andColumnKeys: Creates an index on a map column’s keys.
  • andColumnValues: Creates an index on a map column’s values.
  • andColumnEntries: Creates an index on a map column’s entries.
  • andColumnFull: Creates an index of a frozen collection’s full value.

Index options

After specifying the columns for the index, you may use withOption to provide free-form options on the index. These are really only applicable to custom index implementations.

Dropping an Index (DROP INDEX)

To create a DROP INDEX query, use dropIndex:

dropIndex("ks", "my_idx");
// DROP INDEX ks.my_idx

You may also specify ifExists:

dropIndex("my_idx").ifExists();
// DROP INDEX IF EXISTS my_idx