Use Elasticsearch as Default WordPress Search

Elasticsearch is an open source search tool that I have been watching for awhile.  My current content management system is WordPress and I was interested to see if I could replace it’s default search with Elasticsearch.  This is overkill – I have roughly 550 documents on this site – but the ability to use faceted search was intriguing.

Fortunately, adding Elasticsearch to WordPress is a quick and relatively painless process.  Mine isn’t quite working yet although, on the surface, it looks fine.

Install Elasticsearch + WordPress Plugin

The instructions I used for this installation are here.  The instructions are short and sweet for Ubuntu; I’m running it on 12.10.  It sets up the Java dependencies and also sets it so that Elasticsearch starts up automatically.

Ubuntu users can then skip down to section 3, where it walks you through the plugin installation.  Like the tutorial, I used Fantastic Elasticsearch, which is easy to configure after you have installed it as a WordPress plugin.  There are two other plugins listed so if you have trouble with Fantastic, you can try those.  It seems a bit counter-intuitive but there really isn’t that much to set up in the plugin.

Cache & Search Results

Nothing is truly that easy.  The installation worked flawlessly, though, so that’s not where my hangups came.  The first thing I noticed was that I had a lot of Content not found results for common keywords.  The results were intermittent, though, and it was hard to tell what the problem was.

I use Varnish for my overall site cache, as well as a WordPress cache plugin that integrates with Varnish.  My site runs behind Cloudflare as well, so I had a number of moving parts that could be causing the problem.  I ended up clearing my local caches but that didn’t have any obvious impact.  I also turned off Cloudflare’s Rocket Loader(tm), an aggressive Javascript loader that appeared to break my blog’s pagination.  Whether it was a direct cause or not, the Elasticsearch results started to appear.

Skinning the Facets

Fantastic Elasticsearch creates some widgets to display along side your search results.  Faceting Options appears to display the facets.  Just dropping it into a WordPress widget container (you’ll find your widgets under Appearance) activates a unordered list of checkboxes.  It separates out pages and posts, and can also show a cluster of categories and a cluster of keywords/tags.

Faceting Selections is optional, showing your search terms.  I have only just turned this on and it doesn’t seem to be necessary, other than to display the search terms.  But it allows you to chop the search and enable the searcher to select just elements.

Fantastic Search's Faceting Options widget causes the facets to appear in the widget container, shown here at the top left.

Fantastic Search’s Faceting Options widget causes the facets to appear in the widget container, shown here at the top left.

It is easy to theme the facets.  The basic style I applied, through the Edit CSS option under the WordPress appearance menu was to bring the results into a continuous line, rather than a long list:

.facet-item {
    display: inline;
}

I haven’t spent much time skinning the results beyond this.  Because of the placement of facets on my sidebar, one thing I may eventually try is to theme the boxes containing the facets to make them stand out – and more obvious that they’ve suddenly appeared – from the rest of the page.

All in all, it’s been a fair experiment.  I can’t say that the results are better than they were with the out-of-the-box search but I like seeing the facets come up.  Unfortunately, while you can select the facets, it doesn’t force the search to reload with just your results.  It looks like the plugin project wiki has more detail but I haven’t gotten around to figuring out how to get it working.