简体   繁体   English

无法使用Lucene.net获取搜索到的文档

[英]Unable to get the searched document using Lucene.net

I'm new to Lucene.net. 我是Lucene.net的新手。 I've a situation where I need to search the all the documents in a folder for a keyword that has been entered by the user. 我遇到一种情况,我需要在文件夹中的所有文档中搜索用户输入的关键字。

I've indexed all the files in the folder and prepared a query for the keywords entered by the user and performed searching. 我已经索引了文件夹中的所有文件,并准备了一个查询,以查询用户输入的关键字并执行搜索。

The problem is I could get the hits and when I tried to iterate the hits, I couldn't get the fields from the documents of the hits. 问题是我可以获取匹配,而当我尝试迭代匹配时,我无法从匹配的文档中获取字段。

Here is my code. 这是我的代码。

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
   }
}

When tried to debug, the content I'm getting is null, empty. 当尝试调试时,我得到的内容为空,为空。

Am I missing anything in my code, this is literally bogging me since half day all the way. 我是否在代码中遗漏了任何东西,这从一整天以来一直困扰着我。 Please help me out. 请帮帮我。

Thanks in advance. 提前致谢。

I've been trying this everything whatever I could do. 我一直在尝试所有可能的方法。 The problem is I've been indexing without storing the id field of the document in the index file. 问题是我一直在没有将文档的id字段存储在索引文件中的情况下进行索引。

Here was the code I've used while indexing. 这是我在索引时使用的代码。

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

While it should be like the following, so that it will be available in the index file. 虽然应该如下所示,但它将在索引文件中可用。

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

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

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