Adding Full Text Search to Croogo

Posted by Andrew on November 16th 2009, 4:18am

I just launched full text search on my blog. I am sure that this is something planned for the future of Croogo but I wanted some to add search functionality and not have to wait. Once I figure out Git/Github I will fork the main project and get some of my additions up there.

Adding full text searching is actually quite easy. I limited it to searching the title and the body of the posts and only allowed searching of blog posts. Below is the code I used. This goes in the nodes controller.

function search () {
// Import sanitize library
App::import('Core', array('Sanitize'));

$query = mysql_escape_string(Sanitize::html($this->params['url']['q']));

$this->paginate['Node']['order'] = 'Node.id DESC';
$this->paginate['Node']['limit'] = Configure::read('Reading.nodes_per_page');
$this->paginate['Node']['conditions'] = array(
'Node.type' => 'blog',
'Node.status' => 1,
"MATCH(Node.title, Node.body) AGAINST('{$query}' IN BOOLEAN MODE)"
);
$this->paginate['Node']['contain'] = array(
'User',
'Meta',
'Comment',
'Term' => array('Vocabulary')
);

$this->Node->recursive = 0;
$this->set('nodes', $this->paginate());
$this->set('q', $query);
$this->set('title', "Search results for {$query}");
}

Now all that's needed is a form that will request the search action in the nodes controller and a search template to display your search results. For my form I used the "GET" method. In my search template I used the text helper to highlight the search term and also show an excerpt instead of the full post as shown below.

<p><?=$text->highlight($text->excerpt(strip_tags($this->element('node_body', array('node' => $node))), $q, 200, '...'), $q)?></p>

As usual if you have any questions leave a comment and I should get back to you shortly.

Update November 19th

Croogo Full Text Search

Bookmark and Share
Posted in Croogo

0 Comments

Add New Comment