SOLR Icon

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.


    
        suggester
        BlendedInfixLookupFactory
        DocumentDictionaryFactory
        _suggest
		_language
		facet_category_t
        text_suggester
        false
        false
    


Creating Request Handler:

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


    
        true
        suggester
        true
        10
        true
    
    
        suggest
    

Once we are done with this, we can query and check for results in xml format,
http://localhost:8983/solr/sitecore_web_index/suggest?suggest.q=apple&suggest.cfq=fr-CA


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">
$('#search-box').autocomplete({
	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 }));				
		});
	}
});
</script>

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 http://marianoguerra.org/posts/enable-cors-in-apache-solr.html Don’t forget to change param-value if your SOLR is hosted in a different server.

SOLR Suggestions

Happy Searching!!

References:
https://cwiki.apache.org/confluence/display/solr/Suggester
https://blog.horizontalintegration.com/2015/07/20/configuring-solr-to-provide-search-suggestions/

Share:

Add your comment

 
 

 

Comments

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

    This is the nice article on <a href="http://www.itorigins.com/OnlineTraining/Sitecore.aspx" >sitecore </a> please keep Sharing

Archive

Syndication