Development

Using Elasticsearch on Your Local Dev Environment

Using Elasticsearch (ES) on your site can help boost performance, especially with complex search setups. When using Elasticsearch on your local development environment, it can be tricky, because the names of indices are based on URL and can be the same as another dev’s; and you don’t want to overwrite the data of your team members or production. For local development, the best solution is to use your own ES instance.

Requirements:

The steps in this setup assume you’re using 10up’s ElasticPress plugin to integrate WordPress with ES. This plugin includes a ton of useful hooks to index posts when creating/updating/deleting, as well as some awesome helper functions to help with the process.

You will also need Vagrant, Virtualbox, and WP CLI installed.

Clone this GitHub repository and add define( ‘EP_HOST’, ‘http://10.0.0.11:9200’ ); to your project’s wp-config.php.

Indexing:

Now that your Elasticsearch VM is ready, you can begin the initial indexing of posts. If you’re using VVV, you’ll need to vagrant ssh first. Then `cd` into the root of your project directory (ie: where wp-config.php lives).

If you’re on a single site, the following command will create the index and index posts on the site: `wp elasticpress index –setup`

If you’re on a small mutisite network, you can simply run `wp elasticpress index –setup –network-wide` to combine the steps of creating all indices for sites, indexing posts on the site, and creating the network alias.

Sometimes though, the site is so large that this could take hours when all you really need is one site to be indexed. Using the commands outlined below will allow you to control creating the indices, network alias, and indexing sites separately.

  1. Create the indices for each site on the network wp elasticpress put-mapping --network-wide
  2. Index the site(s) you need on the network wp elasticpress index --url=<url>
  3. Create the network alias which will allow all indices to be searched on the network wp elasticpress recreate-network-alias

As stated at the beginning of this blog post, using Elasticsearch on your WordPress site is a good way to boost performance. Using the production Elasticsearch cluster for development can cause an undesired increase in indices or data can be overwritten, if developers are using the same localhost URL. However, by using an Elasticsearch Vagrant machine to develop locally, developers can better control their own Elasticsearch machine, instead of having to use the production cluster.

For more on this topic, read this blog post on why you should use Elasticsearch on your WordPress site.

Comments

Have a comment?

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

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