6

I am working my way through lucene and been stumped on this issue with the Hits object. I have a Using Lucene.Net.Search but for some reason the VS12 Express cannot find the Hits object so the following fails to compile.

The compiler complains about this line

Hits hits = searcher.Search(booleanQuery, hits_limit);

with the following error message

Error 1 The type or namespace name 'Hits' could not be found (are you missing a using directive or an assembly reference?)

I do not get it, according to the online tutorials alk you need is Lucnen.Net.Search

Any Advice

// validation
if (subqueries.Count == 0) return new List<MATS_Doc>();
// set up lucene searcher
Searcher searcher = new IndexSearcher(_directory, false);
var hits_limit = 1000;
var analyzer = new StandardAnalyzer(Version.LUCENE_30);
BooleanQuery booleanQuery = new BooleanQuery();
foreach (Query fieldQuery in subqueries)
{
    booleanQuery.Add(fieldQuery, Occur.SHOULD);
}
//var parser = new QueryParser(Version.LUCENE_30, searchField, analyzer);
//var query = _parseQuery(searchQuery, parser);
Hits hits = searcher.Search(booleanQuery, hits_limit);
IEnumerable<MATS_Doc> results = _mapLuceneSearchResultsToDataList(hits, searcher);
analyzer.Close();
searcher.Dispose();
return results;
Gergo Erdosi
  • 40,904
  • 21
  • 118
  • 94
TheCodeNovice
  • 750
  • 14
  • 35

1 Answers1

15

I use Lucene.net 3.0.3, and Search() returns a TopDocs object, which contains a few properties and an array of ScoreDoc elements. Here is an exemple :

Lucene.Net.Search.TopDocs results = searcher.Search(booleanQuery, null, hits_limit);


foreach(ScoreDoc scoreDoc in results.ScoreDocs){
    // retrieve the document from the 'ScoreDoc' object
    Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc);
    string myFieldValue = doc.get("myField");
}
mbarthelemy
  • 12,465
  • 4
  • 41
  • 43
  • Thanks! is there anyway to get the whole document back in one shot as opposed to field by field? – TheCodeNovice Feb 19 '13 at 21:26
  • the line **Lucene.Net.Documents.Document doc = searcher.Doc(scoreDoc.Doc);** fetches the whole document. Then you can access its fields just like I did in my example, or iterate through them using something like **foreach(Lucene.Net.Documents.Field f in doc.GetFields()) { Console.WriteLine("field="+f.Name+", value="+f.StringValue)}** – mbarthelemy Feb 19 '13 at 21:29