This article on WordPress search was originally published by Torque Magazine, and is reproduced here with permission.
If you have ever worked on a WordPress site with a massive amount of posts and pages, you might have noticed something about the WordPress search: it doesn’t perform well. In this article, we dive into why the default WordPress search doesn’t scale and some ways to make it better.
How WordPress Searches
By default, the native WordPress search performs a query on your database that looks something like this:
Let’s break down this query a little bit.
- First, we are using
wp_poststable. This says, if there were no limit (coming later), how many results would the following query turn up?
- Then we search through the
post_contentcolumns for the search term (in this example I searched “tech”).
- And we ensure that the content returned is a post, page, or attachment (media).
- We check to make sure the status is “public”–not deleted or private (if you are a logged-in user it will also search only your own private posts).
- We order the results by matching title, descending, or post date, descending.
- Last, we limit the results to 10, paginating the rest of the results.
That’s a whole lot of checks WordPress runs through just to return search results. And every time you iterate through
wp_posts to perform each check, MySQL is processing all rows in wp_posts. When you have hundreds of thousands of rows in the
wp_posts table, this query can get ugly really quick. Here are some benchmarks from a few of my sites:
On sites with a very large number of posts, the query took over 20 seconds! This is not a scalable search option for sites with high traffic.
Knowing what we now know about the default search behavior, it’s important to call out why this might not be ideal for some websites.
- WordPress only searches the “post_title,” “post_content,” and “post_excerpt” fields for your search terms. It also only presents “post,” “page,” and “attachment” post types. For users with custom post types, custom fields, or plugins like WooCommerce that add different page/post types, this can be problematic: these items will not appear in search results.
- The query used by WordPress search performs very poorly at over 100,000 posts. For news sites or media sites with more than 100,000 posts, a search could take several seconds to perform.
- The poor performance on the search query could cause server performance issues if your site receives a fair amount of search traffic.
Improving on WordPress Native Search
So now that we know WordPress native search isn’t an option for many sites, we can explore search solutions. Based on our problem identification above, our qualifications for a search solution include:
- Search tools that will search all content or customized sets of content, not limited to the post types or post fields defined by default.
- Search tools that perform well when presented with large data sets.
- Search tools that will not cause poor server performance when presented with high traffic.
Below we will explore several solutions, including enterprise-grade external services and WordPress plugins.
Algolia is an enterprise-grade search solution in which your posts and content are indexed offsite and returns results to your users. Its feature set includes fuzzy search, geolocation search, multi-language support, and search with synonyms.
Comparing search performance on a site with thousands of posts, there are some clear benefits with Algolia. Below is the performance of the default WordPress search:
Notice the search took 7.43 seconds using the default WordPress search. However, after implementing Algolia search the results were nearly instant:
The Algolia results were about 15x faster than standard WordPress search! That is a massive improvement in performance. Not to mention, if the site supports a large amount of traffic with concurrent searches, it will not cause strain on the server. Offloading searches to an external service that is specifically optimized for searching is a big win.
ElasticPress is a product made by Elastic, which offloads your searching to your own Elasticsearch server instance. This search tool is unique in that you can choose where to host your Elasticsearch instance: your local desktop, dedicated servers, or even on the same server where your WordPress website is hosted. And with the ElasticPress plugin, you can easily connect this instance to your website for use.
ElasticPress + ElasticSearch is a winning combination that provides excellent search features like:
- Including WooCommerce products in search results
- Display related posts without expensive, slow queries
- Choose to index all, some, or only select content on your website
- Automatically complete search suggestions as users type
Learn PHP for free!
Make the leap into server-side programming with a comprehensive cover of PHP & MySQL.
RRP $11.95 Yours absolutely free
Because the search is offloaded to a service optimized for search, the performance impact for sites with many thousands of posts is boosted as well.
The CloudSearch service from Amazon is a viable option in terms of power, performance, and scalability. Amazon uses a “pay-as-you-go” payment service and this makes CloudSearch an inexpensive enterprise-grade solution. You can customize which items get indexed in the setup process as well, but the learning curve for this product is steep. While some like Lift have created WordPress plugins integrating CloudSearch, they are not widely used or often updated, and may not be as reliable. This means that chances are you will be developing your own plugin implementation of Amazon CloudSearch. You can use their Developer Guide for CloudSearch to get started.
Relevanssi is a WordPress plugin that optimizes the native WordPress search “in-house.” That is, it searches your own database on your own server, just with better results. Some of the key benefits this plugin offers, compared to the default WordPress search:
- Fuzzy searches (include partial matches)
- Sort by relevance, not by date
- Select whether the results should match all words, or just one of the words
- Search all content including comments, categories, tags, and custom fields
- Excerpts display and highlight where the match occurred for context
The one aspect Relevanssi does not necessarily improve upon is the speed or scalability of the search function. In this way, Relevanssi is ideal for users who are frustrated with the type of results WordPress search produces. It is not ideal for users who are seeking performance improvements in search.
Last but not least, SearchWP is another paid search solution for WordPress. As with Relevanssi, this plugin extends the search to include better results but makes no claim to improve search performance. SearchWP has extensive configuration options to include exactly what you want in search results. Search things like: eCommerce products, PDFs and Media, Custom Posts and Fields, Categories and Tags, and Shortcode output. Beyond the additional search vectors, SearchWP offers keyword stemming as well. So if your end user searches for a related keyword, they will get content from related keywords in the results.
SearchWP also goes the extra mile in terms of logging searches and providing insights on search traffic. This can help inform your content strategy in ways that make your content easier to find.
Choosing the Right Search Solution
In the end, the search solution you choose will depend upon your budget and upon which problems are the highest priority for your team. If performance gains are your primary concern, Algolia and Elasticsearch are the clear winners. However, if you just want to see better search results, Relevanssi and SearchWP are simple yet powerful options. And if you want a cost-effective way to completely customize the way your website searches in a customized plugin, Amazon CloudSearch could be the right solution. You can use the descriptions above to identify your priorities, and the proper solution to fit that need. As always, be sure to test any new integration or plugin in a staging or development environment first! This will allow you the freedom to test your new search implementation in a non-user-impacting way.
Janna Hilferty is a Content Specialist at WP Engine. She loves both technical and free-form writing, hiking with her dog, and painting with all the colors of the wind. In her free time you can find her blogging, smoking a cigar, or watching cheesy documentaries.
Jump Start Git, 2nd Edition
Visual Studio Code: End-to-End Editing and Debugging Tools for Web Developers