Wordpress is a very popular CMS that is used all over the internet for a variety of use cases. Wordpress traditionally uses MySQL for the database and for its search functionality. 

A good alternative is to use Elasticsearch to extend Wordpress’s search functionality. 

In this article, we cover how to install ElasticPress, a plugin that can be used to exploit Elasticsearch’s powerful search capabilities with Wordpress.

For the sake of simplicity, we use docker4wordpress (which you can find here).Docker4wordpress is a project to help you launch a very customizable Wordpress stack quite easily. You could go ahead and set up a vanilla Wordpress install or you could edit the docker-compose.yml file and create a web application stack much more easily. In this article, we use Wordpress + MariaDB + PHP and Elasticsearch as our stack. We begin with a vanilla Wordpress install, and then we move on to adding Elasticsearch to stack.

Step #1 Install Docker for Wordpress

We start by cloning the code from Github:

$ git clone https://github.com/wodby/docker4wordpress

Now, let's navigate to the directory's root and start up docker4wordpress with docker-compose (Please make sure you have Docker and Docker Compose installed.)

$ cd docker4wordpress
$ docker-compose up

You should see the following output after running docker-compose up:

Docker Compose Up


That's great -- but we haven't set up our docker4wordpress for persistence yet. If we don’t specify volumes where MariaDB can store its data, we won't be able to persist data.

Step #2 Configure MariaDB for Persistence

First, let’s create a directory for our volume:

 $ mkdir ~/Desktop/mariadb

Next, let’s take a look at our docker-compose.yml file. We will need to point the directory that we created to the MySQL data directory in our container. We can modify our config file to use the volume. Here is my config if you would like to use it:

MariaDB Config


Now, if we run docker-compose up all the database data will be automatically saved to the disk of the host (the machine where Docker is running). This means that every time you run docker-compose up, you will be able to carry on where you left off in Wordpress because the database operations will be written to the disk. Therefore, you can move data to a new machine along with your docker-compose configurations and just run docker-compose up on the other machine. This is quite handy if you have multiple developers working on the same project because they can set up the project locally with docker-compose using a few commands.

Step #3 Configure Wordpress

Let's now configure our Wordpress instance. First, open your browser and navigate to http://wp.docker.localhost:8000. To make it work, you may need to add 127.0.0.1 wp.docker.localhost to your /etc/hosts file (some browsers like Chrome may work without it). Here, you can find all the subdomains for accessing docker4wordpress on your local computer (see the description of domains in the image below).

Wordpress Domains


At this point, you can start configuring your Wordpress installation. Click next and navigate to the screen displayed in the image below and fill out all the appropriate fields. I would suggest that you jot down the details that you filled in here in a text file or keep it saved in password storage apps like KeePassX.

Install Wordpress


After installing Wordpress, log in with the details you just entered: 

Wordpress Login

Step #4 Adding Elasticsearch to our Stack

We are now ready to add Elasticsearch to the stack. Make sure to comment out the Elasticsearch section in the docker-compose.yml because, by default, Elasticsearch section does not make use of a volume (this makes your data somewhat ephemeral). I added a volume section in the config, which allows us to make use of a volume to store Elasticsearch index data on a disk:

Adding Elasticsearch to the Stack


You can find the full configuration here.

Now, let's run docker-compose up , which will configure your Elasticsearch instance. You should see the following output:

Adding Elasticsearch to Wordpress


After the volume has been attached to Elasticsearch, we are going to install ElasticPress plugin. This plugin integrates Elasticsearch with Wordpress to enhance its search functionality. You can find the plugin the Wordpress store:

ElasticPress Install


Once the plugin is installed, let's activate it like this under the Plugins section of the Dashboard:

Elasticpress Activate


Next, you'll need to configure the Elasticsearch host or Elasticsearch endpoint(s) for the plugin (use the path to your running Elasticsearch instance):

ElasticPress Elasticsearch Endpoint


After this step is completed, you should see the following message: ”ElasticPress is almost ready. You will need to complete a sync to get the plugin working.” After syncing with Elasticsearch, you'll be redirected to the main plugin page:

Elasticpress installed


From here on, getting ElasticPress to work becomes straightforward. To demonstrate how it works, we created two posts for the blog.

Search with ElasticPress


If you type something in the search bar, the ElasticPress will use ES native functionality and search your ES indices for the relevant data:


The search function is really fast with ElasticPress: just type in a keyword and hit enter, and you will almost instantly see the results of your search.

Conclusion

In this guide, we explored how to use Elasticsearch as the search engine for a Wordpress blog, as opposed to using the default built-in search functionality included in Wordpress. We used docker4wordpress project to simplify setting up and configuring PHP, MySQL, Nginx, and Elasticsearch along with the ElasticPress plugin for Wordpress. We hope this helps you integrate Elasticsearch into your Wordpress blog so you can experience the benefits of using Elasticsearch with Wordpress.