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

33,221

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.

Share:
33,221
Ed.
Author by

Ed.

Updated on April 07, 2020

Comments

  • Ed.
    Ed. about 4 years

    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.

  • James Brady
    James Brady over 15 years
    ...and not tokenized - beat me to it :)
  • Ed.
    Ed. over 15 years
    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...
  • Hakanai
    Hakanai about 11 years
    "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...
  • Scott Simontis
    Scott Simontis about 9 years
    @janwen According to the docs for Lucene, it is not required that the value be stored, only indexed