Enabling spellcheck in Apache Solr is like giving your users a helpful nudge whenever they make a typo—because we all know “seach” is not “search.”
Here’s how to get those “Did you mean…?” suggestions working for your queries!
schema.xml
(in your Solr core’s conf
directory):<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>
<field name="content" type="textSpell" indexed="true" stored="true"/>
<field name="spell" type="textSpell" indexed="true" stored="false" multiValued="true"/>
solrconfig.xml
(in your Solr core’s conf
directory).<requestHandler>
for /select
and add the spellcheck component:<requestHandler name="/select" class="solr.SearchHandler">
<!-- ... -->
<arr name="last-components">
<str>spellcheck</str>
</arr>
</requestHandler>
Still in solrconfig.xml
, define your <searchComponent>
for spellcheck:
<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>
Pro tip: You can tune these parameters based on your data and performance needs. For instance, more “maxEdits” means more generous suggestions, but potentially more noise!
After any schema/config changes, reindex your content.
Otherwise, your spellcheck dictionary will be lonely and unhelpful.
When making a search query, simply add the spellcheck
parameter:
/select?q=your_query&spellcheck=true
You’ll get spellcheck suggestions in your Solr response, usually under the "spellcheck"
section.
Voilà! No more missed searches due to typos. 🎉
Now your Solr is smart enough to fix “teh” into “the.” Happy searching! 🪄