My favorites | Sign in
Project Logo
                
Search
for
Updated Apr 22, 2008 by bubblenut
Labels: Featured
Examples  
Examples on how to use Forage

Forage Examples

This page contains a selection of quick examples on how to use the different features of Forage. For more detail see the specific wiki pages.

Creating a Forage Instance

Al you need to create a Forage instance is DSN. A DSN is made up of an engine identifier and the location of the index seperated by a colon. For example a Solr DSN might look like solr:127.0.0.1:8080; solr is the engine identifier and 127.0.0.1:8080 is the location of the index. Below is an example of how to create a Forage index.

$forage = Forage::create('solr:127.0.0.1:8080');

Indexing A Document

$doc = new ForageDocument();
$doc->add('uid', '123', array('uid'=>true))
    ->add('title', 'This is a test title', array('boost'=>1.5)
    ->add('content', 'lorem ipsum and all that jazz. This is the test content', array('stored'=>false));
$forage->add($doc);

$forage->flush();

Searching

Quick example

$result = $forage->search('my search');
echo "Total Results: " . $result->total . "\n";
foreach ($result as $doc) {
  echo $doc['title'] . ' (' . $doc->getProperty('score') . ")\n";
}

Specifying pagination details

$query = $forage->getQuery('my search');
$query->setPage(5);
$query->setPerPage(10);
$result = $forage->search($query);
// as before

Faceted Search (only supported with Solr engine)

$query = $forage->getQuery('foo');
$query->setFacetFields(array('tag', 'colour'));
$result  = $forage->search($query);
$tags    = $result->getFacet('tag');

echo "Tags associated with 'foo'\n";
foreach ($tags->values as $tag) {
  echo $tag->value . " (" . $tag->count . ")\n";
}

Filtering on a facet

$query = $forage->getQuery('foo');
$query->setFacetFields(array('tag', 'colour'));
$query->addFacetFilter('colour', 'blue');
$query->addFacetFilter('colour', 'green');
$result = $forage->search($query);

Comment by techbirbal, Feb 08, 2008

test


Sign in to add a comment
Hosted by Google Code