Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

User:Nunomorgadinho

   * Nuno Morgadinho (nuno.morgadinho at gmail.com)
   * www.morgadinho.org
   * Writing about integrating WordPress.org with Sphinxsearch.com.
This article is a ROUGH DRAFT. The author is still working on this document, so please do not edit this without the author's permission. The content within this article may not yet be verified or valid. This information is subject to change.

Searching WordPress with Sphinx

This page will try to explain how to integrate WordPress.org with Sphinxsearch.com, a free open-source SQL full-text search engine.

When you search in a WordPress site you are querying the database and searching each entry for a particular string of text. With Sphinx you can index a set of data in memory and use it instead of asking the database.

Sphinx is used in sites like Craigslist, DailyMotion, NetLog and others that have to support tens of millions search queries per day. Sphinx is also used in WordPress.org when you search the plugin directory :)

Installing Sphinx will not be covered since there are quite a few resources explaining that already.

Tools

First you should try one of the plugins that integrates Sphinx into WordPress, e.g. the wp-sphinx-search plugin at http://wordpress.org/extend/plugins/wp-sphinx-search

You will need the sphinxapi.php file that comes bundled with the Sphinx source package.

Custom Fields

To add custom fields to your search you have to include it in the sql query that Sphinx uses. Here is an example with custom fields price and location.

sql_query = select \
		wp_55_posts.*, \
		meta1.meta_value as price_value, \
		meta4.meta_value as location \
from wp_55_posts \
left  join \
	wp_55_postmeta meta1 on (meta1.post_id = wp_55_posts.id AND meta1.meta_key='price') \
left  join \
	wp_55_postmeta meta4 on (meta4.post_id = wp_55_posts.id AND meta4.meta_key='location') \
where  \
	wp_55_posts.post_type = 'your-custom-post-type' AND  \
	wp_55_posts.post_status = 'publish' \
group by \
	wp_55_posts.ID;

You could then use the following attributes:

sql_field_string = post_title
sql_field_string = location
sql_attr_uint = price_value

Searching

You can then search by setting a filter e.g.:

$sphinx->SetFilterRange('price_value', $price_min, $price_max);

Multi-Site Support

One way of supporting multi-site is to have a source for each blog in your sphinx.conf. It is relatively easy to modify sphinx.conf file each time a blog is created to add a new source for it.

Also you should rotate your indexes each time a new post is made.

Resources