Install + Configure Elasticsearch to Speed up WooCommerce Search

WooCommerce often requires a powerful server to run smoothly. For anybody who has a large store with thousands of products or orders, you may have experienced a slow WooCommerce backend in the wp-admin area when doing management tasks.

One way to fix slow WooCommerce product or order search is to use the power of Elasticsearch. Elasticsearch is a NoSQL database which uses Apache lucene structure. It is designed to retrieve results in real time and is geared for performance. Elasticsearch can take over certain searches so that MySQL is used much less giving you faster search results.

WordPress.com uses Elasticsearch extensively, for VIP and regular users. Luckily Elasticsearch is open source hosted on github so we can install it for free on our own dedicated server or VPS.

I have seen WooCommerce order search go from 14 seconds to 3 seconds after implementing Elasticsearch with ElasticPress and ElasticPress WooCommerce. The results and benefits are real and awesome. You will need root access to your server in order to set this up. If you need help migrating to a VPS or dedicated server then get in touch. Alternatively you can pay Elastic to host the Elasticsearch server for you.

Install + Configure Elasticsearch to Speed up WooCommerce Search

Install Java otherwise Elasticsearch won't run. This is for Debian and Ubuntu.

echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" | sudo tee -a /etc/apt/sources.list.d/webupd8team-java.list
echo "deb-src http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main" | sudo tee -a /etc/apt/sources.list.d/webupd8team-java.list
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EEA14886
sudo apt-get update
sudo apt-get install oracle-java8-installer -y

Accept the terms when prompted.

If you have problems with the Oracle package then use openjdk

sudo apt-get install openjdk-8-jdk -y

Install Elasticsearch

Grab the Elasticsearch repository key, add the repository, update and install Elasticsearch

sudo apt-get install apt-transport-https -y
wget -qO - https://packages.elastic.co/GPG-KEY-elasticsearch | sudo apt-key add -
echo "deb https://packages.elastic.co/elasticsearch/2.x/debian stable main" | sudo tee -a /etc/apt/sources.list.d/elasticsearch-2.x.list
sudo apt-get update
sudo apt-get install elasticsearch -y

Enable the Elasticsearch startup service so it will autostart on boot.

sudo update-rc.d elasticsearch defaults 95 10

Configure Elasticsearch for WooCommerce

Back up the current Elasticsearch configuration

sudo mv /etc/elasticsearch/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml.bak

Create a fresh Elasticsearch configuration for WooCommerce.

sudo nano /etc/elasticsearch/elasticsearch.yml

This is a simple Elasticsearch configuration for a single node cluster that listens on the loopback interface

cluster.name: woocommerce
node.name: wpbullet
network.host: 127.0.0.1
http.port: 9200
node.max_local_storage_nodes: 1

Restart Elasticsearch to enable the configuration

sudo service elasticsearch restart

Check the listening ports to see if Elasticsearch is running – it can take 10 seconds or so for it to show up.

sudo netstat -lntp

You should see a java program (Elasticsearch) listening on port 9200

tcp        0      0 127.0.0.1:9200          :::*                    LISTEN      10428/java
tcp        0      0 127.0.0.1:9300          :::*                    LISTEN      10428/java
tcp6       0      0 127.0.0.1:9200          :::*                    LISTEN      10428/java
tcp6       0      0 127.0.0.1:9300          :::*                    LISTEN      10428/java

Can test if Elasticsearch is using the configuration we just made

curl -X GET http://localhost:9200/

You'll see this output

{
  "name" : "wpbullet",
  "cluster_name" : "woocommerce",
  "version" : {
    "number" : "2.3.3",
    "build_hash" : "218bdf10790eef486ff2c41a3df5cfa32dadcfde",
    "build_timestamp" : "2016-05-17T15:40:04Z",
    "build_snapshot" : false,
    "lucene_version" : "5.5.0"
  },
  "tagline" : "You Know, for Search"
}

Configure WooCommerce to use ElasticSearch

Disable any object caching you may have set up or you could get php loops which crash your server!

We are going to install ElasticPress and ElasticPress WooCommerce (no longer required)

elasticpress-elasticpress-woocommerce-1400

Install ElasticPress (hosted on github by 10up) which enables Elasticsearch for WordPress.

elasticpress-banner

Note that ElasticPress 2.1 will include ElasticPress WooCommerce! (source)

ElasticPress WooCommerce (also on github by 10up) to integrate Elasticsearch used to be necessary to integrate WooCommerce with Elasticsearch.

elasticpress-woocommerce

After enabling ElasticPress you can create the Elasticsearch index by going to the ElasticPress and clicking the Gear icon.

Set the Elasticsearch Host to http://127.0.0.1:9200 and click Save Changes.

Click ElasticPress in the Admin sidebar again to see the Elasticsearch integration options.

I recommend Activating Search and Admin.

You will need to Activate WooCommerce here.

Now click the Refresh icon in the top right to start indexing.

If you get any timeouts or errors when running the index then use WP-CLI instead, you can install WP-CLI easily

sudo wget -q https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -O /usr/bin/wp
sudo chmod 755 /usr/bin/wp

Then enter your WooCommerce installation folder

cd /var/www/wp-bullet

You can define the ElasticPress Elasticsearch host in wp-config.php

// ElasticPress Host
define( 'EP_HOST', 'http://127.0.0.1:9200' );

Use this command to create the WooCommerce Elasticsearch index.

wp elasticpress index --setup --allow-root

It took about 10 minutes to index 9 products with 70,000 orders on a DigitalOcean VPS.

Processed 77000/78364 entries. . .
Processed 77350/78364 entries. . .
Processed 77700/78364 entries. . .
Processed 78050/78364 entries. . .
Processed 78364/78364 entries. . .
Number of posts indexed on site 1: 78364
Total time elapsed: 756.411
Warning: ElasticPress is already activated.
Success: Done!

After you run the initial index, Elasticsearch will be updated automatically when you make changes to posts, pages, products. You do not need to re-index periodically or anything like that.

Sources

Elasticsearch Repositories
Improving WooCommerce Search