【问题标题】:Unable to get the searched document using Lucene.net无法使用 Lucene.net 获取搜索到的文档
【发布时间】:2016-09-04 13:56:32
【问题描述】:

我是 Lucene.net 的新手。我有一种情况,我需要在文件夹中的所有文档中搜索用户输入的关键字。

我已经为文件夹中的所有文件编制了索引,并为用户输入的关键字准备了查询并执行了搜索。

问题是我可以获得命中,而当我尝试迭代命中时,我无法从命中的文档中获取字段。

这是我的代码。

public void Searching()
{
   Analyzer analyzer = new StandardAnalyzer(luceneVersion.Version.LUCENE_29);
   QueryParser parser = new QueryParser(luceneVersion.Version.LUCENE_29, "content", analyzer);
   Query query = parser.Parse(txtSearchText.Text);

   Directory directory = FSDirectory.Open(new System.IO.DirectoryInfo(txtIndexPath.Text.Trim()));
   Searcher searcher = new IndexSearcher(IndexReader.Open(directory, true));
   TopScoreDocCollector collector = TopScoreDocCollector.Create(100, true):
   searcher.Search(query, collector);
   ScoreDoc [] hits = collector.TopDocs(). ScoreDocs;

   foreach (ScoreDoc hit in hits)
   {
      int id = hit.Doc;
      float score = hit.Score;

      Document doc = searcher.Doc(id);

      string content = doc.Get("content");  // null
   }
}

尝试调试时,我得到的内容为空,为空。

我的代码中是否遗漏了任何内容,这从半天以来一直困扰着我。请帮帮我。

提前致谢。

【问题讨论】:

    标签: c# .net indexing lucene lucene.net


    【解决方案1】:

    我一直在尝试一切我能做的事情。问题是我一直在索引,而没有将文档的 id 字段存储在索引文件中。

    这是我在编制索引时使用的代码。

    doc.Add(new Field("id", id, Field.Store.NO, Field.Index.ANALYZED);
    

    虽然它应该如下所示,以便在索引文件中可用。

    doc.Add(new Field("id", id, Field.Store.YES, Field.Index.ANALYZED);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-24
      • 2017-01-31
      • 1970-01-01
      • 2022-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多