Using ElasticSearch on WordPress

So here’s how I recently applied it and what my observations were:

An FAQ page was loading at anywhere between 0.6s to 0.9s. It’s a page intended to display all FAQs but broken up into their FAQ categories. (It’s an entirely custom post_type and custom taxonomy.) My team are still adding FAQs from the clients existing site as well as new documents they’ve provided. So it’s still pretty light weight.

I’m running a get_terms() to grab all the FAQ taxonomy IDs, then a foreach to loop through them and pull every FAQ post within each FAQ Category. (There are duplicates, but that’s by design as per client request.)

I’m thinking this is eventually going to be a pretty big page, lots of categories from my get_terms() so lots of unique new WP_Query within the foreach.

According to Query Monitor the page is already executing 17 core WP queries (aka WP_Query), but in 0.6 to 0.9s, so it’s ok, for now.

Now we test with ElasticPress:

I added a single line to my query_args.

$query_args     = array(
    'ep_integrate'      => true, //this single line...
    'post_type'         => 'prefix_faqs',
    'post_status'       => 'publish',
    'posts_per_page'    => -1,
    'tax_query'         => array(
        array(
            'taxonomy'      => 'prefix_faq_cat',
            'field'         => 'term_id',
            'terms'         => $faq_term->term_id,
        )
    ),
    'orderby'           => 'menu_order',
    'order'             => 'ASC'
);
$faq_query = new WP_Query( $query_args );

Page loads are now completed in 1.15s to 1.22s. So a bit slower. This is because I’m communicating with another server, so there’s that nominal delay and expected I guess.

But here’s the interesting bit… …the query count went down from 17 to 5. The WP Queries in my foreach() loop aren’t taxing the server – they’re executing via ElasticPress.

As of right now, it appears slower, but less taxing on the server. And when I say ‘appears slower’ I don’t mean visually. Just observing with your eyes the difference is imperceptible – everything is loaded and in place and by the time you get to an FAQ category section the page is fully loaded.

However, once this page has the volume of FAQs I’m anticipating, I imagine applying ElasticPress will have both the benefit of reducing server load AND reducing load times.

Hope that helps.