繁体   English   中英

如何阅读Lucene.net返回的查询结果

[英]How to Read Lucene.net returned query results

我无法弄清楚如何读取Lucene.net查询返回的结果。

我有这个代码:

初始化

  var test = new Document();
            test.Add(new Field("id", "1", Field.Store.YES, Field.Index.NOT_ANALYZED));
            test.Add(new Field("title", "the title", Field.Store.YES, Field.Index.ANALYZED));
            test.Add(new Field("body", "the body of the question", Field.Store.YES, Field.Index.ANALYZED));

            string path = HttpRuntime.AppDomainAppPath + "\\LuceneIndex";
            Lucene.Net.Store.Directory directory = FSDirectory.Open(new DirectoryInfo(path));
            Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);

            var writer = new IndexWriter(directory, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
            writer.AddDocument(test);

            writer.Optimize();
            writer.Flush(true, true, true);
            writer.Dispose();

            directory.Dispose();
            analyzer.Dispose();

读数据

Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);

QueryParser parser = new QueryParser(Version.LUCENE_30, "ti", analyzer);

string path = HttpRuntime.AppDomainAppPath + "\\LuceneIndex";
Lucene.Net.Store.Directory directory = FSDirectory.Open(new DirectoryInfo(path));

IndexSearcher searcher = new IndexSearcher(directory);

Query query = new TermQuery(new Term("body", "body"));


TopDocs docs =  searcher.Search(query,5);
analyzer.Dispose();
searcher.Dispose();

我检查了docs的数据,但它不包含匹配的搜索结果的ID。

您可以按如下方式处理结果:

IndexSearcher searcher = new IndexSearcher(directory);
Query query = new TermQuery(new Term("body", "body"));
TopDocs docs =  searcher.Search(query,5);

// Get id and score for the top docs
ScoreDocs[] results = docs.ScoreDocs

foreach (ScoreDoc item in results)
{
    // Get lucene docID
    int luceneID = item.Doc

    // Get actual document for the docID from index
    Document doc = searcher.Doc(luceneID);
}

Lucene(.Net)为索引文档提供了自己唯一的docID,它独立于ID字段中存储的ID。 您可以通过调用searcher.Doc(int docID)或者可以调用reader.Doc(int docID)IndexReader访问实际的Document及其存储的字段。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM