Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting in lucene.net

Tags:

sorting

lucene

I got my lucene index with a field that needs to be sorted on. I have my query and I can make my Sort object. If I understand right from the javadoc I should be able to do query.SetSort(). But there seems to be no such method...

Sure I'm missing something vital. Any suggestions?

like image 992
Boris Callens Avatar asked Apr 28 '09 15:04

Boris Callens


2 Answers

There are actually two important points. First, the field must be indexed. Second, pass the Sort object into the overloaded search method.

Last time I looked, the docs didn't do a very good job of pointing out the indexing part, and certainly didn't explain why this is so. It took some digging to find out why.

When a field is sortable, the searcher creates an array with one element for each document in the index. It uses information from the term index to populate this array so that it can perform sorting very quickly. If you have a lot of documents, it can use a lot of memory, so don't make a field sortable unless there is a need.

One more caveat: a sortable field must have no more than one value stored in each field. If there are multiple values, Lucene doesn't know which to use as the sort key.

like image 71
erickson Avatar answered Nov 21 '22 20:11

erickson


It looks like the actual method you want is e.g. Searcher.search(Query query, Filter filter, int n, Sort sort). setSort is a method of Sort.

like image 38
Matthew Flaschen Avatar answered Nov 21 '22 20:11

Matthew Flaschen