lucene短语查询没有得到结果

时间:2013-12-18 01:14:22

标签: c# lucene

当我运行此代码时,不会返回任何结果 我正在做的错误在哪里

Lucene.Net.Store.Directory directory = FSDirectory.Open(new DirectoryInfo("LuceneIndex"));
Analyzer analyzer = new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29);
var writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);
Document doc = new Document();            
string path = "beslikelimecogul.txt";
string title = "contents";
doc.Add(new Field("path", path, Field.Store.YES, Field.Index.ANALYZED));
doc.Add(new Field("title", title, Field.Store.YES, Field.Index.ANALYZED));
writer.Optimize();
writer.Commit();
writer.Close();
Lucene.Net.Store.Directory directory1 = FSDirectory.Open(new DirectoryInfo("LuceneIndex"));
var searcher = new IndexSearcher(directory1);
PhraseQuery query = new PhraseQuery();
query.Add(new Term("contents", "sehid"), 0);
query.Add(new Term("contents", "dusen"), 1);
query.Add(new Term("contents", "gormezden"), 3);
query.Add(new Term("contents", "gelenlerin"), 4);
// display search results
List<string> results = new List<string>();
TopDocs topDocs = searcher.Search(query, 10);
foreach (ScoreDoc scoreDoc in topDocs.ScoreDocs)
{
    Document doc1 = searcher.Doc(scoreDoc.Doc);
    results.Add(doc1.Get("contents").Split(' ')[2]);
}

1 个答案:

答案 0 :(得分:0)

我猜你希望Lucene读取名为beslikelimecogul.txt的文件。不要 - 你必须自己做这件事并向Lucene提供文件内容。

另外,您创建了两个字段(pathtitle),但是您对contents进行了查询。

此外,无需重新打开同一目录,只需重复directory而不是创建directory1

希望这有帮助。