Searching for a Better WordPress Search

Follow Along

v.gd/wpsearch

What This Talk Will Cover

  • What’s Wrong with Search
  • Plugins & Services to Improve Search
  • Q&A

About Me

Russell Heimlich on a pogo stick in a suit

I’ve been playing with WordPress since 2007

Screenshot of Russell Heimlich's first blog post

Lead Developer at BillyPenn.com

Billy Penn

I joined Twitter before @twitter Joined Twitter

@kingkool68 joined Twitter in December 2006, @twitter joined February 2007

When I’m not coding…

Zadie wearing a WordPress onesie

What’s Wrong with Search?

Common Complaints of WordPress’ Search

  • Slow
  • No options/customizations
  • Irrelevant results

Search is Hard!

  • Look through all posts in the database
  • Find posts matching keyword(s)
  • Determine most relevant results
  • Output results via a template
  • Do all of this in under a few seconds!

WordPress is designed for broad compatibility

  • “It just works” under all kinds of server configurations
  • Commodity hardware can be slow/constrained
  • MySQL is not a search engine

WordPress is not a search engine!

Bottom line: WordPress is not a search engine. If you need a real search engine, use one and use a plugin to enable it. Default search is going to be default, always. Basic. Works everywhere. Lowest common denominator.

How Does a WordPress Search Work?

Pre 3.7

Search for “term”

SELECT SQL_CALC_FOUND_ROWS wp_posts.id
FROM   wp_posts
WHERE  1 = 1
       AND (( ( wp_posts.post_title LIKE '%term%' )
               OR ( wp_posts.post_content LIKE '%term%' ) ))
       AND wp_posts.post_type IN ( 'post', 'page', 'attachment' )
       AND ( wp_posts.post_status = 'publish'
              OR wp_posts.post_author = 1
                 AND wp_posts.post_status = 'private' )
ORDER  BY wp_posts.post_date DESC
LIMIT  0, 10 
  1. Find posts where the post title or the post content contains “term”
  2. Sort the results by post date in descending order (reverse chronological)

Search for “search term”

SELECT SQL_CALC_FOUND_ROWS wp_posts.id
FROM   wp_posts
WHERE  1 = 1
       AND ( ( ( wp_posts.post_title LIKE '%search%' )
                OR ( wp_posts.post_content LIKE '%search%' ) )
             AND ( ( wp_posts.post_title LIKE '%term%' )
                    OR ( wp_posts.post_content LIKE '%term%' ) ) )
       AND wp_posts.post_type IN ( 'post', 'page', 'attachment' )
       AND ( wp_posts.post_status = 'publish'
              OR wp_posts.post_author = 1
                 AND wp_posts.post_status = 'private' )
ORDER  BY wp_posts.post_date DESC
LIMIT  0, 10 
  1. Find posts where…
    1. the post title or the post content contains “search”
    2. AND the post title or the post content contains “term”
  2. Sort the results by post date in descending order (reverse chronological)

Trac Ticket #7394

Screenshot of Trac ticket #7394 titled Search: order results by relevance

Sorting Search by Relevance

A long time in the making…

  • Opened July 24, 2008
  • Closed October 2, 2013
  • 5 Years, 2 months, 8 days!
Screenshot of the New Post admin interface for WordPress version 2.6

From WordPress 3.7 and later searches are sorted by relevancy by default

(except for feeds)

The ordering logic is as follows:

via Changeset 25632

  • Full sentence matches in post titles
  • All search terms in post titles
  • Any search terms in post titles
  • Full sentence matches in post content

Search for “term”

SELECT SQL_CALC_FOUND_ROWS wp_3_posts.id
FROM   wp_3_posts
WHERE  1 = 1
       AND (( ( wp_3_posts.post_title LIKE '%term%' )
               OR ( wp_3_posts.post_content LIKE '%term%' ) ))
       AND wp_3_posts.post_type IN ( 'post', 'page', 'attachment' )
       AND ( wp_3_posts.post_status = 'publish'
              OR wp_3_posts.post_status = 'private' )
ORDER  BY wp_3_posts.post_title LIKE '%term%' DESC,
          wp_3_posts.post_date DESC
LIMIT  0, 10 
  1. Find posts where the post title or the post content contains “term”
  2. Sort the results by…
    1. posts where the post tile contains “term”
    2. post date in descending order (reverse chronological)

Search for “search term”

SELECT SQL_CALC_FOUND_ROWS wp_3_posts.id
FROM   wp_3_posts
WHERE  1 = 1
       AND ( ( ( wp_3_posts.post_title LIKE '%search%' )
                OR ( wp_3_posts.post_content LIKE '%search%' ) )
             AND ( ( wp_3_posts.post_title LIKE '%term%' )
                    OR ( wp_3_posts.post_content LIKE '%term%' ) ) )
       AND wp_3_posts.post_type IN ( 'post', 'page', 'attachment' )
       AND ( wp_3_posts.post_status = 'publish'
              OR wp_3_posts.post_status = 'private' )
ORDER  BY ( CASE
              WHEN wp_3_posts.post_title LIKE '%search term%' THEN 1
              WHEN wp_3_posts.post_title LIKE '%search%'
                   AND wp_3_posts.post_title LIKE '%term%' THEN 2
              WHEN wp_3_posts.post_title LIKE '%search%'
                    OR wp_3_posts.post_title LIKE '%term%' THEN 3
              WHEN wp_3_posts.post_content LIKE '%search term%' THEN 4
              ELSE 5
            end ),
          wp_3_posts.post_date DESC
LIMIT  0, 10 
  1. Find posts where…
    1. the post title or the post content contains “search”
    2. AND the post title or the post content contains “term”
  2. Sort the results by…
    1. posts that contain “search term” in the post title
    2. posts that contain “search” AND “term” in the post title
    3. posts that contain “search” OR “term” in the post title
    4. posts that contain “search term” in the post content
    5. post date in descending order (reverse chronological)

GIF Break

Plugins

Screenshot of Relevannsi plugin page

Pros

  • Search custom post types, terms, fields
  • Lots of options to tweak
  • Search query logging
  • Free and Premium versions

Cons

  • Extra load on your server
  • Performance issues with larger amounts of content
  • Hard to remember how to spell
Screenshot of SearchWP website

Pros

  • Native to WordPress
  • Configurable through wp-admin
  • Search stats
  • bbPress integration
  • PDF Indexing
  • Multiple Search Engines

Cons

  • Extra load on your server
Screenshot of Google custom search engine plugin page

Pros

  • Integrates with your theme
  • Index is built through crawling
  • Multisite support
  • Ad free

Cons

  • Pricing
  • Cap on queries per year
  • Google’s Developer Console is confusing
Pricing breakdown of Google Custom Search: $100/year for 20,000 queries through $2,000/year for 500,000 queries
Screenshot of Swiftype plugin page

Pros

  • Drag and drop to reorder results
  • PDF indexing
  • Faceted Search
  • Analytics
  • Developer API
  • Two ways to build search index: web crawler or API
  • No hardware to manage

Cons

  • Pricing ($249/month or $2,988/year)
  • Top-query report is enterprise only
A yellow rack server that looks like a piece of cheese

Pros

  • Your own mini-Google
  • Self-learning Ranking
  • XML version of results
  • OneBox support (i.e. “weather 90210” returns weather results )
  • Search behind a firewall or authentication
  • Index 220 content types (PDF, Microsoft Office, databases)

Cons

  • “If you have to ask you probably can’t afford it” pricing
  • Manage Google-provided hardware
  • Highly technical
  • No WordPress plugin
  • End of life 2017

Screw It! Just Use Google

Searches on my blog redirect to Google

Pros

  • Easy
  • Free (GitHub Repo)
  • No maintenance
  • Just as good as Google

Cons

  • No customizations
  • Linking off your site
  • Not professional?

Except mobile.reuters.com did this!

Screnshot of mobile.reuters.com
Screenshot of ElasticPress plugin page

Pros

  • Super Flexible
  • Integrates with WP_Query
  • Faceted Search
  • Multisite Support
  • Proximity/Geograpic Queries
  • Free (plugin & Elasticsearch software)
  • Open Source

Cons

  • WP-CLI required
  • Server management required
  • Highly technical
  • Aimed at developers

Setting up Elasticsearch & ElasticPress

Set-up Elasticsearch

* Don’t forget to secure Elasticsearch

Install Plugin and WP-CLI

Define the address of your Elasticsearch instance

Add the following to wp-config.php with the IP address (and port) of your Elasticsearch instance:

define( 'EP_HOST', 'http://192.168.1.2:9200' );

Start the initial sync

Single site

wp elasticpress index --setup

Multisite

wp elasticpress index --setup --network-wide

Or go through the WordPress Admin

ElasticPress WordPress Admin interface

Additional ElasticPress Integrations

Which one is right for me?

Small Scale/Low Budget

Medium Scale/Small Business

Large Scale/Enterprise

Heavy Customization

Wrapping up!

Search is hard

WordPress is not a search engine

But there are lots of ways to enhance your site’s search

Thank You!

Tweet me! @kingkool68

Questions?