Haystack: an IndexTank client for CodeIgniter 2.0+

IndexTank

Update: The IndexTank service is no longer in operation. They were bought by LinkedIn and have closed their API and services to all existing and new customers. However, the good folks at IndexTank have open sourced their entire platform, and several great companies have been created to continue to service IndexTank customers. They are all completely compatible with my Haystack library as well as any existing IndexTank code you have written.

Take a moment to check out these great new companies:

The IndexTank API is a hosted search solution used by some of the largest sites on the web such as Reddit, Twitvid and ZenCoder. It offers some cool features such as Geolocation, fuzzy search, autocomplete, “Did you mean” spelling correction, stemming and most importantly, real-time indexing.

It’s a pretty cool service, and did I mention that it’s free to index up to 100,000 documents?

The only problem is that there wasn’t a PHP client available for the CodeIgniter framework, until now.

Haystack is a PHP client offering basic interaction with the IndexTank API, implemented as a CodeIgniter library. It is very much a work in progress, and as I wrote it primarily for use in my current project, it is not as general as it could be. I will be maintaining the library on Github and will continue to add new functionality and improvements as time allows.

You can download the library from my Github repository.

Basic Usage

class Search extends CI_Controller
{
     $this->load->library('Haystack');
 
     // Create a new index
     $this->haystack->create_index('cars');
 
     // Set the index to work with for the next one or more IndexTank API calls
     $this->haystack->set_index('cars');
 
 
     // Add a single document to the currently selected index
     $docid = 1;
     $fields = array(
          'text' => 'The Chevrolet Impala is a full-size automobile built by the Chevrolet division of General Motors introduced for the 1958 model year.',
     );
 
     $this->haystack->add_document($docid, $fields);
 
 
     // Add multiple documents to the currently selected index
     $docs = array(
          array(
               'docid' => 1,
               'fields' => array(
                    'text' => 'The Chevrolet Impala is a full-size automobile built by the Chevrolet division of General Motors introduced for the 1958 model year.'
               )
          ),
          array(
               'docid' => 2,
               'fields' => array(
                    'text' => 'The Ford GT is a mid-engine two-seater sports car. Ford Motor Company produced the Ford GT for the 2005 to 2006 model years. The designers drew inspiration from Ford\'s GT40 race cars of the 1960s.'
               )
          )
     );
 
     $this->haystack->add_documents($docs);
 
 
     // Delete a single document from the currently selected index
     $this->haystack->delete_document(1);
 
 
     // Delete multiple documents from the currently selected index
     $docids = array(1, 2, 3, 10);
     $this->haystack->delete_documents($docids);
 
 
     // Select a different search index to work with and add a new document
     $this->haystack->set_index('trucks');
     $docid = 12;
     $fields = array(
          'text' => 'Bigfoot, introduced in 1979, is regarded as the original monster truck. Other trucks with the name "Bigfoot" have been introduced in the years since, and it remains the most well-known monster truck moniker in the United States.'
     );
 
     $this->haystack->add_document($docid, $fields);
 
 
     // Delete an entire search index and all of its documents
     $this->haystack->delete_index('vans');
 
 
     // Search in the currently selected index
     $terms = "monster truck";
     $results = $this->haystack->search($terms);
 
     if($results->matches == 0)
     {
          // No results found
     }
     else
     {
          // Results found. Loop through them and print the id.
          foreach($results->results as $doc)
          {
               echo "Found document {$doc->docid}<br />";
          }
     }
}

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *