Configuring the solr spell checker

Spell Checking is using for correcting the misspelled word when a searcher searches misspelled words. When we search misspelled words in google, google showed  "Did you mean?" after search. Exactly the same thing we can configure by using the spell checker. Now let's see how to configure it-


1.  First, we need to configure the solrconfig.xml file in solr.


     <searchComponent name="spellcheck" class="solr.SpellCheckComponent">

                <str name="queryAnalyzerFieldType">type_spellcheck</str

       <lst name="spellchecker">

               <str name="name">default</str>

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

              <str name="classname">solr.DirectSolrSpellChecker</str>

              <str name="distanceMeasure">internal</str>

              <float name="accuracy">0.5</float>

             <int name="maxEdits">2</int>

             <int name="minPrefix">1</int>

             <int name="maxInspections">5</int>

             <int name="minQueryLength">4</int>

             <float name="maxQueryFrequency">0.01</float>

      </lst>

  </searchComponent>     


You have to add "queryAnalyzerFieldType" and field (to build a spell checker result on this field).

Also, queryAnalyzerFieldType should have "StandardTokenizerFactory".


2.  Now, we need to add a request handler.


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

    <lst name="defaults">

      <str name="spellcheck.dictionary">default</str>

      <str name="spellcheck">on</str>

      <str name="spellcheck.extendedResults">true</str>

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

      <str name="spellcheck.alternativeTermCount">5</str>

      <str name="spellcheck.maxResultsForSuggest">5</str>

      <str name="spellcheck.collate">true</str>

      <str name="spellcheck.collateExtendedResults">true</str>

      <str name="spellcheck.maxCollationTries">10</str>

      <str name="spellcheck.maxCollations">5</str>

    </lst>

    <arr name="last-components">

      <str>spellcheck</str>

    </arr>

  </requestHandler>


After adding we need to restart our solr and start using it.

Querying for the string is: "speed light", but I am going to send misspelled string: "sped ligh".

http://127.0.0.1:8983/solr/Your solr core name /spell?spellcheck.q=sped ligh&spellcheck=true

Now see the response -

{"responseHeader": {"status": 0,"QTime": 7},"response": {"numFound": 0,"start": 0,"numFoundExact": true,"docs": []},"spellcheck": {"suggestions": ["sped",{"numFound": 1,"startOffset": 0,"endOffset": 4,"origFreq": 0,"suggestion": [{"word": "speed","freq": 2}]},"ligh",{"numFound": 1,"startOffset": 5,"endOffset": 9,"origFreq": 0,"suggestion": [{"word": "light","freq": 4}]}],"correctlySpelled": false,"collations": ["collation",{"collationQuery": "speed light","hits": 4,"misspellingsAndCorrections": ["sped","speed","ligh","light"]}]}}

You can see the "collationQuery" gives the correct word. And also you can get suggestions.

You can use the collationQuery result in your "Did you mean?" after search.