Add the ability to inverse a Sort
Description
Currently there is no easy way to reverse a Sort unless you know how that sort was created. Adding an inverse option gives users an easy way to reverse a given sort.
In Solr, I plan on using this to implement reverse pagination (cursorMark).
This PR does not have an entry in lucene/CHANGES.txt. Consider adding one. If the PR doesn't need a changelog entry, then add the skip-changelog label to it and you will stop receiving this reminder on future updates to the PR.
I'm unsure about this PR for a few reasons:
-
It sounds relatively easy to do on the application side by using
Sortfactories that can either produce aSortobject in the natural or inverse order and creating thisSortobject at the last moment when we know what order is needed. -
It doesn't seem to handle ties correctly, you probably need to append a
SortFieldthat sorts by descending doc ID, since Lucene tie-breaks by ascending doc ID by default. But then it's a bit annoying that the inverseSortobject produces different sort values compared with the originalSortobject? -
The handling of missing values probably needs changing, inversing a
SortFieldon a String field that it configured to sort missing values last should probably sort missing values first and vice-versa?
I wonder if there's a better way of doing this, maybe something like adding support for searchBefore, which would do the opposite of searchAfter (I'm thinking out loud, I having given much thoughts to this).
I wonder if there's a better way of doing this, maybe something like adding support for
searchBefore, which would do the opposite ofsearchAfter(I'm thinking out loud, I having given much thoughts to this).
I would be all for this, but it seemed like a harder/more intrusive change.
It doesn't seem to handle ties correctly, you probably need to append a SortField that sorts by descending doc ID, since Lucene tie-breaks by ascending doc ID by default. But then it's a bit annoying that the inverse Sort object produces different sort values compared with the original Sort object?
That's fair, it wasn't something I was considering since in our use case, the uniqueId must be a part of the sort. (No ties)
The handling of missing values probably needs changing, inversing a SortField on a String field that it configured to sort missing values last should probably sort missing values first and vice-versa?
Again, fair. In my mind, Lucene only had a "missing value" not a "sort missing last" option, like Solr does. But you are right, there would be no way to fix that when using this inverse() method.
Thanks for the feedback, I think it'll probably be best to do this in the Application, as you mentioned. (Or have a searchBefore() option, which would be great!)
Thinking a bit more about it, adding searchBefore sounds like it could work. Would you like to give it a try?
Should we refuse to allow inverse of Sort.SCORE? or RELEVANCE?
This PR has not had activity in the past 2 weeks, labeling it as stale. If the PR is waiting for review, notify the [email protected] list. Thank you for your contribution!
This PR has not had activity in the past 2 weeks, labeling it as stale. If the PR is waiting for review, notify the [email protected] list. Thank you for your contribution!