Faceted Autocomplete for a Sitecore Multilingual site using SOLR Suggester API

Posted 01/27/2017 by Subramanian Ramanathan

There are lot of options for SOLR Autocomplete and the popular ones are NGrams, Terms Component and Suggester. Suggester is the most recent, fastest and most recommended one. 

Though faceting/filtering of results based on template or any other field may seem to be difficult with Suggester, it is possible with a minor tweak!!

Create Field for Building Suggestions

The field from which suggestions have to be built can be a Sitecore field or a custom computed field. In my case it involved few Sitecore fields like Title, Subtitle, Description etc.. So I created a copy field with these as below.

<field name="_suggest" type="text_general" indexed="true" stored="true"/>
<copyfield source="title_t" dest="_suggest"/>
<copyfield source="subtitle_t" dest="_suggest"/>
<copyfield source="description_t" dest="_suggest"/>

Creating Field Type for Suggester

This fieldtype will be used by suggester for analyzing and building the suggestions from our field (_suggest). We can either use existing field types like text_general etc. or create a fieldtype like the one below (in the schema.xml file) in case any customization may later be required.

        <tokenizer class="solr.WhitespaceTokenizerFactory"/>
        <filter class="solr.LowerCaseFilterFactory"/>
        <filter class="solr.RemoveDuplicatesTokenFilterFactory"/>
        <charfilter class="solr.PatternReplaceCharFilterFactory" pattern="[^a-zA-Z0-9]" replacement=" "/>

Creating The Suggest Component:

Though we have many Lookup Implementations for creating Suggest Component, but Context Filtering and Payload Fields are supported only by AnalyzingInfixLookupFactory and BlendedInfixLookupFactory.

Out of these, AnalyzingInfixLookupFactory doesn’t remove duplicate suggestion values. So I preferred BlendedInfixLookupFactory, which returns only unique values though the search term is present in multiple indexed documents.

Since we need to get the suggestions only for the current language (by Context Filtering) we should have contextField set as _language (this SOLR field of indexed document corresponds to Sitecore Item’s Language).

SOLR has come up with a Payload field for purposes like boosting, tagging etc. but it can be used for other purposes too. Here we can use it for faceting results!! In my case I wished to group results based on a sitecore field, Facet Category.


Creating Request Handler:

We need to create a request handler pointing to our suggest component, that will help in building and retrieving suggestions.


Once we are done with this, we can query and check for results in xml format,

Building jQuery Autocomplete:

We can use getJSON API to retrieve the suggestions in json format and display after adding querystring ‘wt=json’.
An Example below,

<input id="search-box" type="text" />
<script type="text/javascript">
	source: function (request, response) {
		var currentLanguage = 'en';
		var url = "http://localhost:8983/solr/sitecore_master_index/suggest?suggest.q=" + request.term + "&suggest.cfq=" + currentLanguage + "&wt=json";
		$.getJSON(url, function(data){
			response($.map(data.suggest.suggester[request.term].suggestions, function(value) { return value.term }));				

In case if you face any Cross Domain issues, you need to add the filter component as mentioned in the below url to your web.xml Don’t forget to change param-value if your SOLR is hosted in a different server.

SOLR Suggestions

Happy Searching!!



Add your comment




  • ravi teja said:
    1/3/2018 1:06 AM

    This is the nice article on <a href="" >sitecore </a> please keep Sharing

  • Priancy Gahlot said:
    7/4/2018 5:04 AM

    Hi, I need to add my own dictionary in Solr and on the basis on some rules, I need to present results for autosuggest.Please let me know when can we discuss this. Thanks in advance!