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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
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 />"; } } } |
IndexTank is closing down at 10 April 2012, and I am encourage your to try IndexDen which is 100% compatible with IndexTank API.
I forked and added autocomplete https://github.com/abda53/Haystack—IndexTank-Client-for-CodeIgniter
Thanks for the good work!
Oh, I also added
$query[‘match_any_field’] = ‘true';
to function search()