Wednesday, March 2, 2016

Sitecore Lucene facet phrases or sentences with spaces

I came across an interesting error with an implementation of Sitecore search using Lucene. I had a facet based on a phrase (page category) that contained spaces. When the facets came back on search code they were split up into individual words - "New" and "Zealand" instead of "New Zealand".

I spent a bit of time searching for a resolution for this one, however most results appeared to work with SOLR (where an untokenized field simply fixed the issue). Then I came across this post on the Sitecore blog.

The fix is to define your computed index field as untokenized (which I had already done), but then to also add that same field under the standard fieldMap > fieldNames element - with the LowerCaseKeywordAnalyzer.
<fieldMap type="Sitecore.ContentSearch.FieldMap, Sitecore.ContentSearch">
<fieldNames hint="raw:AddFieldByFieldName">
  <field fieldName="categoryfacet" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" type="System.String" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider">
    <Analyzer type="Sitecore.ContentSearch.LuceneProvider.Analyzers.LowerCaseKeywordAnalyzer, Sitecore.ContentSearch.LuceneProvider" />
    </field>
</fieldNames>
</fieldMap>

<fields hint="raw:AddComputedIndexField">
<field fieldName="categoryfacet" storageType="yes" indexType="untokenized"
        patch:after="field[last()]">MyProject.IndexCategory, MyProject
</field>
</fields>
That fix worked and my facets were coming back as single phrases. Of course if you are faceting and searching on the same field, you should probably have a separate version that does not use this fix (so that searching on the individual words works as expected).

No comments:

Post a Comment