我有一个向用户提供数据的网站。我想使用 Lucene.Net 进行自动完成。问题是我希望能够返回纠正拼写错误的结果。我看到 Lucene.Net 具有建议其他词的拼写检查功能。但它会返回单词,我需要 ID 才能获得该项目的更多信息。从拼写检查器获得结果后,我是否必须对常规索引进行另一次查询,还是有更好的方法???
最佳答案
您将需要搜索它,它无法执行此操作,因为拼写检查工作在一个单独的索引上,该索引未链接到您创建建议的主索引。
这很容易做到:
RAMDirectory dir = new RAMDirectory();
IndexWriter iw = new IndexWriter(dir, new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30), IndexWriter.MaxFieldLength.UNLIMITED);
Document d = new Document();
Field textField = new Field("text", "", Field.Store.YES, Field.Index.ANALYZED);
d.Add(textField);
Field idField = new Field("id", "", Field.Store.YES, Field.Index.NOT_ANALYZED);
d.Add(idField);
textField.SetValue("this is a document with a some words");
idField.SetValue("42");
iw.AddDocument(d);
iw.Commit();
IndexReader reader = iw.GetReader();
SpellChecker.Net.Search.Spell.SpellChecker speller = new SpellChecker.Net.Search.Spell.SpellChecker(new RAMDirectory());
speller.IndexDictionary(new LuceneDictionary(reader, "text"));
string [] suggestions = speller.SuggestSimilar("dcument", 5);
IndexSearcher searcher = new IndexSearcher(reader);
foreach (string suggestion in suggestions)
{
TopDocs docs = searcher.Search(new TermQuery(new Term("text", suggestion)), null, Int32.MaxValue);
foreach (var doc in docs.ScoreDocs)
{
Console.WriteLine(searcher.Doc(doc.Doc).Get("id"));
}
}
reader.Dispose();
iw.Dispose();
关于C# Lucene.Net 拼写检查器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16586212/