Configuring the solr Suggester for Autocomplete


Solr suggester is used to improve the search capabilities and experience and provides users with automatic suggestions for query terms. In spellchecking, we got suggestions for query terms. But in the suggester, we will get powerful auto-suggest/autocomplete functionalities in your search.

In solr have several suggesters, but I am going to use BlendedInfixLookupFactory. It is an extension of the AnalyzingInfixSuggester. 

For Dictionary Implementation, I am going to use HighFrequencyDictionaryFactory.

Now let's see how to configure it:-


   1. First, we need to configure solrconfig.xml file: -         

         <searchComponent name="suggest" class="solr.SuggestComponent">

            <lst name="suggester">

                <str name="name">suggest</str>

               <str name="lookupImpl">BlendedInfixLookupFactory</str>

                <str name="dictionaryimpl">HighFrequencyDictionaryFactory</str>

                <str name="field">field_autocomplete</str>

                <str name="suggestAnalyzerFieldType">field_type</str>

                <str name="indexPath">suggest</str>

                <str name="buildOnStartup">true</str>

                <str name="buildOnOptimize">true</str>

                <str name="buildOnCommit">true</str>

        </lst>

     </searchComponent>


You have to put field  name(s) in "<str name="field">". If you want to add multiple fields, you can copy fields in one field and put the field name here.

And also you have to put the type in "<str name="suggestAnalyzerFieldType">" here.  


  2. Now, we need to add the request handler: -

       <requestHandler name="/suggest" class="solr.SearchHandler" startup="lazy">

            <lst name="defaults">

                  <str name="echoParams">explicit</str>

                  <str name="indent">false</str>

                  <str name="suggest">true</str>

                  <str name="suggest.count">10</str>

            </lst>

                <arr name="components">

                     <str>suggest</str>

                </arr>

  </requestHandler>

    

After adding, we need to restart the solr.

Now, I am going to give the query result as per my solr index.

http://127.0.0.1:8983/solr/your solr core name here/suggest?suggest=true&suggest.dictionary=suggest&wt=json&suggest.q=what is&suggest.build=true

Here query text is "what is". Lets see the response-

{"responseHeader": {"status": 0,"QTime": 1335,"params": {"suggest.build": "true","suggest.q": "what is","suggest": "true","suggest.dictionary": "suggest","wt": "json"}},"command": "build","suggest": {"suggest": {"what is": {"numFound": 1,"suggestions": [{"term": "<b>what</b> is reflection of light and lighter","weight": 10,"payload": ""}]}}}}


As per the index, I got this response. Response in 'suggestions' tag and autocomplete term in the 'term' tag. 

One thing in the query you don't have to use (suggest.build=true) to build the index on every query-instead you should use buildOnCommit or buildOnOptimize if you have regularly changing documents.