Neil Crookes

Learnings and Teachings on Web Application Development & CakePHP



CakePHP Searchable Plugin

A flexible and full featured CakePHP plugin for quickly adding site wide, multi-model search functionality to your application.

Share and Enjoy:

  • Digg
  • StumbleUpon
  • Technorati
  • Slashdot

At work we recently developed an application for a client that required site-wide search functionality, and provided a single results set from multiple models/sources. Normally I’d use the CakePHP Yahoo BOSS site search I wrote and blogged about previously, but this particular app requires users to login to access any content, so Yahoo wouldn’t be able to index any of the content.

I had a hunt around to see if there was anything already out there that would fit the requirements and I found this which is a behavior that stores the data from multiple models in a single search_index table and performs mysql full text search on that, but it didn’t quite have all the features I needed such as scope for search results, i.e. you set the status of a record to in-active and the corresponding record in the search_index table goes in-active. I did, however, like this approach.

So, inspired by the above solution, I’ve written a plugin that you can add to your app and integrate site wide search functionality in a matter of minutes.

The code is available on my github account. Note, it relies on MySQL Full Text Search, but you could replace this with your own search algorithm or alternative RDBMS equivalent.

The plugin includes:

  1. A Searchable Behavior to attach to models in your app that automatically maintains a record in the search_index table for each record in the model you attach it to.
  2. A shell script to build/re-build the search_index table for all/some models that have the Searchable Behavior attached
  3. Model, View and Controller for the search_index table that handles performing the search, and displaying the results.

To get it up and running:

  1. Get the code from github
  2. Run the SQL in searchable/config/sql/search_index.sql
  3. Attach the Searchable Behavior to the models in your app that you want to search, e.g.

    var $actsAs => array('Searchable.Searchable');

  4. Run the build_search_index shell, e.g.

    $> cake build_search_index

  5. Add the searchable/config/routes.php file to you app/config/routes.php

    // app/config/routes.php

  6. Add the search form element to a page in your site, or in the default.ctp layout file e.g.

    echo $this->element('form', array('plugin' => 'searchable'));

Now type something in the search box and go.

Some additional features/notes:

  • You’ll notice on the search results page you can restrict your search to a single model.
  • Search results are paginated.
  • Search terms are added to the URL so you can deep link to search results
  • The search supports MySQL Full Text Search in boolean mode, so you can do things like searching for phrases using quotes and excluding words using the minus sign
  • The search_index table has a scope field which is a boolean (tinyint 1) and is set to 1 by default, but if you specify some normal cakephp conditions in the scope setting when you attach the Searchable Behavior, this will be set depending on whether these conditions are met for that particular record. E.g.var $actsAs = array(‘Searchable.Searchable’ => array(‘scope’ => array(‘’ => 1)));
  • Data from your model is stored in the ‘data’ field of the search_index table and is json_encode’d. This is to circumvent one of the issues of the Searchable behavior I found earlier that someone noted in the issues list – if you call saveField, only that field’s data got saved in the search_index table. With this behavior, when editing a record, if not all fields are present in the data you are saving, the existing content of the data field is merged with the new data you are saving, so you don’t lose any data that you had previously.
  • By default, all string type fields are included in the json_encode’d data field, but you can override this if necessary using the ‘fields’ setting when you attach the behavior. E.g.var $actsAs = array(‘Searchable.Searchable’ => array(‘fields’ => array(‘title’, ‘abstract’, ‘body’, ‘published‘)));
  • Sometimes it’s useful to be able to search for associated data as well, e.g. the name of the Category that a Post belongsTo, to achieve this you can do the following:var $actsAs = array(‘Searchable.Searchable’ => array(‘fields’ => array(‘title’, ‘abstract’, ‘body’, category_id‘ => ‘’)));I.e. the foreign key field in the searchable model => the model.field you want to fetch the value from.
  • The search_index table also includes fields for ‘name’ and ‘summary’, you can configure which fields in your model are used to populate these fields in the search_index table in the settings array too. What goes in here are what’s displayed in the search results.
  • If your data uses a published date field (or equivalent) to determine whether content should be displayed or not, as an alternative or in addition to scope, the search_index table also has a published field, and again you can configure which field in your model should map through to it. The search results are scope to only display records whose published field is null (which it will be by default if you have no published data), or the published date is in the past – but you can configure this as required by your app. For example on another app I’ve used this on I changed these conditions to published in the past, but not more than 6 months ago, or if logged in (i.e. an administrator), display future content as well so they can preview stuff.
  • By default, the search result will link through to the controller for the model of that search result, it’s view action, and pass the id of the record as a parameter. You can configure this to some extent at the moment, e.g. if your model is actually in another plugin, you can add this to the settings, but that’s about it at the moment, so no slugs or anything like that. If you need to configure the url formats, suggest you just amend the views/search_indexes/index.ctp view file to your requirements.

Enjoy ;-)

Share and Enjoy:

  • Digg
  • StumbleUpon
  • Technorati
  • Slashdot
(7 votes, average: 4.43 out of 5)
Loading ... Loading ...

Comments are closed.