Sorting based on relevance score and field

Hello everyone.
Currently I want to sort based on relevance score (matching), but sometimes user might want to sort by content freshness (creation date), so I also want to sort content as fresh as possible.

So, there’s two thing to be order by here: Matching score and entity creation date.
Current documented multi-sort is sort by score first, then when the same score happen for multiple entities, then we will sort again based on freshness.
But I think in many cases, that relevance will win over freshness. So multiple old entity with high relevance score will appear on top of the result.

My proposed solution is: We gonna order by creation date first, then select top n result, for example top 100 entities, which will be temporary table. Then we’ll apply matching score sorting to that temporary table instead of entire result set. That will somewhat guarantee freshness of result.

Is that something can be done with Hibernate Search ? Backend is Apache Lucene.
Thank you.

First, I’d advise against this, because you will end up with all sorts of problems when you start considering paging (i.e. scrolling to the second page of results).

If you really want it, sure, you can do it. Just ask Hibernate Search to sort by creation date, limit the results to the top 100 hits, and project on both the entity and score so that you can sort the resulting list manually, with Collections.sort(List, Comparator).

But really, I’d advise you to clarify the requirements instead. Because that’s your real problem: the requirements do not make sense. You cannot sort on two (almost) unique values (creation date and score) simultaneously. Find out what the problems of your users are exactly. Maybe they just need a toggle in the UI between “sort by score” and “sort by creation date”. I’m afraid I cannot help you with that, it’s between you and your user :slight_smile:

1 Like