Why doesnt lucene return results

The name of the picture


Why doesnt lucene return results



I use lucene.net to index the documents. My main aim was to get to search and have the line number and the line of text returned in a document.



Here's the code that indexes


using (TextReader contentsReader = new StreamReader(fi.FullName))
{
doc.Add(new StringField("FullFileName", fi.FullName, Field.Store.YES));
doc.Add(new StringField("LastModifiedDate", modDate, Field.Store.YES));
//doc.Add(new TextField("Contents", contentsReader.ReadToEnd(), Field.Store.YES));

int lineCount = 1;
string line = String.Empty;
while ((line = contentsReader.ReadLine()) != null)
{
doc.Add(new Int32Field("LineNo", lineCount, Field.Store.YES));
doc.Add(new TextField("Contents", line, Field.Store.YES));
lineCount++;
}

Console.ForegroundColor = ConsoleColor.Blue;
Console.WriteLine("adding " + fi.Name);
Console.ResetColor();
writer.AddDocument(doc);
}



As you can see I add the filename, modified date, then I loop through all the lines in the file and add a TextField for each line.


TextField



This is how I search:


Lucene.Net.Store.Directory directory = Lucene.Net.Store.FSDirectory.Open(new System.IO.DirectoryInfo(indexDir));
Lucene.Net.Search.IndexSearcher searcher = new Lucene.Net.Search.IndexSearcher(Lucene.Net.Index.DirectoryReader.Open(directory));
TopScoreDocCollector collector = TopScoreDocCollector.Create(100, true);
searcher.Search(query, collector);
ScoreDoc hits1 = collector.GetTopDocs().ScoreDocs;
for (int i = 0; i < hits1.Length; i++)
{
int docId = hits1[i].Doc;
float score = hits1[i].Score;

Lucene.Net.Documents.Document doc = searcher.Doc(docId);

string result = "FileName: " + doc.Get("FullFileName") + "n"+
" Line No: " + doc.Get("LineNo") + "n"+
" Contents: " + doc.Get("Contents");
}



Yet. My search results return 0 hits whereas if I simply comment out that while loop and uncomment the commented line above I get the results.


while



What could be the problem?




1 Answer
1



Where is your QueryParser and Query ?





This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review
– ilim
3 mins ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived