Mugo Web main content.

Sorting search results by date-based relevance in eZ Find

By: Peter Keung | November 19, 2015 | eZ Publish development tips and Web solutions

The eZ Publish search extension eZ Find offers many sorting parameters for its search results, the most common being by relevance / score, and by date. On large sites, sorting by date can lead to non-relevant results showing up first. What if you want to sort by date and relevance, essentially boosting newer content but still giving you relevant results?

With eZ Find, results can be boosted at index time or at query time. In this case, it makes the most sense to boost the results by date at query time, so that other searches aren't affected by the age of the content.

eZ Find uses the Solr search engine, which has some documentation on date-based boosting. Using the native Solr functionality and eZ Find's boost_functions parameter, we can construct a query such as this:

{def $search_parameters = hash( 'query', $q,
                                                 'offset', $offset,
                                                 'class_id', $classes,
                                                 'limit', $page_limit,
                                                 'boost_functions', hash( 'mfunctions', array( 'recip(ms(NOW/HOUR,meta_published_dt),3.16e-11,1,1 )' ) )

{set $search = fetch( 'ezfind', 'search', $search_parameters )}

"mfunctions" makes the boost multiplicative instead of additive, and "meta_published_dt" is the name of the Solr field that stores the standard object publish date.

With such a query, the standard score for each is multiplied by "1 for very recent documents, 1/2 for documents a year old, 1/3 for documents two years old", and so on, as per the example documentation, with "3.16e-11" representing the inverse of a year in milliseconds. The boost can be applied differently based on different periods, with a boost intensity to suit your site. Here is a useful graph showing how the different settings can be tweaked to adjust the intensity scale.