My favorites | Sign in
Project Home Downloads Wiki Issues Source
Project Information
Members
Featured
Downloads
Links

This behaviour allows Cake developers to make a model fulltext searchable, meaning that the user will be able to perform mysql fulltext search (which powers Wikimedia for example) on the models.

For example, a fulltext search allows end users to use boolean expressions (eg. '-' to exclude a term) or natural language search which interprets the search string as a phrase in natural human language.

Setup

To setup this behaviour you will need to create the following MySQL table

CREATE TABLE `search_index` (
	`id` int(11) NOT NULL auto_increment,
	`association_key` varchar(36) NOT NULL,
	`model` varchar(128) collate utf8_unicode_ci NOT NULL,
	`data` longtext collate utf8_unicode_ci NOT NULL,
	`created` datetime NOT NULL,
	`modified` datetime NOT NULL,
	PRIMARY KEY  (`id`),
	KEY `association_key` (`association_key`,`model`),
	FULLTEXT KEY `data` (`data`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Usage

At your disposal, there are several ways to use it:

SearchIndex Model

You can use the SearchIndex model to do the searches using the regular cake php Model->find() . By default this will search all the subjects (if you have more than one searchable model). To search only ceratin models you can use SearchIndex->searchModels()

An exemple usage, using cakephp pagination:

<?php
	$this->SearchIndex->searchModels(array('Post','Comment'));
	$this->paginate = array(
		'limit' => 10,
		'conditions' =>  "MATCH(SearchIndex.data) AGAINST('$q' IN BOOLEAN MODE)"
	);
	$this->set('results', $this->paginate('SearchIndex'));
?>

The searchable model

To do a search on the actual searchable model, you can use Model->search(query)

Indexing data

To provide a custom indexing function, you can define your own indexData() function. By default all varchar, char and text fields are indexed.

<?php
class Comment extends AppModel {

	var $name = 'Comment';
	var $actsAs = array('Searchable');
	
	function indexData() {
		$index = $this->data['Comment']['text'];
		return $index;
	}
}
?>
Powered by Google Project Hosting