✨ Enable Spellcheck in Solr (Because Spelling Is Hard)
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!
📝 Step 1: Schema Configuration
- Edit your
schema.xml(in your Solr core'sconfdirectory): - Define a field type for spellcheck:
<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>
- Define your content field and a spellcheck field:
<field name="content" type="textSpell" indexed="true" stored="true"/> <field name="spell" type="textSpell" indexed="true" stored="false" multiValued="true"/>
⚙️ Step 2: Solr Configuration
- Edit your
solrconfig.xml(in your Solr core'sconfdirectory). - Find the
<requestHandler>for/selectand add the spellcheck component:
<requestHandler name="/select" class="solr.SearchHandler"> <!-- ... --> <arr name="last-components"> <str>spellcheck</str> </arr> </requestHandler>
🔮 Step 3: Spellcheck Component Configuration
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!
♻️ Step 4: Reindex Your Data
After any schema/config changes, reindex your content.
Otherwise, your spellcheck dictionary will be lonely and unhelpful.
🧑💻 Step 5: Querying with Spellcheck
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. 🎉
💡 Bonus Tips
- Spellcheck is a game-changer for user experience—but don’t overdo it. Too many suggestions can be distracting.
- Always test with real-world typo examples (we all have a user who types "bannana" instead of "banana").
- Tweak your spellchecker for speed vs. accuracy—there’s always a balance.
Now your Solr is smart enough to fix “teh” into “the.” Happy searching! 🪄

