How to create in Lucene DID YOU MEAN feature like google?

Ishan Upamanyu | Apr 3, 2022 min read

In this blog post, we will create in Lucene Did you mean feature as seen in Google. Let’s begin by understanding what this feature is.

Whenever you misspell a word in google, google is able to figure out that you made a mistake. If the mistake is not significant, then google simply shows results for what it thinks is the correct spelling. It also gives you the option to search instead for the original query you entered.

But if it feels there is a significant difference in the spelling of the query and what it thinks the right query is, it will ask you what you meant by using the “Did you mean” feature as shown below:


Lucene did you mean feature java example

If you are creating a search engine using Apache Lucene, you can create a similar feature.

We can create in Lucene DID YOU MEAN feature by using the Hunspell dictionaries. These come prebuilt in Lucene in the analyzer common library.

To create a Did you mean feature in Lucene we need to have the following capabilities:

  1. We need to know when a word is not spelled correctly. This will help us show the did you mean prompt to the user only when we need to.
  2. Once we know the word is misspelled, then we need the ability to show the correct spelling.

We can achieve both these things using Lucene’s Hunspell class. Here is a working example of how to use the Hunspell class.


We need Lucene’s core and analyzers-common library.

In case you are using MAVEN, copy the following dependencies in pom.xml

 2   <groupId>org.apache.lucene</groupId>
 3   <artifactId>lucene-core</artifactId>
 4   <version>8.10.1</version>
 8   <groupId>org.apache.lucene</groupId>
 9   <artifactId>lucene-analyzers-common</artifactId>
10   <version>8.10.1</version>

Hunspell Dictionary files

For this feature to work you need the following two libraries:

  1. Affix file: This can be found here.
  2. The dictionary file: Can be found here.

You can get affix and dictionary files for other languages here.

JAVA code

Here is the java code. It does two things:

  1. Check the spelling of words and
  2. Get some suggestions for words.
 1package upmanyu.ishan.lucene.tutorial;
 3import org.apache.lucene.analysis.hunspell.Dictionary;
 4import org.apache.lucene.analysis.hunspell.Hunspell;
10import java.nio.file.Files;
11import java.text.ParseException;
13public class DidYouMean {
14    public static void main(String[] args) throws IOException, ParseException {
15        Directory directory ="temp"));
16        InputStream affFileStream = new FileInputStream("src/main/resources/hunspell/en-US/en_US.aff");
18        InputStream dicFileStream = new FileInputStream("src/main/resources/hunspell/en-US/en_US.dic");
19        Dictionary dictionary = new Dictionary(directory, "spellCheck", affFileStream, dicFileStream);
21        Hunspell spellChecker = new Hunspell(dictionary);
23        String correctWord = "guava";
24        String misspelledWord = "recieve";
26        System.out.println(String.format("Is %s spelled correctly?: %b", correctWord, spellChecker.spell(correctWord)));
27        System.out.println(String.format("Is %s spelled correctly?: %b", misspelledWord, spellChecker.spell(misspelledWord)));
28        System.out.println(String.format("Did you mean: %s", spellChecker.suggest(misspelledWord)));
29    }


lucene did you mean example output

Now that you have seen the java code in action, let us try and understand how this code works. The core part of this code is the Hunspell class in Lucene. According to Wikipedia:

Hunspell is a spell checker and morphological analyser designed for languages with rich morphology and complex word compounding and character encoding, originally designed for the Hungarian language.

Hunspell is used by LibreOffice office suite, free browsers, like Mozilla Firefox and Google Chrome, and other tools and OSes, like Linux distributions and macOS. It is also a command-line tool for Linux, Unix-like, and other OSes.

How do Hunspell dictionaries work?

A Hunspell dictionary needs two files to work:

  1. Affix File: An affix file (*.aff) contains a list of suffixes and prefixes and rules describing them. The rules describe where we can use a suffix or prefix. For example, you may have a rule which says the suffix ied can be applied only if the last character of the word is y, in other cases suffix ed should be used.
  2. Dictionary file: A dictionary file (*.dic) contains a list of words one per line. The first line of the dictionaries (except personal dictionaries) contains the approximate word count (for optimal hash memory size). Each word may optionally be followed by a slash (“/”) and one or more flags, which represents the word attributes, for example, affixes.

Using these two files, the algorithm tries to find if the word can be formed with the given affix and dictionary file rules.

As stated in Linux man-pages:

Consider the Dictionary file:


The flags B and A specify the attributes of these words.

and the Affix file:


REP f ph
REP ph f

PFX A 0 re .

SFX B 0 ed [^y]
SFX B y ied y

In the affix file, prefix A and suffix B have been defined. Flag A defines a re-‘ prefix. Class B defines two -ed’ suffixes. First B suffix can be added to a word if the last character of the word isn’t y’. The second suffix can be added to the words terminated with an y’.

All accepted words with this dictionary and affix combination are: “hello”, “try”, “tried”, “work”, “worked”, “rework”, “reworked”.


In this blog post, we understood what did you mean feature in google is. Then we saw some code to create in Lucene Did you mean feature by using Hunspell dictionary. We ended this post by learning how Hunspell dictionaries work.

What Next?

If you enjoyed this article I am guessing you might be using Lucene to create a search application. I also created a search engine to search on English Wikipedia Articles. Do you want to know how I did it? Read all about it here.