Skip to content

Jetpack Search

Jetpack Search is a paid upgrade to the Jetpack plugin that improves the search experience for WordPress sites. Jetpack Search uses Elasticsearch for indexing and searching, typically without any need for a site owner to add custom code.

When enabled on a VIP site, Jetpack Instant Search syncs content changes to a private cache site located within Automattic’s data centers, and uses that cached copy to perform indexing. When search queries are performed on the VIP site, the database query is sent to a public endpoint which only returns results for publicly available data.

To enable Jetpack Search on a production WordPress site:

  1. Create a VIP Support request and specify the site for which Jetpack Search should be enabled.
  2. VIP Support will communicate in the support ticket when this is complete and Jetpack Search is ready to use.

Enable Jetpack Search on a non-production site

Enabling Jetpack Search on a non-production WordPress site requires a VIP Support request as well as code-enablement. After VIP Support has enabled Jetpack Search on a non-production site, it will not work as expected until Jetpack’s search module is force-enabled via code.

To enable Jetpack Search on a non-production WordPress site:

  1. Create a VIP Support request and specify the site for which Jetpack Search should be enabled.
  2. VIP Support will communicate in the support ticket when this is complete and Jetpack Search is ready to use.
  3. Force-enable the Jetpack search module on a non-production site with the jetpack_active_modules filter. Add the following code snippet to a file in /client-mu-plugins/ within the branch that deploys to the environment of the non-production site:
/client-mu-plugins/plugin-name.php
/**
 * Force Jetpack 'search' module to be active.
 *
 * @param array $modules
 *
 * @return array
 */

function enable_jetpack_search( $modules ) {
    $modules[] = 'search';
    return array_unique( $modules );
}

add_filter( 'jetpack_active_modules', 'enable_jetpack_search', 9999 );

When adding this code to a WordPress multisite, additional logic may be needed in order for the jetpack_active_modules filter to only be applied to a specific network site(s).

Jetpack Search is only able to handle front-end search queries (whereas Enterprise Search is able to offload front-end and non-standard queries). To offload an existing front-end query to the Jetpack Search index, 'es' => 'true' must be set for those WP_Query parameters. Supported queries that are sent to the Jetpack Search index will obtain a list of matching post IDs, and a MySQL query will then fetch the post data.

In this code example, the pre_get_posts filter is used to enable offloading queries for category pages, tag pages, and author pages to the Jetpack Search index:

/**
* Enable Elastic search for category, tag and author pages.
*
* This is optional, and only necessary if you need Jetpack search
* for these queries
*
* @param $query
*/
function vip_s__maybe_enable_es_wp_query( $query ) {

        if ( is_admin() || ! $query->is_main_query() ) {
                return;
        }

        if ( $query->is_category() || $query->is_tag() || $query->is_author() ) {
                $query->set( 'es', 'true' );
        }
}

add_action( 'pre_get_posts', 'vip_s__maybe_enable_es_wp_query' );

Last updated: July 16, 2024

Relevant to

  • WordPress