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?
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.
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 String
s, Float
s or Integer
s.
Lucene in Action covers all of the details, as well as sorting by multiple fields and so on.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With