Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I sort Lucene results by field value using a HitCollector?

Tags:

search

lucene

I'm using the following code to execute a query in Lucene.Net

var collector = new GroupingHitCollector(searcher.GetIndexReader());
searcher.Search(myQuery, collector);
resultsCount = collector.Hits.Count;

How do I sort these search results based on a field?


Update

Thanks for your answer. I had tried using TopFieldDocCollector but I got an error saying, "value is too small or too large" when i passed 5000 as numHits argument value. Please suggest a valid value to pass.

like image 661
Ed. Avatar asked Jan 30 '09 22:01

Ed.


1 Answers

The search.Searcher.search method will accept a search.Sort parameter, which can be constructed as simply as:

new Sort("my_sort_field")

However, there are some limitations on which fields can be sorted on - they need to be indexed but not tokenized, and the values convertible to Strings, Floats or Integers.

Lucene in Action covers all of the details, as well as sorting by multiple fields and so on.

like image 166
James Brady Avatar answered Nov 16 '22 02:11

James Brady