ElasticSearch - PageSpeedPlus/easyengine-2 GitHub Wiki

ElasticSearch

Install + Configure Elasticsearch to Speed up WooCommerce Search

Elasticsearch is written in Java so we need to install that first. 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 Oracle’s 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 WordPress

Back up the default Elasticsearch configuration

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

Create a fresh Elasticsearch configuration for WordPress.

sudo nano /etc/elasticsearch/elasticsearch.yml

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

If you are using Elasticsearch on a separate server I’d recommend using nginx as a reverse proxy for SSL so your post data is encrypted.

cluster.name: wordpress
node.name: wpboost
network.host: 127.0.0.1
http.port: 9200
node.max_local_storage_nodes: 1

Restart the Elasticsearch service to enable the configuration

sudo service elasticsearch restart

Let’s make sure Elasticsearch is running on the loopback interface (127.0.0.1), note that it can take up to 10 seconds or so for the Elasticsearch daemon to fully start up.

sudo netstat -lntp

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

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

Let’s make sure Elasticsearch is using the configuration we just made

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

You’ll see this output showing the name of the node and the cluster name.

{
  "name" : "wordpress",
  "cluster_name" : "wpboost",
  "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 WordPress to use ElasticSearch

  1. Disable any object caching like memcached or Redis – you could get php loops which crash your server!
  2. My forked version of Memcached is your friend will not cause any php loops (source).
  3. To enable Elasticsearch for WordPress, install ElasticPress (hosted on github) made by 10up.
  4. After enabling ElasticPress you can create the Elasticsearch index by going to the ElasticPress and clicking the Gear icon.
  5. Set the Elasticsearch Host to http://127.0.0.1:9200 and click Save Changes.6. Click ElasticPress in the Admin sidebar again to see the Elasticsearch integration options.
  6. I recommend Activating Search and Admin.
  7. The Related posts will add a small widget at the bottom of your posts.
  8. To start the Elasticsearch index process click the Refresh icon in the top right.
  9. 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 WordPress installation folder and use this command to create the Elasticsearch index.

cd /var/www/wp-bullet
wp elasticpress index --setup

On a DigitalOcean VPS it took about 10 minutes for ElasticPress to index 78000 posts.

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 and pages. You do not need to re-index periodically or anything like that to keep your Elasticsearch index updated.