Solr schema file (schema.xml)

Solr organizes its data into documents, which consist of fields. In WebSphere Commerce, one document consists of all the descriptions of a product. The schema.xml file contains information about the Solr fields and how they are analyzed and filtered during searches. Different field types can contain different types of data. Solr uses the schema.xml file to determine how to build indexes from the input documents, and how to perform index and query time processing.

The schema.xml file contains the following fields and options:


Field type

Field types define a list of different data types for values. We can define strings, numeric types, or new types. For example:

A more complicated field definition resembles the following example:

Where the wc_text fieldType is processed by the analyzers, tokenizers, and filters, and are used to influence search relevancy.


Text analyzers

Text analyzers map the source string of text and the final list of tokens. This process occurs during indexing and querying. We can have different analyzer chains for indexing and querying, depending on your business needs. For example, the EdgeNGramFilterFactory is better suited for indexing, rather than querying section. However, typically the same analyzer chains are used for indexing and queries, as searches require the query tokens to match the indexed tokens.

Different analyzers return different results. For example, we can use some analyzers to return the term Flash, when flash is used.


Tokenizers

Tokenizers break field data into tokens. The following example shows how an input of O'Reilly's wi-fi guide is tokenized:

Standard tokenizer (standardTokenizerFactory) O Reilly s wi fi guide Keyword tokenizer O'Reilly's wi-fi guide Whitespace tokenizer O'Reilly's wi-fi guide WordDelimiterFilterFactory o Reilly s oReillys Wi Fi WiFi guide
Tokenizer Output

By using the WordDelimiterFilterFactory, we can make searches for sailboat match searches for sail boat or sail-boat.


Filters

Filters are used after tokenizers to examine a stream of tokens and either keep them as-is, transform or discard them, or create new ones. Tokenizers and filters can be combined to form pipelines, or chains, where the output of one becomes the input for the next. A sequence of tokenizers and filters is called an analyzer, and the resulting output of an analyzer is used to match query results or build indexes.

The snowballPorterFilterFactory is used for stemming, where stemming is used to reduce a word to a shorter form. For example, increased, increasing, and increases all stem to the word increase.

Solr uses Porter and KStem by default when stemming. Porter stems the word to a base form that does not necessarily have to be a dictionary word. In contrast, KStem stems the word to a base form that matches a dictionary word. Typically, Porter results in more matches with less precision.

For example, Porter can identify territorial when searching for territory (KStem does not), but it also identifies visuals when searching for visualization (again, where KStem does not).

The SnowballPorterFilterFactory is the Snowball Porter Stemming algorithm that will be applied to each word (token). It is the implementation of the Porter2 (snowball) stemming algorithm. The Porter2 algorithm is a slight improvement over the Porter algorithm.

The most common stemming algorithms being used are Porter, Snowball (Porter2), and Lancaster, where Porter is the least aggressive, and Lancaster the most (some shorter words will become totally obfuscated after Lancaster). Porter is the slowest and Lancaster the fastest. For more information about the snowball algorithm, see The English (Porter2) stemming algorithm. Porter and Porter2 stem approximately 5% of words to different forms.

Output from snowballPorterFilterFactory: o Reilli s oReilli Wi Fi WiFi guid


Field

Every field must declare a unique name and associate it with one of the previously-defined types. For example:

Every field can define three important attributes:

For example, in the schema.xml file, the categoryname is set to hold multiple values:

Then, in the wc-data-config.xml file:


Copy fields

Copy fields are used when the content of a source field needs to be added and indexed on different destination fields. The following example copies the name field to the name_suggest destination field:


Dynamic fields

Dynamic fields allow you to index data without defining the name of the field. Instead, the name of the field is defined by a wildcard (*). We can use prefixes and suffixes so that the actual name of the field is accepted at runtime. However, the field type must be defined. For example, the following dynamic field accepts names such as price_USD= "100" or price_EUR= "120".

This sample is ideal, as we can work with the Solr API without defining a final schema for the data.


Unique key

Unique keys assign a unique identity to a specific Solr document, similar to primary keys in databases. In the CategoryEntry index's schema.xml file, the uniqueKey is defined as catentry_id.


Default search field

This field is used when the request does not contain a specific field.


Default operator

The default operator indicates the default behavior when handling multiple tokens in the search. The OR operator is defined by default and cannot be changed. The following naming conventions are used in the schema.xml file:

Use non-tokenized fields when we want to search for exact matches. For example, use non-tokenized field for brand names, so that when a shopper searches for Econo Sense, products from Econo Sense appear in search results, instead of products from Sense.

Use case sensitive searches for more accurate matches. For example, for fields such as part number.


Related tasks
Extending the schema.xml file using the x-schema.xml file