public
Description: Acts As Indexed is a plugin which provides a pain-free way to add fulltext search to your Ruby on Rails app
Homepage: http://douglasfshearer.com/blog/rails-plugin-acts_as_indexed
Clone URL: git://github.com/dougal/acts_as_indexed.git
Click here to lend your support to: acts_as_indexed and make a donation at www.pledgie.com !
name age message
file .gitignore Thu Jun 11 14:48:58 -0700 2009 Removed dependency on a Rails application to ru... [dougal]
file CHANGELOG Thu Jun 11 12:32:32 -0700 2009 Fixed Ruby 1.8.6 compatibility. [dougal]
file MIT-LICENSE Thu Sep 06 03:42:32 -0700 2007 Adding everything again. Yah. git-svn-id: http... [dougal]
file README.rdoc Sun Dec 06 11:29:44 -0800 2009 Moved docs to rdoc.info. [dougal]
file Rakefile Mon Dec 15 12:41:32 -0800 2008 Tidied up init structure. Added rake tasks for ... [dougal]
file init.rb Mon Dec 15 12:41:32 -0800 2008 Tidied up init structure. Added rake tasks for ... [dougal]
directory lib/ Thu Jul 02 15:05:28 -0700 2009 Improved performance of sorts by 10x. Minor oth... [dougal]
directory test/ Thu Jun 11 14:54:24 -0700 2009 Fixed improper deletion of index between test r... [dougal]
README.rdoc

acts_as_indexed

If you find this plugin useful, please consider a donation to show your support!

www.paypal.com/cgi-bin/webscr?cmd=_send-money

Paypal address: dougal.s@gmail.com

Instructions

This plugin allows boolean-queried fulltext search to be added to any Rails app with no dependencies and minimal setup.

Resources

Install

  ./script/plugin install git://github.com/dougal/acts_as_indexed.git

If you don’t have git installed, you can download the plugin from the GitHub page (github.com/dougal/acts_as_indexed) and unpack it into the +vendor/plugins+ directory of your rails app.

Usage

Setup

Add acts_as_indexed to the top of any models you want to index, along with a list of the fields you wish to be indexed.

        class Post < ActiveRecord::Base
          acts_as_indexed :fields => [:title, :body]

          ...
        end

The fields are not limited to model fields, but can be any instance method of the current model.

        class User < ActiveRecord::Base
          acts_as_indexed :fields => [:address, :fullname]

                def fullname
                        return self.firstname + ' ' + self.lastname
                end

          ...
        end

Acts_as_indexed automatically filters out query words shorter than 3 characters, for performance sake. This value can be changed by passing min_word_size along with the fields. NOTE: The index files must be rebuilt by deleting the ‘index’ directory in your app’s root directory.

        class Post < ActiveRecord::Base
          acts_as_indexed :fields => [:title, :body], :min_word_size => 5

          ...
        end

Searching

To search, call the find_with_index method on your model, passing a query as the first argument. The optional ids_only parameter, when set to true, will return only the IDs of any matching records.

        # Returns array of Post objects.
        my_search_results = Post.find_with_index('my search query')

        # Pass any of the ActiveRecord find options to the search.
        my_search_results = Post.find_with_index('my search query',{:limit => 10}) # return the first 10 matches.

        # Returns array of IDs.
        my_search_results = Post.find_with_index('my search query',{},{:ids_only => true}) # =>  [12,19,33...

Query Options

The following query operators are supported:

AND:This is the default option. ‘cat dog’ will find records matching ‘cat’ AND ‘dog’.
NOT:‘cat -dog’ will find records matching ‘cat’ AND NOT ‘dog’
INCLUDE:‘cat +me’ will find records matching ‘cat’ and ‘me’, even if ‘me’ is smaller than the min_word_size
"":Quoted terms are matched as phrases. ’"cat dog"’ will find records matching the whole phrase. Quoted terms can be preceded by the NOT operator; ‘cat -"big dog"’ etc. Quoted terms can include words shorter than the min_word_size.

Pagination

Pagination is supported via the paginate_search method whose first argument is the search query, followed all the standard will_paginate arguments.

  @images = Image.paginate_search 'girl', :page => 1, :per_page => 5

RDoc Documentation

To generate the RDoc documentation, run the rake rdoc task in the acts_as_indexed plugin folder. Then point your browser at /vendor/plugins/acts_as_indexed/rdoc/index.html.

Alternatively, you can view the rdoc documentation online.

Problems, Comments, Suggestions?

All of the above are most welcome. dougal.s@gmail.com

Credits

Douglas F Shearer - douglasfshearer.com

Future Releases

Future releases will be looking to add the following features:

  • Optional html scrubbing during indexing.
  • Ranking affected by field weightings.