I am looking for a way to custom sort my Lucene.Net results where I place null
-values (field does not exist on document) at the bottom no matter the direction (ascending or descending) of the sort.
The following data sums up the situation and the wanted results:
data in index wanted sort result
data desc asc
---- ---- ----
100 400 100
400 300 300
null 100 400
300 null null
My situation is that I have some products where not all products have a price. When sorting ascending, I want the cheapest products first, not the products with no price (as is the expected default behavior). The products with no price should still be in the result, but at the end, since these are least relevant when sorting on price.
I've looked quite a bit around with google and I haven't really found any answer to how you implement custom sorting in Lucene.Net 3.0.3.
The best example I've found is this answer that seems to point me in the direction I'm looking for. But the answer is old and the ScoreDocComparator
it is refering to, seems to be deprecated in the original source, and thereby also in the current version 3.0.3 of Lucene.Net.
The original project refers to FieldComparator as replacement, but this seems to be highly more complex to implement than the ScoreDocComparator
(a lot of methods that needs to be implemented/overridden and many which could benefit of inheritance instead of duplicate implementations), and I get in doubt that this is the right path to go with?
Ideally I want to implement something generic for int/long fields where it can take fieldname in account like the SortField object, since I expect to have more fields in the future that would benefit of this custom sorting behavior.
I would think that the implementation is done somewhere around the usage of Sort
/SortField
class, so my ending usage code could be something like:
var sort = new Sort(new MyNullLastSortField("Price", SortField.INT, reverse));
But maybe that is also the wrong way? SortField
has a constructor which takes a FieldComparator
as parameter, but I can't seem to wrap my head around how this is constructed and implemented and where the actual data values from the index flows in and out.
Any help pointing me in the right direction (preferably with sample code) is much appreciated.
My failover solution (not prefered) will be to add two fields to the index that is only used to do the sorting, manually handling the null values on insert time and set them to -1 in the descending case and to 9999999 in ascending case. Then sort normally by the field with the specific fieldname for the price and the direction.