Full-text search in WordPress with MySQL MATCH AGAINST
The built-in WordPress search runs LIKE '%word%' on the title, excerpt and content of every post. That reads the whole table for each search and sorts the results only roughly by relevance. On a site with tens of thousands of posts, a MySQL FULLTEXT index answers the same question in milliseconds and ranks the results by how well they match.
Problem: On a blog with 15,000 posts, a search takes four seconds, and the best matching post is often on the second page of results.
Solution: Add a FULLTEXT index on post_title and post_content, replace the search condition of the main query in the posts_search filter with MATCH ... AGAINST, and sort by the relevance score in posts_orderby.
The examples use the wp_ prefix.
The index
ALTER TABLE wp_posts ADD FULLTEXT INDEX ha_search (post_title, post_content);
SELECT ID, post_title,
MATCH (post_title, post_content) AGAINST ('redis object cache') AS score
FROM wp_posts
WHERE post_type = 'post' AND post_status = 'publish'
AND MATCH (post_title, post_content) AGAINST ('redis object cache')
ORDER BY score DESC
LIMIT 10;
InnoDB supports full-text indexes since MySQL 5.6 and MariaDB 10.0.5. Building the index on a large table takes a while and should be done after a backup and outside peak hours. The MATCH columns must be exactly the columns of an index.
Use it for the WordPress search
add_filter( 'posts_search', function ( $search, WP_Query $query ) {
if ( ! $query->is_main_query() || ! $query->is_search() || is_admin() ) {
return $search;
}
global $wpdb;
$terms = $query->get( 's' );
if ( '' === trim( $terms ) ) {
return $search;
}
$query->set( 'ha_fulltext', $terms );
return $wpdb->prepare( " AND MATCH ({$wpdb->posts}.post_title, {$wpdb->posts}.post_content) AGAINST (%s IN NATURAL LANGUAGE MODE) ", $terms );
}, 10, 2 );
add_filter( 'posts_orderby', function ( $orderby, WP_Query $query ) {
$terms = $query->get( 'ha_fulltext' );
if ( ! $terms ) {
return $orderby;
}
global $wpdb;
return $wpdb->prepare( "MATCH ({$wpdb->posts}.post_title, {$wpdb->posts}.post_content) AGAINST (%s IN NATURAL LANGUAGE MODE) DESC", $terms );
}, 10, 2 );
The search string goes through $wpdb->prepare(), so quotes in it are harmless. Post type and status conditions of the query stay as they are; only the LIKE part is replaced.
Things to know
- Words shorter than the minimum length are ignored: 3 characters for InnoDB (
innodb_ft_min_token_size), and common English stopwords such as “the” are skipped. - The relevance score depends on how rare a word is in the whole table, so results on a small test database look different from production; test with a copy of the real content.
IN BOOLEAN MODEallows operators such as+redis -memcachedand prefix search withcache*, but it does not sort by relevance as well.- The index covers only the post table. Searching custom fields needs a different approach: search in custom fields.
NOTE: The index is not part of a standard WordPress install. Without it, MATCH fails with an SQL error and the search returns nothing, so the index can get lost silently when the database is rebuilt from an export of another site or a fresh install. Create it on plugin activation (check SHOW INDEX FROM wp_posts WHERE Key_name = 'ha_search' first), or keep the filters disabled until the index exists.