簡體   English   中英

無法使用Lucene.net獲取搜索到的文檔

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

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

當嘗試調試時,我得到的內容為空,為空。

我是否在代碼中遺漏了任何東西,這從一整天以來一直困擾着我。 請幫幫我。

提前致謝。

我一直在嘗試所有可能的方法。 問題是我一直在沒有將文檔的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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM