Syntax for changing schema settings
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. Example:
ALTER SEARCH INDEX SCHEMA ON <table> 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
, andcopyField
) and then change the setting using an element path or JSON definition. Example:
ALTER SEARCH INDEX SCHEMA ON <table> 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.
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>
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..."/>
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
fieldcolumn_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 field fips;
ADD
fields.field[@attribute_name='value', …
]-
Adds a new field to the pending schema and manually set the attributes. For example, 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']
;-
Adds a new field to the pending schema and manually set the attributes. For example, 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 usecopyField
to copy the primary key field data to a new field defined as typeLowerCaseStrField
.ALTER SEARCH INDEX SCHEMA ON <table> ADD field[@name='key_column_copy', @type='LowerCaseStrField', @indexed='true', @stored='true', @docValues='true'];
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.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.
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']
Optionally add the leading element
fields.
inADD
orSET
fields.field field_name
to follow a naming convention and provide structure.