1 # Copyright (c) 2006 Kasper Weibel Nielsen-Refs, Thomas Lockney, Jens Krämer
3 # Permission is hereby granted, free of charge, to any person obtaining a copy
4 # of this software and associated documentation files (the "Software"), to deal
5 # in the Software without restriction, including without limitation the rights
6 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 # copies of the Software, and to permit persons to whom the Software is
8 # furnished to do so, subject to the following conditions:
10 # The above copyright notice and this permission notice shall be included in all
11 # copies or substantial portions of the Software.
13 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 require 'active_support'
22 require 'active_record'
27 require 'bulk_indexer'
28 require 'ferret_extensions'
30 require 'search_results'
31 require 'class_methods'
32 require 'shared_index_class_methods'
33 require 'ferret_result'
34 require 'instance_methods'
37 require 'more_like_this'
41 require 'shared_index'
42 require 'remote_index'
44 require 'ferret_server'
47 # The Rails ActiveRecord Ferret Mixin.
49 # This mixin adds full text search capabilities to any Rails model.
51 # The current version emerged from on the original acts_as_ferret plugin done by
52 # Kasper Weibel and a modified version done by Thomas Lockney, which both can be
53 # found on the Ferret Wiki: http://ferret.davebalmain.com/trac/wiki/FerretOnRails.
56 # include the following in your model class (specifiying the fields you want to get indexed):
57 # acts_as_ferret :fields => [ :title, :description ]
59 # now you can use ModelClass.find_by_contents(query) to find instances of your model
60 # whose indexed fields match a given query. All query terms are required by default, but
61 # explicit OR queries are possible. This differs from the ferret default, but imho is the more
62 # often needed/expected behaviour (more query terms result in less results).
64 # Released under the MIT license.
67 # Kasper Weibel Nielsen-Refs (original author)
68 # Jens Kraemer <jk@jkraemer.net> (active maintainer)
72 # global Hash containing all multi indexes created by all classes using the plugin
73 # key is the concatenation of alphabetically sorted names of the classes the
75 @@multi_indexes = Hash.new
76 def self.multi_indexes; @@multi_indexes end
78 # global Hash containing the ferret indexes of all classes using the plugin
79 # key is the index directory.
80 @@ferret_indexes = Hash.new
81 def self.ferret_indexes; @@ferret_indexes end
85 def self.ensure_directory(dir)
86 FileUtils.mkdir_p dir unless (File.directory?(dir) || File.symlink?(dir))
89 # make sure the default index base dir exists. by default, all indexes are created
90 # under RAILS_ROOT/index/RAILS_ENV
91 def self.init_index_basedir
92 index_base = "#{RAILS_ROOT}/index"
93 @@index_dir = "#{index_base}/#{RAILS_ENV}"
96 mattr_accessor :index_dir
99 def self.append_features(base)
101 base.extend(ClassMethods)
104 # builds a FieldInfos instance for creation of an index containing fields
105 # for the given model classes.
106 def self.field_infos(models)
107 # default attributes for fields
108 fi = Ferret::Index::FieldInfos.new(:store => :no,
113 fi.add_field(:id, :store => :yes, :index => :untokenized)
115 have_class_name = false
116 models.each do |model|
117 fields.update(model.aaf_configuration[:ferret_fields])
119 if !have_class_name && model.aaf_configuration[:store_class_name]
120 fi.add_field(:class_name, :store => :yes, :index => :untokenized)
121 have_class_name = true
124 fields.each_pair do |field, options|
125 options = options.dup
126 options.delete(:boost) if options[:boost].is_a?(Symbol)
127 fi.add_field(field, { :store => :no,
128 :index => :yes }.update(options))
133 def self.close_multi_indexes
134 # close combined index readers, just in case
135 # this seems to fix a strange test failure that seems to relate to a
136 # multi_index looking at an old version of the content_base index.
137 multi_indexes.each_pair do |key, index|
138 # puts "#{key} -- #{self.name}"
139 # TODO only close those where necessary (watch inheritance, where
140 # self.name is base class of a class where key is made from)
141 index.close #if key =~ /#{self.name}/
148 # include acts_as_ferret method into ActiveRecord::Base
149 ActiveRecord::Base.extend ActsAsFerret::ActMethods