Using ElasticSearch with WordPress

I’m a big fan of stuff that’s cool. And ElasticSearch is really cool…

I’m not going to explain ElasticSearch in any detail, that’s not my job, other better people have already done that. But I am going to discuss my experience working with ElasticSearch and WordPress on a couple of recent projects.

Earlier this year I started working on an analytics platform. It’s still in very experimental stages (meaning I have no idea what I’m building), but we have lots of data in multiple locations and we needed a central way to process and understand it.

We already had a very basic system that almost did what we wanted that used MySQL as the central data store, but it was old and not the complete picture. It only looked at one data source, whereas we needed it to look at multiple.

Enter ElasticSearch. Until then I didn’t have much experience with it beyond some videos and online blog posts, but I wanted to give it a try anyway. I managed to set up a single instance running locally on a Linux server and feed in some data using PHP.

I indexed a bunch of stuff and managed to create some cool looking graphs with Kibana.

I’ve got the PHP code sitting here on my laptop, so I’ll upload it to github soon and post a link.

For the next project, I have a site that is built (for various reasons) using WordPress multi-site. It’s actually a “single” site to the outside world, but trust me when I say, there was a legitimate reason to build it as a multi-site.

As it’s a single site from the outside, I needed to have an integrated search functionality that worked across both sub-sites and multiple custom post types. There’s a lot of different data in there that visitors needed to find easily and WordPress search just wasn’t working the way we needed it to.

This time I decided to try AWS ElasticSearch rather than set up a local server. On the WordPress side I installed the plugin ElasticPress and activated network-wide. It was as easy as pasting the ElasticSearch endpoint into the plugin settings and the plugin did all the hard work. Within 5 minutes I had all of the data indexed and search worked brilliantly.

There was one small problem, some of the custom post types had ‘exclude_from_search’ set to false even though I didn’t want them to be searchable. Finding private data in search results is not ideal. I changed the CPT settings to true and reindexed and all was good with the world.

See also