2013-05-16 150 views
1

我有一个网站给用户提供数据。我想为我的自动完成使用Lucene.Net。事情是我想能够返回正确拼写错误的结果。我发现Lucene.Net具有拼写检查功能,可以显示其他词语。但它返回的话,我需要的ID,以获得更多的信息该项目。当我从拼写检查器得到结果后还需要对常规索引执行另一个查询吗?还是有更好的方法?C#Lucene.Net拼写检查器

回答

3

您将需要搜索它,它不能这样做,因为拼写检查工作在与您没有链接的单独索引上主要索引您创建的建议。

它很容易做到寿:

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(); 
+0

你的答案看起来有趣,易于实现。当试图得到一些错误,如:“类型'Lucene.Net.Store.Directory'是在未引用的程序集中定义的。您必须添加对程序集'Lucene.Net,Version = 2.0.0.4的引用, Culture = neutral,PublicKeyToken = null'“。和“不能从'Lucene.Net.Store.RAMDirectory'转换为Lucene.Net.Store.Directory'”。我正在引用lucene.net版本3.0.3.0。有任何想法吗? – Gidi

+0

好像你在引用冲突的程序集,尝试删除所有引用,并得到Lucene.Net 3.0.3和Lucene.Net 3.0.3 Contrib,并做一个干净的重建。我建议你从Nuget获得程序集。我用nuget构建了3.0.3的例子 –

+0

好吧,我修正了这个问题。我不明白你为什么需要“RAMDirectory dir = new RAMDirectory();”我看不到你用它。它是否必须指向索引目录?我试过这样,它返回空结果。 – Gidi