| 1 |
This is a very minimalistic example of using ferret |
|---|
| 2 |
to do full text searches in Rails model data. Tested with |
|---|
| 3 |
Ruby 1.8.3, Rails 1.0 and Ferret 0.3.2. |
|---|
| 4 |
|
|---|
| 5 |
You can install the latter with |
|---|
| 6 |
gem install ferret |
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
The steps it took me to create this small demo project were: |
|---|
| 10 |
|
|---|
| 11 |
Rails project setup |
|---|
| 12 |
=================== |
|---|
| 13 |
These steps are pretty common and have nothing to do with ferret. |
|---|
| 14 |
|
|---|
| 15 |
Create rails project |
|---|
| 16 |
rails ferret |
|---|
| 17 |
cd ferret |
|---|
| 18 |
|
|---|
| 19 |
Create databases (if you don't use unix and/or sudo, use 'mysql -u root -p') |
|---|
| 20 |
sudo mysql |
|---|
| 21 |
|
|---|
| 22 |
create database ferret_development; |
|---|
| 23 |
create database ferret_test; |
|---|
| 24 |
grant all on ferret_development.* to 'ferret'@'localhost' identified by 'ferret'; |
|---|
| 25 |
grant all on ferret_test.* to 'ferret'@'localhost' identified by 'ferret'; |
|---|
| 26 |
|
|---|
| 27 |
Create database schema |
|---|
| 28 |
mysql -u ferret -pferret ferret_development < db/schema.sql |
|---|
| 29 |
|
|---|
| 30 |
Edit config/database.yml (user names and passwords) |
|---|
| 31 |
|
|---|
| 32 |
Create content controller and model |
|---|
| 33 |
script/generate scaffold Content |
|---|
| 34 |
|
|---|
| 35 |
Running rake at this point should yield no errors. |
|---|
| 36 |
Otherwise check your database configuration. |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
Ferret integration |
|---|
| 40 |
================== |
|---|
| 41 |
Put acs_as_ferret plugin into vendor/plugin |
|---|
| 42 |
|
|---|
| 43 |
Add ferret to the content model class: |
|---|
| 44 |
acts_as_ferret :fields => [ 'title', 'description' ] |
|---|
| 45 |
|
|---|
| 46 |
Add unit tests for the find_by_contents method to content_test.rb. |
|---|
| 47 |
|
|---|
| 48 |
running rake should succeed, and there should be an index in |
|---|
| 49 |
RAILS_ROOT/index/test/Content. |
|---|
| 50 |
|
|---|
| 51 |
Extend the ContentController with a search method and create the search view |
|---|
| 52 |
Add unit tests for the search method to content_controller_test.rb |
|---|
| 53 |
Run rake again to see the tests pass. |
|---|
| 54 |
Remember that, as a good programmer, you would've written the tests first, |
|---|
| 55 |
before implementing the feature ;-) |
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
Try it out |
|---|
| 59 |
========== |
|---|
| 60 |
Run the app with script/server and create some content objects. You should |
|---|
| 61 |
see the index in RAILS_ROOT/index/development/Content. |
|---|
| 62 |
|
|---|
| 63 |
Try searching at http://localhost:3000/content/search |
|---|
| 64 |
|
|---|
| 65 |
|
|---|
| 66 |
Please contact me if you find any bugs/problems within this howto or |
|---|
| 67 |
in the code. |
|---|
| 68 |
|
|---|
| 69 |
Jens Kraemer <jk@jkraemer.net> |
|---|
| 70 |
Feel free to use under the terms of the MIT license. |
|---|