Created
August 28, 2011 22:00
-
-
Save ninetwentyfour/1177288 to your computer and use it in GitHub Desktop.
Fast CakePHP Search With IndexTank 1 - blogpost
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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')); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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')); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$indexData = array('text'=>$this->data['Project']['title'],'title'=>$this->data['Project']['title'],'description'=>$this->data['Project']['description'],'user_id'=>$_SESSION['Auth']['User']['_id']); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php $id = $this->Project->id; ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php $this->addIndextank("HomkoraProjects",$id,$indexData); ?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if ($this->Project->delete($id)) { | |
$this->Session->setFlash('Project deleted', 'default', array('class' => 'flash_good')); | |
$this->redirect(array('action'=>'index')); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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')); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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