Indexing float, double and decimal values – Sitecore 7 Content Search

Reason

Sitecore Content Search stores Sitecore Number field values as integers by default, e.g. 12.50 will be indexed as 12. Luckily it’s very easy to reconfigure this behavior as the required indexing mechanisms are already present in the Sitecore Content Search API. The indexing behavior can be changed for the Sitecore Number field type as a whole, rather than having to be changed for each individual field in the solution.

Examples are based on Sitecore CMS 7.1 Update-1 (rev. 140130).

Code

Copy the following configuration into a .config-file and place it in a subfolder of “/App_Config/Include”, e.g. “/App_Config/Include/MyCompany/ContentSearch.NumberField.config”. Doing so will map a field reader of type PrecisionNumericFieldReader to all Sitecore fields of type Number:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <contentSearch>
      <configuration>
        <defaultIndexConfiguration>
          <fieldReaders>
            <mapFieldByTypeName>
              <fieldReader fieldTypeName="number" fieldNameFormat="{0}" fieldReaderType="Sitecore.ContentSearch.FieldReaders.PrecisionNumericFieldReader, Sitecore.ContentSearch" />
            </mapFieldByTypeName>
          </fieldReaders>
        </defaultIndexConfiguration>
      </configuration>
    </contentSearch>
  </sitecore>
</configuration>

Example

Below is a screenshot taken from Luke showing indexed Number field values, incl. the fractional part. Note that the values are shown in Danish culture which means commas are displayed as the decimal mark:

Sitecore Number field value indexed using the PrecisionNumericFieldReader example

One thought on “Indexing float, double and decimal values – Sitecore 7 Content Search

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s