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.
In the original (Java) version of Lucene, there is no hard restriction on the size of the the
TopFieldDocCollector
results. Any number greater than zero is accepted. Although memory constraints and performance degradation create a practical limit that depends on your environment, 5000 hits is trivial and shouldn't pose a problem outside of a mobile device.Perhaps in porting Lucene,
TopFieldDocCollector
was modified to use something other than Lucene's "heap" implementation (calledPriorityQueue
, extended byFieldSortedHitQueue
)—something that imposes an unreasonably small limit on the results size. If so, you might want to look at the source code forTopFieldDocCollector
, and implement your own similar hit collector using a better heap implementation.I have to ask, however, why are you trying to collect 5000 results? No user in an interactive application is going to want to see that many. I figure that users willing to look at 200 results are rare, but double it to 400 just as factor of safety. Depending on the application, limiting the result size can hamper malicious screen scrapers and mitigate denial-of-service attacks too.
The constructor for Sort accepting only the string field name has been depreciated. Now you have to create a sort object and pass it in as the last paramater of searcher.Search()
The
search.Searcher.search
method will accept asearch.Sort
parameter, which can be constructed as simply as: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 orInteger
s.Lucene in Action covers all of the details, as well as sorting by multiple fields and so on.
What you're looking for is probably TopFieldDocCollector. Use it instead of the
GroupingHitCollector
(what is that?), or inside it.Comment on this if you need more info. I'll be happy to help.