fuzzy search with lucene

full-text-search, fuzzy-search, lucene

Solution

large edit distances are no longer supported by `FuzzyQuery` in Lucene 4.x. The current implementation of `FuzzyQuery` is a huge improvement on performance from the Lucene 3.x implementation, but only supports two edits. Distances greater than 2 Damerau–Levenshtein edits are considered to rarely be really useful.

According to the `FuzzyQuery` documentation, if you really must have higher edit distances:

If you really want this, consider using an n-gram indexing technique (such as the SpellChecker in the suggest module) instead.

The strong implication is that you should rethink what your trying to accomplish, and find a more useful approach.

Problem

I implemented a fuzzy search with lucene 4.3.1 but i'm not satisfied with the result. I would like to specify a number of results it should return. So for example if I want 10 results, it should return the 10 best matches, no matter how bad they are. Most of the time it returns nothing if the word I search for is very different from anything in the index. How can I achieve more/fuzzier results? Here the code I have: ``` public String[] luceneQuery(String query, int numberOfHits, String path) throws ParseException, IOException { File dir = new File(path); Directory index = FSDirectory.open(dir); query = query + "~"; Query q = new QueryParser(Version.LUCENE_43, "label", analyzer) .parse(query); IndexReader reader = DirectoryReader.open(index); IndexSearcher searcher = new IndexSearcher(reader); Query fuzzyQuery = new FuzzyQuery(new Term("label", query), 2); ScoreDoc[] fuzzyHits = searcher.search(fuzzyQuery, numberOfHits).scoreDocs; String[] fuzzyResults = new String[fuzzyHits.length]; for (int i = 0; i < fuzzyHits.length; ++i) { int docId = fuzzyHits[i].doc; Document d = searcher.doc(docId); fuzzyResults[i] = d.get("label"); } reader.close(); return fuzzyResults; } ```

Original source