Syntax for changing schema settings

Describes the syntax used to change schema settings.

Use the ALTER SEARCH INDEX to add, set, or drop settings of an existing search index schema.

The search index schema is in XML format and supports most Solr schema.xml elements:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<schema name="autoSolrSchema" version="1.5">
  <types>
    <fieldType class="class_name" name="type_name"/>
      <analyzer>
        <tokenizer class="class_name"/>
        <filter class="class_name"/>
      </analyzer>
    </fieldType>
   </types>
  <fields>
    <field indexed="boolean" multiValued="boolean" name="unique_name" stored="boolean" type="type_name"/>
    <dynamicField indexed="boolean" multiValued="boolean" name="fieldname_wildcard_match" stored="boolean" type="type_name"/>
  </fields>
  <uniqueKey>partition_key</uniqueKey>
  <copyField source='field_name' dest='field_name' />
</schema>
CQL ALTER SEARCH INDEX SCHEMA basic syntax:
ALTER SEARCH INDEX SCHEMA ON keyspace_name.table_name 
ADD ([shortcut]| element_path) [ element_definition | WITH $$jsonsnippet$$];

Using shortcut keywords

Use shortcuts field, fieldType, and copyField to:
  • Add or drop table columns from the index, for example:
    ALTER SEARCH INDEX SCHEMA ON demo.health_data ADD field gender;

    If the field name matches a column name the field definition is automatically added to the pending schema.

  • Identify the element (field, fieldType, and copyField) and then change the setting using an element path or JSON definition:
    ALTER SEARCH INDEX SCHEMA ON wiki.solr ADD copyField[@source='title', @dest='catch_all'];

Using element paths

The element path uniquely describes the setting in the schema XML. Enclose attributes after an element in brackets; to define multiple attributes use a comma-separated list. When adding an element, include all of the attributes.
top_level_element_name.child_element_name[@attribute_name='value', ...]
For example to add a the Text field type definition:
types.fieldType[ @name='TextField_intl' , @class='org.apache.solr.schema.TextField' ]

The element path can also be used to describe a sub-element in the schema.

You can use element_path to change a sub-element in the schema. For example, to change the ASCIIFoldingFilterFactory in the search analyzer to a ClassicFilterFactory:
ALTER SEARCH INDEX SCHEMA ON demo.users
  SET types.fieldType[@name='TextField_intl']
           .analyzer[@type='search']
           .filter[@class='solr.ASCIIFoldingFilterFactory']@class=
           'solr.ClassicFilterFactory';
Changes the fieldType to:
<fieldType class="org.apache.solr.schema.TextField" name="TextField_intl">
  ...
  <analyzer type="search">
    <filter class="solr.LowerCaseFilterFactory"/>
    <filter class="solr.ClassicFilterFactory"/>
    <tokenizer class="solr.StandardTokenizerFactory"/>
  </analyzer>
</fieldType>

Defining complex elements with JSON

This JSON snippet is translated into XML elements and attributes:
  • JSON pair translates to XML attribute.
  • JSON object translates to XML element.
The JSON is translated into these XML attributes:
$${
    "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 JSON is translated into these XML elements:
"analyzer": [				
{ "type": "index", <analyzer type="index">
  "tokenizer":                         
  { "class": "solr.Standard..." }, <tokenizer class="solr.Standard..."/>
    "filter": [				  
    { "class": "solr.LowerCase..." }, <filter class="solr.LowerCase..."/>
    { "class": "solr.ASCII..." } <filter class="solr.ASCII..."/>

Removing elements or attributes

The CQL command syntax to remove the second filter on the search phase analyzer:
ALTER SEARCH INDEX SCHEMA ON demo.users
  DROP types.fieldType[@name='TextField_intl']
            .analyzer[@type='search']
            .filter[@class='solr.ClassicFilterFactory'];
Changes the fieldType to:
<fieldType class="org.apache.solr.schema.TextField" name="TextField_intl">
  ...
<analyzer type="search">
    <filter class="solr.LowerCaseFilterFactory"/>
    <tokenizer class="solr.StandardTokenizerFactory"/>
  </analyzer>
</fieldType>

Schema

Describes the CQL columns to index, sets the Solr data type, defines how to index and search each field type, and defines the primary key.

The schema displays in XML format. Use element paths to define and identify elements and attributes:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<schema name="autoSolrSchema" version="1.5">
  <types>
    <fieldType class="class_path" 
               name="fieldtype_name" >
      <analyzer>
        <tokenizer class="class_path"/>
        <filter class="class_path"/>
      </analyzer>
    </fieldType>
  </types>
  <fields>
    <field attribute_name="value"
          docValues="true|false" 
          indexed="true|false" 
          multiValued="true|false" 
          name="column_name" 
          stored="true|false" 
          type="fieldtype_name"/>
    
    <copyField 
               source="field_name" 
               dest="field_name" />
  </fields>
  <uniqueKey>pk_column_list</uniqueKey>
</schema>
ADD FIELD column_name
Adds a column from the CQL table to the pending search index schema using the default mapping.
ALTER SEARCH INDEX SCHEMA ON demo.health_data ADD fields.field fips;
Note: Adding the leading element fields. in ADD fields.field fieldname is optional and provides only cosmetic structure.
ADD fields.field[@attribute_name='value', ...]
Adds a new field to the pending schema and manually set the attributes. For example, to add a column from the table to the index and set the field type to string.
ALTER SEARCH INDEX SCHEMA ON demo.health_data 
ADD fields.field[@name='fish', @type='StrField', @indexed='true'];
ADD copyField[@attribute_name='value', ...]
Copy the value of the source field to a new field. For example, as a workaround to the rule that you cannot use LowerCaseStrField on a primary key column, you can use copyField to copy the primary key field data to a new field defined as type LowerCaseStrField.
ALTER SEARCH INDEX SCHEMA ON <table> ADD lowerCaseString key_column_copy;
ALTER SEARCH INDEX SCHEMA ON <table> ADD copyField[@source='key_column', @dest='key_column_copy'];
DROP field field_name
Removes a field from the pending search index schema.
ALTER SEARCH INDEX SCHEMA ON demo.health_data 
DROP field fips;
SET fields.field[@name='field_name']@attribute_name='value'
Changes the field identified by the attribute in brackets by adding or replacing the attribute_to_change.
Field attributes:
  • name: Matches a CQL table column name or the name of a copyField destination.
  • type: Name of a defined fieldType.
  • indexed: True indicates that the field is indexed. By default, only the fields that are included in the index on creation are displayed.
    Note: Primary key columns must be indexed (indexed="true").
  • docValues: Creates a forward index on the field values.
  • multiValued: Contains more than one value, such as a set, map, list column, or the destination of multiple copyField definitions.
Restriction: The stored field attribute is not supported.
ALTER SEARCH INDEX SCHEMA ON demo.health_data 
SET fields.field[@name='gender_s']@multiValued='true';
ADD types.fieldType[@attribute_name='value', …] WITH $$ { json_map } $$
Adds a field type definition to the schema for analyzing, tokenizing, and filtering fields in the index.
ADD types.fieldType[@name='TrieIntField', @class='solr.TrieIntField']
Note: Optionally add the leading element fields. in ADD and SET fields.field field_name to follow a naming convention and provide structure.