Quick Start for CQL index management

This quick start example provides an overview of creating and altering search indexes using CQL index management commands.

Creating a search index

  1. Launch cqlsh and create a tutorial keyspace on a solr node:

    cqlsh
  2. Set up the table schema and create a default index:

  3. Create the keyspace, create the table, and create the search index on the users table from the KillrVideo application:

    CREATE KEYSPACE demo WITH
       replication = {
          'class': 'SimpleStrategy',
          'replication_factor': 1};
    
    CREATE TABLE demo.users (
       userid uuid,
       firstname text,
       lastname text,
       email text,
       created_date timestamp,
       PRIMARY KEY (userid));
    
    CREATE SEARCH INDEX ON demo.users;

    A new search index is generated on the table. Existing data is reindexed.

  4. Use the CQL shell DESCRIBE SEARCH INDEX SCHEMA to view the pending search index schema

    The generated schema looks like this:

    <schema name="autoSolrSchema" version="1.5">
      <types>
        <fieldType class="org.apache.solr.schema.TextField" name="TextField">
          <analyzer>
            <tokenizer class="solr.StandardTokenizerFactory"/>
            <filter class="solr.LowerCaseFilterFactory"/>
          </analyzer>
        </fieldType>
        <fieldType class="org.apache.solr.schema.UUIDField" name="UUIDField"/>
        <fieldType class="org.apache.solr.schema.TrieDateField" name="TrieDateField"/>
      </types>
      <fields>
        <field indexed="true" multiValued="false" name="firstname" stored="true" type="TextField"/>
        <field docValues="true" indexed="true" multiValued="false" name="userid" stored="true" type="UUIDField"/>
        <field indexed="true" multiValued="false" name="email" stored="true" type="TextField"/>
        <field indexed="true" multiValued="false" name="lastname" stored="true" type="TextField"/>
        <field docValues="true" indexed="true" multiValued="false" name="created_date" stored="true" type="TrieDateField"/>
      </fields>
      <uniqueKey>userid</uniqueKey>
    </schema>
  5. To increase tolerance of non-ASCII characters in the name field, add a new fieldType to the schema with this statement:

    ALTER SEARCH INDEX SCHEMA ON demo.users
       ADD types.fieldType [
          @name='TextField_intl' ,
          @class='org.apache.solr.schema.TextField' ]
       WITH  $${
          "analyzer": [ {
             "type": "index",
             "tokenizer": { "class": "solr.StandardTokenizerFactory" },
             "filter": [ { "class": "solr.LowerCaseFilterFactory" },
                  { "class": "solr.ASCIIFoldingFilterFactory" }
                ]
              },
              {
                "type": "search",
                "tokenizer": { "class": "solr.StandardTokenizerFactory" },
                "filter": [
                  { "class": "solr.LowerCaseFilterFactory" },
                  { "class": "solr.ASCIIFoldingFilterFactory" }
                ]
              }
            ]
          }$$;

    The dollar signs ($$) syntax in the ALTER SEARCH INDEX SCHEMA example are dollar quotes to escape a single quotation mark, see Escaping single quotation marks. This new fieldType has separate index and search analysis phases:

    <fieldType class="org.apache.solr.schema.TextField" name="TextField_intl">
      <analyzer type="index">
        <filter class="solr.LowerCaseFilterFactory"/>
        <filter class="solr.ASCIIFoldingFilterFactory"/>
        <tokenizer class="solr.StandardTokenizerFactory"/>
      </analyzer>
      <analyzer type="search">
        <filter class="solr.LowerCaseFilterFactory"/>
        <filter class="solr.ASCIIFoldingFilterFactory"/>
        <tokenizer class="solr.StandardTokenizerFactory"/>
      </analyzer>
    </fieldType>
  6. Change the type on the firstname and lastname fields:

    ALTER SEARCH INDEX SCHEMA ON demo.users
      SET field[@name='firstname']@type = 'TextField_intl';
    ALTER SEARCH INDEX SCHEMA ON demo.users
      SET field[@name='lastname']@type = 'TextField_intl';
  7. In contrast to the dsetool search index management commands, all changes made with ALTER SEARCH INDEX affect only the pending resources for the search index. To use the changes:

    1. Reload the index.

      RELOAD SEARCH INDEX ON demo.users;
    2. If there is existing data in the index, rebuild the index.

      REBUILD SEARCH INDEX ON demo.users;

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com