WordPress Search is Rudimentary • Only searches post title, content, and excerpt. • Relies on MySQL and thus is slow. • Relevancy calculations are poor and overly simplistic. • Not able to handle any advanced filtering.
I Want to…. • Search as follows • Search post content, title, excerpt, and public taxonomy terms against the users search terms • Only return posts that have a specific meta key/value association
Elasticsearch • Open-source search server written in Java based on a technology called Lucene (open- source search software by Apache). • A standalone database server that provides a RESTful interface to accept and store data in a way that is optimized for search. • Extremely scalable, performant, and reliable
Elasticsearch • Relevant results • Autosuggest • Fuzzy matching • Geographic searches • Filterable searches • Data weighting • Much more
Get an Elasticsearch Server • Very flexible and customizable. There is not really a “one size fits all” setup. Generally, you have two options: • Option 1: Pay someone else to manage/host your Elasticsearch cluster (SaaS) • Option 2: Host your own cluster
Elasticsearch Self-Hosted Cluster • Make sure you have Java installed. • Download Elasticsearch: https://www.elastic.co/downloads/elasticsearch • Assuming you are installing on a Linux distribution, you can easily install using DEB or RPM packages.
Configuring Your Cluster For the purposes of this talk, we won’t go into Elasticsearch installation or configuration.
ElasticPress A free 10up WordPress plugin that powers WordPress search with Elasticsearch.
ElasticPress • Build filterable performant queries (filter by taxonomy term, post meta key, etc.) • Fuzzy search post title, content, excerpt, taxonomy terms, post meta, and authors • Search across multiple blogs in a multi-site instance • Results returned by relevancy. Relevancy calculations are highly customizable. • Very extensible and performant
Point to Your ES Instance • In your wp-config.php file, add the following where the URL is the domain or IP of your Elasticsearch instance:
define( 'EP_HOST', 'http://192.168.50.4:9200' );
Index Your Posts • Just activating the plugin will do nothing. We need to run a WP-CLI command:
wp elasticpress index --setup [--network-wide] • --network-wide will force indexing across all the blogs on a network of sites in multisite. This is required if you plan to do cross-site search. It’s basically a shortcut so you don’t have to run the command once with the --url parameter for each of your blogs.
What Happens Next? • Once ElasticPress is activated and posts are indexed, it will integrate with WP_Query to run queries against Elasticsearch instead of MySQL. • WP_Query integration will only happen on search queries (“s” parameter) or when the ep_integrate parameter is passed.
Note on Search • We can use ElasticPress to power queries OTHER than search! This is powerful.
Advanced Queries • ElasticPress uses Elasticsearch to do many cool types of queries in a performant manner. By passing special params to a WP_Query object we can: • Search taxonomy terms • Filter by taxonomy terms (unlimited dimensions) • Search post meta • Filter by post meta (unlimited dimensions) • Search authors • Filter by authors • Search across blogs in multisite • more!
Example Queries new WP_Query( array( ’s’ => ‘miami florida’, ‘sites’ => ‘all’, ) );
Example Queries new WP_Query( array( ‘ep_integrate’ => true, ‘tax_query’ => array( array( This would require 3 inner joins ‘taxonomy’ => ‘category’, ‘terms’ => array( ‘category1’ ), in MySQL plus extra queries to ), determine term IDs! array( ‘taxonomy’ => ‘post_tag’, ‘terms’ => array( ‘tag1’, ‘tag2’ ), ), array( ‘taxonomy’ => ‘custom_taxonomy’, ‘terms’ => array( ‘term1’, ‘term2’ ), ), ) ) );
Example Queries new WP_Query( array( ‘ep_integrate’ => true, ‘meta_query’ => array( array( ‘key’ => ‘city’, ‘value’ => ‘Miami’, ), This would require 3 inner joins array( ‘key’ => ‘price’, in MySQL! ‘value’ => ‘100’, 'compare' => '>', ), array( ‘key’ => ‘_thumbnail_id’, ‘value’ => 1, ), ) ) );
WP_Query Integration • The goal for ElasticPress is to make WP_Query work behind the scenes through Elasticsearch and provide extra functionality. • This means we have to support every query parameter which isn’t the case yet. Github contains a full list of parameters WP_Query supports with ElasticPress and usage for each parameter.
Elasticsearch in Your Language • Elasticsearch is designed to be internationalized. • Out of the box, it will work fine with most languages.
Analysis and Analyzers • When a document is indexed in Elasticsearch, text is analyzed, broken into terms (tokenized), and normalized with token filters. • In normalization, strings might be lowercased and plurals stripped.
Custom Analyzers • ElasticPress by default uses a pretty standard set of analyzers intended for the English language. • We can easily customize our analyzers for use with other languages by filtering ep_config_mapping (see EP source code). • You can read about language specific analyzers here: