Scylla Documentation Logo Documentation
  • Server
    • Scylla Open Source
    • Scylla Enterprise
    • Scylla Alternator
  • Cloud
    • Scylla Cloud
    • Scylla Cloud Docs
  • Tools
    • Scylla Manager
    • Scylla Monitoring Stack
    • Scylla Operator
  • Drivers
    • CQL Drivers
    • DynamoDB Drivers
Download
Menu

Caution

You're viewing documentation for a previous version of Scylla Java Driver. Switch to the latest stable version.

Scylla Java Driver Manual Query builder Schema builder Index

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
PREVIOUS
Function
NEXT
Keyspace
  • 4.7.2.x
    • 4.13.0.x
    • 4.12.0.x
    • 4.11.1.x
    • 4.10.0.x
    • 4.7.2.x
    • 3.11.2.x
    • 3.11.0.x
    • 3.10.2.x
    • 3.7.2.x
  • Java Driver for Scylla and Apache Cassandra®
  • API Documentation
  • Manual
    • API conventions
    • Case sensitivity
    • Core driver
      • Address resolution
      • Asynchronous programming
      • Authentication
      • Bill of Materials (BOM)
      • Compression
      • Configuration
        • Reference configuration
      • Control connection
      • Custom codecs
      • Detachable types
      • Query idempotence
      • Integration
      • Load balancing
      • Logging
      • Metadata
        • Node metadata
        • Schema metadata
        • Token metadata
      • Metrics
      • Native protocol
      • Paging
      • Performance
      • Connection pooling
      • Query timestamps
      • Reactive Style Programming
      • Reconnection
      • Request tracker
      • Retries
      • Using the shaded JAR
      • Speculative query execution
      • SSL
      • Statements
        • Batch statements
        • Per-query keyspace
        • Prepared statements
        • Simple statements
      • Temporal types
      • Request throttling
      • Query tracing
      • Tuples
      • User-defined types
    • Developer docs
      • Administrative tasks
      • Common infrastructure
        • Concurrency
        • Driver context
        • Event bus
      • Native protocol layer
      • Netty pipeline
      • Request execution
    • Mapper
      • Integration
      • DAOs
        • Custom result types
        • Delete methods
        • GetEntity methods
        • Insert methods
        • Null saving strategy
        • Query methods
        • Query provider methods
        • Select methods
        • SetEntity methods
        • Statement attributes
        • Update methods
      • Entities
      • Mapper interface
    • OSGi
    • Query builder
      • Conditions
      • DELETE
      • Idempotence in the query builder
      • INSERT
      • Relations
      • Schema builder
        • Aggregate
        • Function
        • Index
        • Keyspace
        • Materialized View
        • Table
        • Type
      • SELECT
      • Terms
      • TRUNCATE
      • UPDATE
  • Upgrade guide
  • Frequently asked questions
  • Changelog
  • Create an issue
  • Edit this page

On this page

  • Index
    • Creating an Index (CREATE INDEX)
      • Custom Indices
      • Column Index Types
      • Index options
    • Dropping an Index (DROP INDEX)
Logo
Docs Contact Us About Us
Mail List Icon Slack Icon
© 2022, ScyllaDB. All rights reserved.
Last updated on 25 May 2022.
Powered by Sphinx 4.3.2 & ScyllaDB Theme 1.2.2