Sometimes there are situations where you would like to have some kind of functionality that would allow you to give your user the search results even though he/she made a typo or even multiple typos. In Solr, there are multiple ways to undo that: using a spellchecker component to try and correct the user's mistake, using the fuzzy query, or for example, using the ngram approach. This recipe will concentrate on the third approach and show you how to use ngrams to handle user typos.
For the purpose of the recipe, let's assume that our index is built up of four fields: identifier
, name
, description
, and the description_ngram
field which will be processed with the ngram filter.
So let's start with the fields definition of our index which should look like the following code (place this in your
schema.xml
file in thefields
section):<field name="id" type="string" indexed="true" stored="true" required="true" multiValued="false" /> <...