WordPress

Why Use Elasticsearch on Your WordPress Site?

What Is Elasticsearch?

Elasticsearch is an incredibly fast, open-source, distributed, and highly-scalable solution for managing your searchable content. Elasticsearch can scale up with your site, because of its distributed architecture. This means that as your site grows, Elasticsearch grows with it; and it still provides performance benefits. One of the main advantages of Elasticsearch is to offload search to a separate service, which saves valuable server resources for your site.

Why Use Elasticsearch?

WordPress’ built-in search is not optimized on sites that operate with heavy search use or complex searches. That’s because WordPress’ search works by matching full sentences in post titles, any/all search terms in post titles, and full sentence matches in post content. It relies on MySQL and does not support complex relevancy calculations or advanced filtering. More complex queries also have the risk of utilizing significant server resources.

Benchmarking Setup:

  • VVV2 Vagrant box with test site
  • Elasticsearch Vagrant box – base setup with only one cluster
  • WordPress version 4.7.3 with ~24471 posts
  • ElasticPress version 2.4.1 to integrate WP queries with ElasticSearch
  • Debug Bar and Debug Bar ElasticPress to monitor ElasticSearch queries
  • Query Monitor to monitor raw SQL queries

Normal Queries:
Consider a user visits your site and searches for “wine country” to find relevant events. Using normal WordPress search, the search query would execute in about 987.2ms. The equivalent Elasticsearch query executes in about 11ms, which is over 89 times faster than normal WordPress search.

Complex Queries:
Now, imagine your site also has a more complex search that includes a form with multiple taxonomies, meta fields, additional keywords, and date range search. A user comes to your site and searches for all “half marathon” events that occur between March 1, 2017 and June 30 2017 in either Arizona or California. A complex query like this, where event dates are stored in postmeta and event type is a taxonomy, would require at least eight MySQL joins and take about 1260ms to execute. The equivalent Elasticsearch query executed in about 16ms.

Apache Benchmarking:
When put to the test with Apache benchmarking using 10,000 requests and 100 concurrent users against a simple search query (e.g., “Short Ribs”), the results were:

Search Provider  

Time per Request

(mean, across all concurrent requests)

Default WP 89.708 [ms]
SearchWP/FacetWP 216.619 [ms]
Elasticsearch 88.564 [ms]

Using the complex query from above that queries posts, meta, taxonomies, and dates and benchmarking using 10,000 requests and 100 concurrent users gives the following results:

Search Provider  

Time per Request

(mean, across all concurrent requests)

Raw SQL 240.163 [ms]
Elasticsearch 102.867 [ms]

Other Reasons to Use Elasticsearch:

    • Elasticsearch isn’t only for search results pages. It can also be used to for almost any query for performance benefits. Elasticsearch mappings and queries can be updated to enhance querying.
    • Dynamics rules and boosts can be added to queries to allow more control over how search results are displayed. For example: a user is searching for events in California and certain events are arranged to show before the rest.
    • Elasticsearch aggregations can be used to build dynamic facet forms, where only taxonomy terms that won’t show empty result sets are shown.
    • Search all or some sites on a network for results, without cross table joins by searching an Elasticsearch alias.
    • There are many more possibilities on ways to use Elasticsearch to boost performance and add features to your site’s search.

Conclusion

Elasticsearch is a good way to make your site performant and add enhancements to search. Whether it be facet forms, simple searches, or complex rules applied to search results, Elasticsearch will outperform most alternatives.

Comments

Have a comment?

Your email address will not be published. Required fields are marked *

accessibilityadminaggregationanchorarrow-rightattach-iconbackupsblogbookmarksbuddypresscachingcalendarcaret-downcartunifiedcouponcrediblecredit-cardcustommigrationdesigndevecomfriendsgallerygoodgroupsgrowthhostingideasinternationalizationiphoneloyaltymailmaphealthmessagingArtboard 1migrationsmultiple-sourcesmultisitenewsnotificationsperformancephonepluginprofilesresearcharrowscalablescrapingsecuresecureseosharearrowarrowsourcestreamsupporttwitchunifiedupdatesvaultwebsitewordpress