Integrating Elasticsearch with WordPress using ElasticPress
If you've ever used Elasticsearch for managing your searchable content, you know that it is incredibly fast and scalable. If you're a WordPress user, you likely also know that obtaining WP_Query objects can sometimes have a negaitve impact on your resource usage if you have a large database of center to search from. Thankfully, 10up has developed a solution in which the WP_Query object can use Elasticsearch instead of the default behavior, which is much easier on your server resources.
In this article, we will show you how to integerate WordPress with Elasticsearch using ElasticPress by 10up.
Note: This article assumes that you have already installed Elasticsearch. If you have not already done so, you may review our article on installing Elasticsearch.
This ElasticPress also requires WP-CLI. If you don't already have it installed, you may follow our article on installing WP-CLI.
First, you will need to edit your wp-config.php file to include the location of your Elasticsearch server. In this example, Elasticsearch is on the same server as our WordPress site, so we add the following:
define( 'EP_HOST', 'http://127.0.0.1:9200' );
If you need help doing so, you may review our article on editing your wp-config.php file.
Next, you will need to install the ElasticPress plugin. The ElasticPress plugin is available both within the WordPress plugin directory, or within GitHub. If you want the most updated version, we recommend GitHub.
If you need help, you may review our article on installing the ElasticPress plugin.
Of course, Elasticsearch isn't doing us any good if we don't have any data populated within it. To begin, log into your server via SSH and execute one of the following:
For single WordPress sites
wp elasticpress index --setup
For cross-site search on multisite
wp elasticpress index --setup --network-wide
ElasticPress is now running on your site and all searches are now WP_Query is now handling searches with Elasticsearch instead of running MySQL queries for your data. For more information on ElasticPress, check out the ElasticPress GitHub page.