Enable Spellcheck in Solr

Spellcheck

Spellcheck gives your visitors a "did you mean" suggestion when they mistype a word. Solr builds the suggestions from the words already in your index, so it takes one field type, one field and one search component.

01 · In schema.xml

Open the index, click Configuration and edit schema.xml (edit a file). Add a field type for the spellcheck field:

<fieldType name="textSpell" class="solr.TextField" positionIncrementGap="100">
   <analyzer type="index">
       <tokenizer class="solr.StandardTokenizerFactory"/>
       <filter class="solr.LowerCaseFilterFactory"/>
   </analyzer>
   <analyzer type="query">
       <tokenizer class="solr.StandardTokenizerFactory"/>
       <filter class="solr.LowerCaseFilterFactory"/>
   </analyzer>
</fieldType>

Add the field that holds the words, and copy your text into it:

<field name="spell" type="textSpell" indexed="true" stored="false" multiValued="true"/>
<copyField source="content" dest="spell"/>

02 · In solrconfig.xml

Define the spellcheck component:

<searchComponent name="spellcheck" class="solr.SpellCheckComponent">
   <lst name="spellchecker">
       <str name="name">default</str>
       <str name="field">spell</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">3</int>
       <float name="maxQueryFrequency">0.5</float>
   </lst>
</searchComponent>

Then attach it to the /select handler:

<requestHandler name="/select" class="solr.SearchHandler">
   <!-- ... -->
   <arr name="last-components">
       <str>spellcheck</str>
   </arr>
</requestHandler>

Saving each file reloads the index. maxEdits (1 or 2) decides how far a suggestion may be from what was typed, accuracy how close it must be, and minQueryLength the shortest word that gets suggestions.

03 · Fill the field and ask for suggestions

The spell field is filled when documents are indexed, so send your data again after the change. Then add spellcheck=true to a query:

/select?q=your_query&spellcheck=true

The suggestions arrive in the spellcheck section of the response.