我用 lucene 4.3.1 实现了模糊搜索,但我对结果不满意。我想指定它应该返回的一些结果。因此,例如,如果我想要 10 个结果,它应该返回 10 个最佳匹配项,无论它们有多糟糕。大多数情况下,如果我搜索的单词与索引中的任何内容都非常不同,它不会返回任何内容。我怎样才能获得更多/更模糊的结果?
这是我的代码:
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;
}