How to Create a Field for Faceting or Sorting in Site Search for Sitecore


In this guide you will learn how to create a field to facet or sort on in the SearchStax Site Search solution.

You may have already tried to create a facet or sort in Site Search, but didn’t get the results you were expecting. This is because fields of type text_general are tokenized. The data in them are broken up into tokens making any Faceting/Sorting behave unpredictably.

As an example, if you want to facet on the field city_t, you would expect the value “Los Angeles” to appear as a facet value. However, since city_t is a text_general type, “Los Angeles” actually becomes “Los” and “Angeles”. Your facet now has values for “Los” and “Angeles” which is not the expectation.

In this example, the solution is to change city_t to a “string type” field. String types are not tokenized. The data remains as in. So, the facet value would be “Los Angeles” as expected.

Now, let’s update your text_general field to a string type in Sitecore.

Change Field Type in Sitecore

  1. Begin by creating a new XML file named Patch.config in the app_config/include directory. Paste the provided XML configuration into this file. The configuration will modify the return types of specific field types in Sitecore. This is how the configuration looks:
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:search="http://www.sitecore.net/xmlconfig/search/">
    <sitecore>
        <contentSearch search:require="solr">
            <indexConfigurations>
                <defaultSolrIndexConfiguration type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration, Sitecore.ContentSearch.SolrProvider">
                    <fieldMap type="Sitecore.ContentSearch.SolrProvider.SolrFieldMap, Sitecore.ContentSearch.SolrProvider">
                        <fieldTypes hint="raw:AddFieldByFieldTypeName">
                            <fieldType fieldTypeName="html|rich text|single-line text|multi-line text|text|memo|image|reference" returnType="text">
                                <patch:attribute name="fieldTypeName">html|rich text|multi-line text|text|memo|image</patch:attribute>
                            </fieldType>
                            <fieldType fieldTypeName="single-line text|reference" returnType="string"/>
                        </fieldTypes>
                    </fieldMap>
                </defaultSolrIndexConfiguration>
            </indexConfigurations>
        </contentSearch>
    </sitecore>
</configuration>

Update the Schema

  1. In the Sitecore desktop interface, select the Sitecore button at the bottom left (or the Sitecore logo at the top left if you’re using the newer interface).
  2. Select Control Panel.
  3. In the Control Panel, under the Indexing section, select Populate Solr Managed Schema.

    Reindex the Data

    1. In the Sitecore Content Editor, go to the Control Panel.
    2. Under the Indexing section, select Index Manager.
    3. In the Index Manager screen, select the relevant indexes and click on Rebuild.
      1. Login to the Site Search dashboard at searchstudio.searchstax.com.
      2. Navigate to Settings > Search API. At the bottom of the page click Reload Schema.

        Questions?

        Do not hesitate to contact the SearchStax Support Desk.