title: fuzzy search with lucene
body: <p>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?</p>
  
  <p>Here the code I have:</p>
  
  <pre><code>    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 &lt; fuzzyHits.length; ++i) {
          int docId = fuzzyHits[i].doc;
          Document d = searcher.doc(docId);
          fuzzyResults[i] = d.get("label");
      }
  
      reader.close();
      return fuzzyResults;
  }
  </code></pre>