Custom Solr fields

In this lesson, you add your own Solr field type to the x-schema-field-types.xml configuration file. The new field will remove dashed concatenations from search suggestions. For instance, instead of suggesting "jewel-toned" in a search for "jewel" it will suggest "jewel toned."

If you want to add customized Solr field types, you can define them in the workspace_dir\search-config-ext\src\index\managed-solr\config\v3\common\ x-schema-field-types.xml file.

To use a custom field type, modify the field and field type mapping in the search-config-ext\src\index\managed-solr\config\v3\indextype\x-schema.xml file, where indextype is: CatalogEntry, CatalogGroup, Price, or Unstructured..

Following this procedure, you will add a new field type, and use this type in your catalogEntry index. You will add a word delimiter filter into this new field type.

Procedure

  1. Search for the string "jewel" in the storefront. The Solr search engine will suggest product names concatenated by dashes, as in the following result:


  2. Copy the x-schema-field-types.txt file from the sample solrconfiguration.zip to the workspace_dir\search-config-ext\src\index\managed-solr\config\v3\common\x-schema-field-type.xml.
  3. Add the following text to the x-schema-field-type.xml file.
    
         <fieldType name="x_textSpell_en" class="solr.TextField" positionIncrementGap="100" omitNorms="true">
              <analyzer type="index">
                <tokenizer class="solr.WhitespaceTokenizerFactory"/>
                <filter class="solr.LowerCaseFilterFactory"/>
                <filter class="solr.StopFilterFactory" ignoreCase="true" words="${stopwords_en:../../../v3/common/stopwords.txt}"/>
                <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1" generateNumberParts="1" catenateWords="0" 
                               catenateNumbers="0" catenateAll="0" splitOnCaseChange="1" splitOnNumerics="1" preserveOriginal="0"/>
    
                <filter class="solr.ShingleFilterFactory" outputUnigrams="true" minShingleSize="2" maxShingleSize="3" tokenSeparator=" " fillerToken="" />
                <filter class="solr.PatternReplaceFilterFactory" pattern="\s{2,}" replacement=" " replace="all"/>
                <filter class="solr.TrimFilterFactory"/>
                <filter class="solr.RemoveDuplicatesTokenFilterFactory"/>
              </analyzer>
              <analyzer type="query">
                <tokenizer class="solr.WhitespaceTokenizerFactory"/>
                <filter class="solr.LowerCaseFilterFactory"/>
                <filter class="solr.WordDelimiterFilterFactory" generateWordParts="1" generateNumberParts="1" catenateWords="0" catenateNumbers="0" 
                               catenateAll="0" splitOnCaseChange="1" splitOnNumerics="1" preserveOriginal="0"/>
                <filter class="solr.StopFilterFactory" ignoreCase="true" words="${stopwords_en:../../../v3/common/stopwords.txt}"/>
              </analyzer>
            </fieldType>
    The classes defined in bold delete the dash character ‘-’ from search suggestions. For example, instead of returning ‘a-b’, the output can be ‘a b’.
  4. Open the workspace_dir\search-config-ext\src\index\managed-solr\config\v3\CatalogEntry\x-schema.xml file. Add a spellcheck attribute to the field element as follows.
    Locate the field element.
    <field name="spellCheck" type="wc_textSpell_${lang:en}" indexed="true" stored="false" multiValued="true" /> 
    Change it to read:
    <field name="spellCheck" type="x_textSpell_en" indexed="true" stored="false" multiValued="true" />
    Locate the dynamicField element:
    <dynamicField name="spellCheck*" type="wc_textSpell_${lang:en}" indexed="true" stored="false" multiValued="true" />
    Change it to read:
    <dynamicField name="spellCheck*" type="x_textSpell_en" indexed="true" stored="false" multiValued="true" />
  5. Restart the HCL Commerce Search server.
  6. Rebuild the search index.
    https://transaction_server_hostname:transaction_server_https_port/wcs/resources/admin/index/dataImport/build
    The method is POST, and you must include basic authorization in the header, i.e. a spiuser ID and corresponding password. For more information, see Building the HCL Commerce Search index.

Results

In the storefront, search for the string "jewel" as in Step 1. Your suggestions will be returned without hyphenation, as below.