tags:

views:

2870

answers:

5

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.

+4  A: 

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 my multiple fields and so on.

Alabaster Codify
One key point: the field to be sorted must be indexed.
erickson
...and not tokenized - beat me to it :)
Alabaster Codify
A: 

thanks for your input,but I need to use collector object(instead of using hits) and I dont see any overloaded Searcher.search method which returns a collector object as well as sort it on a field.

Ed
That's easy to do as well. Maybe if you accept answers for some of the other questions you've already asked, an answer will appear.
erickson
@erickson: Well, he only asked 2 questions so far, and the other one is really general.@unknown: Consider choosing a better user name. Also, you should edit you question with clarifications instead of posting an "Answer".
itsadok
He/she has asked at least fifteen questions so far, with no accepted answers. Look at the gravatars, which are a hash of the identity.
erickson
OK, now you got me all curious. How do you track someone's questions through their gravatar?
itsadok
Delete this "answer". Edit your question to provide additional information as needed.
erickson
A: 

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
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
A: 

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...

Thanks.

Ed
Delete this "answer". Updates to your question should be provided by editing (as I've done for you).
erickson
A: 
erickson