Jetpack: Search: add filters to allow customization of the search algorithm

Created on 11 Oct 2017  Â·  8Comments  Â·  Source: Automattic/jetpack

We do weight the default search query to boost recent content higher
https://github.com/Automattic/jetpack/blob/5.4/modules/search/class.jetpack-search.php#L906

It would be nice if such parameters could be filtered by site owners, so they can for example give more recent posts a bigger boost.

Read more about it in 1115-gh-jpop-issues

Search [Pri] Normal [Type] Enhancement

Most helpful comment

Another use case would be to boost WooCommerce products so a store / site would show those before blog posts or page content.

All 8 comments

Some useful feedback in 725559-zen as to the filters that he's set up for WordPress search that weren't working in Jetpack search.

We’ve modified the search query to sort by date displaying the newest stories first.

// Sort search results by date
function my_sort_search_query( $query ) {
    // not an admin page and is the main query
    if ( ! is_admin() && $query->is_main_query() ) {
        if ( is_search() ) {
            $query->set( 'orderby', 'date' );
        }
    }
}
add_action( 'pre_get_posts', 'my_sort_search_query' );

The built-in WordPress search adheres to our function limiting the amount of posts that display per page on archives and search results. The Jetpack Search, however, did not.

// Limit the number of posts per page in archives and search results
function my_archive_mod( $query ) {
    // Don't change in WordPress admin.
    if ( is_admin() || ! $query->is_main_query() ) {
        return;
    }

    // Display only 20 posts for the original blog archive.
    if ( $query->is_main_query() ) {
        $query->set( 'posts_per_page', '20' );
    }
}

Another use case would be to boost WooCommerce products so a store / site would show those before blog posts or page content.

Going to have to punt on adding boosting by post type for the next release too many dependencies. Adjusting scores by recency should make it in.

Just adding some notes on boosting.

The way I want to do this looks something like:

        $boosted_post_types = apply_filters(
            'jetpack_search_boost_post_types',
            array(),
            $args
        );

        if ( ! empty( $boosted_post_types ) ) {
            $parser->add_weighting_function( array(
                'filter' => array( 'terms' => array( 'post_type' => $boosted_post_types ) ),
                'weight' => 6.0,
            ) );
        }

Three problems:
1) add_weighting_function() is not yet ported from WP.com to Jetpack. This is the main blocker. Too close to code freeze to get it in.
2) Need a better way to boost by the weight
3) This is probably not the only case we want to easily boost on a single matching field, so should probably make this more generic.

The parts of this that are going to make it into 5.8 have been merged, so I am removing from that milestone. I want to get to the other ideas, but am worried they are too big of a change and that I am not sure what the filters should look like yet. Suspect the query will change some more.

A request for boosting by post type more easily (this is the same as the boosting of products mentioned above): https://wptavern.com/jetpack-5-8-adds-lazy-loading-for-images-module#comment-244890

I think there should be an easy filter for doing this boosting.

Should probably boost by title a bit more than we currently are. Especially comes up for older posts where there are many other posts with similar content. Example post when searching for exact title: https://fabiusmaximus.com/2006/01/31/myth/

Maybe try a 4x boost rather than 2x on title.

Also kinda related to #9426

Having an easier way to adjust the boosting for different fields would also help here a lot I think.

This issue has been marked as stale. This happened because:

  • It has been inactive in the past 6 months.
  • It hasn’t been labeled `[Pri] Blocker`, `[Pri] High`.

No further action is needed. But it's worth checking if this ticket has clear reproduction steps and it is still reproducible. Feel free to close this issue if you think it's not valid anymore — if you do, please add a brief explanation.

Marking this as done. The new version of search has a greatly improved alg that uses site stats to improve weighting. We're also going to be doing some relevancy evaluations. Mostly I don't want end users to have to customize the search alg.

Was this page helpful?
0 / 5 - 0 ratings