16

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.

Community
  • 1
  • 1
Ed.
  • 1,654
  • 7
  • 20
  • 33

4 Answers4

25

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.

matija kancijan
  • 1,205
  • 10
  • 11
James Brady
  • 27,032
  • 8
  • 51
  • 59
0

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()

/* sorting by a field of type long called "size" from greatest -> smallest 
(signified by passing in true for the last isReversed paramater)*/

Sort sorter = new Sorter(new SortField("size", SortField.Type.LONG, true))
searcher.Search(myQuery, collector, sorter);
Antoine Dahan
  • 574
  • 2
  • 9
  • 23
0

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.

itsadok
  • 28,822
  • 30
  • 126
  • 171
  • 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... – Ed. Feb 04 '09 at 02:20
0

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 (called PriorityQueue, extended by FieldSortedHitQueue)—something that imposes an unreasonably small limit on the results size. If so, you might want to look at the source code for TopFieldDocCollector, 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.

erickson
  • 265,237
  • 58
  • 395
  • 493
  • 1
    "No user in an interactive application is going to want to see that many" is a bit presumptuous. Users scream and complain even when we truncate to 200,000 for legitimate technical reasons... – Hakanai Feb 22 '13 at 05:28