Enabling the Suggester from SOLR for the Search box

We recently used SOLR suggester to implement autocomplete feature for our Site search.

There are 3 steps involved to implement the Suggester:
1. Add the Search component in solrconfig.xml file.
2. Add the new field in Managed-Schema file.
3. Call the Suggester from your Controller method.

  1. Add the Search component in solrconfig.xml file
    a. Go to the solrconfig.xml file [\server\solr\sitecore_web_index\conf\solrconfig.xml] under the SOLR core you want to implement (ideally Web).
    b. Add the Search Component and Request Handler at the end before </config> tag:
        <searchComponent name="suggest" class="solr.SuggestComponent">
	 <lst name="suggester">
	   <str name="name">mySuggester</str>
	   <str name="lookupImpl">AnalyzingInfixLookupFactory</str>
	   <str name="dictionaryImpl">DocumentDictionaryFactory</str>
	   <str name="field">autocomplete__txtsug</str>
	   <str name="contextField">_template</str>
	   <str name="suggestAnalyzerFieldType">string</str>
	   <str name="buildOnStartup">true</str>
	 </lst>
	 <lst name="suggester">
	   <str name="name">mySuggester1</str>
	   <str name="lookupImpl">AnalyzingInfixLookupFactory</str>
	   <str name="dictionaryImpl">DocumentDictionaryFactory</str>
	   <str name="field">autocomplete__txtsug</str>
	   <str name="contextField">_template</str>
	   <str name="suggestAnalyzerFieldType">text_en</str>
	   <str name="buildOnStartup">true</str>
	 </lst>
	</searchComponent>

	<requestHandler name="/suggest" class="solr.SearchHandler" startup="lazy">
	 <lst name="defaults">
	   <str name="spellcheck">true</str>
	   <str name="suggest">true</str>
	   <str name="suggest.dictionary">mySuggester</str>
	   <str name="suggest.dictionary">mySuggester1</str>
	   <str name="spellcheck.count">10</str>
	   <str name="spellcheck.collate">true</str>
	 </lst>
	 <arr name="components">
	   <str>suggest</str>
	 </arr>
	</requestHandler>

2. Add the field type to Managed Schema
a. Add the field type to Managed Schema file [\server\solr\sitecore_web_index\conf\managed-schema], search for </fieldtype> and paste the below code after last fieldtype in the Managed-schema

       <fieldType class="solr.TextField" name="textSuggest" positionIncrementGap="100">
        <analyzer>
          <tokenizer class="solr.StandardTokenizerFactory"/>
          <filter class="solr.LowerCaseFilterFactory"/>
          </analyzer>
       </fieldType>

b. Search for the “dynamicfield” and paste below code as the first dynamic field.

       <dynamicField name="*_txtsug" type="textSuggest" indexed="true" stored="true"/>

c. Paste below line of code before </schema> tag at the end of the file.

       <copyField source="title_t" dest="autocomplete__txtsug"/>

d. Restart the SOLR service and you are almost done 🙂

3. Add a GetSuggestion method to the Search Controller (Assuming your JS code for autocomplete accepts JSON content)

        [HttpGet]
        public ActionResult GetSuggestion(string query)
        {
            var result = SearchService.Suggestions(query.ToLower());

            Suggestions model = new Suggestions();
            model.suggestionsList = new List<suggest>();

            foreach (string _suggest in result.ToList())
            {
                if (!string.IsNullOrEmpty(_suggest))
                {
                    string suggestValue = _suggest;
                    suggest item = new suggest();
                    item.value = suggestValue;
                    item.data = suggestValue;
                    model.suggestionsList.Add(item);
                }
            }
            return Json(new { suggestions = model.suggestionsList }, JsonRequestBehavior.AllowGet);
        }

You will need to add a route so that the front end code can call this method using the AJAX call.

The best blog to understand the concepts of the SOLR Suggester in more detail and which helped me a lot is here.

Hope you learnt something new!

Thank you.. Keep Learning.. Keep Sitecoring.. 🙂

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 )

Connecting to %s