简体   繁体   中英

Unable to get the searched document using Lucene.net

I'm new to 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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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