Skip to content

Instantly share code, notes, and snippets.

@ninetwentyfour
Created August 28, 2011 22:00
Show Gist options
  • Save ninetwentyfour/1177288 to your computer and use it in GitHub Desktop.
Save ninetwentyfour/1177288 to your computer and use it in GitHub Desktop.
Fast CakePHP Search With IndexTank 1 - blogpost
<?php
function createIndextankClient(){
App::import('Vendor', 'indextank_client');
$API_URL = 'YOUR API URL HERE';
$client = new ApiClient($API_URL);
return $client;
}
function addIndextank($indexType,$id,$data){
//send project to indextank
$client = $this->createIndextankClient();
$index = $client->get_index($indexType);
$doc_id = $id;
$index->add_document($doc_id, $data);
}
function deleteIndextank($indexType,$id){
//delete indextank document
$client = $this->createIndextankClient();
$index = $client->get_index($indexType);
$index->delete_document($id);
}
function searchIndextank($indexType,$query){
//search indextank
$client = $this->createIndextankClient();
$index = $client->get_index($indexType);
$index->add_function(2, "relevance");
$res = $index->search($query);
return $res;
}
?>
<?php
if ($this->Project->save($this->data)) {
$this->Session->setFlash('The project has been saved', 'default', array('class' => 'flash_good'));
$this->redirect(array('action' => 'index'));
}
?>
<?php
if ($this->Project->save($this->data)) {
//send project to index tank
$indexData = array('text'=>$this->data['Project']['title'],'title'=>$this->data['Project']['title'],'description'=>$this->data['Project']['description'],'user_id'=>$_SESSION['Auth']['User']['_id']);
$id = $this->Project->id;
$this->addIndextank("HomkoraProjects",$id,$indexData);
$this->Session->setFlash('The project has been saved', 'default', array('class' => 'flash_good'));
$this->redirect(array('action' => 'index'));
}
?>
<?php
$indexData = array('text'=>$this->data['Project']['title'],'title'=>$this->data['Project']['title'],'description'=>$this->data['Project']['description'],'user_id'=>$_SESSION['Auth']['User']['_id']);
?>
<?php $id = $this->Project->id; ?>
<?php $this->addIndextank("HomkoraProjects",$id,$indexData); ?>
<?php
if ($this->Project->delete($id)) {
$this->Session->setFlash('Project deleted', 'default', array('class' => 'flash_good'));
$this->redirect(array('action'=>'index'));
}
?>
<?php
if ($this->Project->delete($id)) {
$this->deleteIndextank("HomkoraProjects",$id);
$this->Session->setFlash('Project deleted', 'default', array('class' => 'flash_good'));
$this->redirect(array('action'=>'index'));
}
?>
<?php
function search(){
$query = $this->data['Project']['search'];
$res = $this->searchIndextank("HomkoraProjects",$query);
$i = 0;
foreach($res->results as $doc_id){
$params = array(
'conditions' => array('_id' => $doc_id->docid)
);
$projects[$i++] = $this->Project->find('first',$params);
}
$this->set('projects', $projects);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment