Faceted Search With Forage
15th February 2008
Today I added support for faceted search to Forage. Faceted search is a way of drilling down into search results by filtering on particular fields or categories. The example below is relatively simple in that it only has one facet, category, but there is no reason why you can't have multiple different 'facets'. This is, in fact, done quite often and very successfully in product searches.
And on to the example
This example is an extension of the previous example, only this time we'll be using one of the BBC's news feeds. The reason we're going to use BBC's new feeds this time is that they have an extra element in the feed, category. We're going to use this category as a facet field and then list the most popular categories in the feed.
require_once dirname(__FILE__) . '/../lib/Forage.php'; require_once 'Zend/Feed.php'; $feed = Zend_Feed::import('http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml'); // we're now using Solr as the engine, more about this later... $forage = Forage::create('solr:127.0.0.1:8080'); foreach ($feed as $item) { $document = new ForageDocument(); $document->add('title', (string)$item->title()) ->add('link', (string)$item->link(), array('indexed'=>false)) ->add('description', (string)$item->content(), array('stored'=>false)) // we mark the facet field as such at index time ->add('category', (string)$item->category(), array('facet'=>true)); $forage->add($document); } $forage->flush(); // create an empty query and tell it that we want to // facet on 'category' before searching $query = $forage->getQuery(); $query->setFacetFields(array('category')); $response = $forage->search($query); echo "Total Results: " . $response->getTotal() . "\n"; // get the category facet from the response $facet = $response->getFacet('category'); $i=0; // loop over the category values showing the top three // along with the number of documents it apears in. foreach ($facet->values as $value) { if ($i++>3) { break; } echo $value->value . " (" . $value->count . ")\n"; }
At the moment it's only supported by the Solr engine but Xapian will be adding faceting support in version 1.1. You can check the code out of subversion and more detailed documentation is in the wiki.

Leave a reply