Elasticpress: Sort Search Result From Latest/Newest

Created on 9 Mar 2021  路  2Comments  路  Source: 10up/ElasticPress

I just using Elasticsearch + ElasticPress on my wordpress site. But I notice that the search result isn't being sorted by the latest/newest result. Is there any way / setting adjustment that I should make to do it that way? I've been trying to find it but unable to find any working solution.

Many thanks for the help.

question

All 2 comments

Hi @sirmbhe,

Just to add some context to future readers, by default ElasticPress won't order posts by date but by "score", i.e., how relevant a post is to the searched term. Post dates affect that score if you have Weight results by date enabled under the Post Search feature. That change can be too subtle though, but you can adjust that changing the values applied to the decay function. Something like this can change it:

add_filter( 'epwr_boost_mode', function() { return 'multiply'; } ); add_filter( 'epwr_decay', function() { return .5; } );

Follow #2042 for future changes related to that.

If you really want to order just by date (ignoring the score), you can add this snippet to your theme's functions.php or, even better, into a new plugin:

add_action(
    'pre_get_posts',
    function ( $query ) {
        if ( ! is_admin() && $query->is_main_query() && $query->is_search() && ! $query->get( 'orderby' ) ) {
            $query->set( 'orderby', 'post_date' );
            $query->set( 'order', 'DESC' );
        }
    }
);

I hope that helps!

@felipeelia I can confirm that changing epwr_boost_mode to multiply and epwr_decay to .5 significantly improved the relevance of the date weight on the results!

Thanks a lot for sharing that!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vnmedeiros picture vnmedeiros  路  3Comments

mihaijoldis picture mihaijoldis  路  6Comments

jakejackson1 picture jakejackson1  路  8Comments

MediaMaquina picture MediaMaquina  路  3Comments

barryceelen picture barryceelen  路  4Comments