acts_as_ferret 0.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.init.rb.swp +0 -0
- data/.rakefile.swp +0 -0
- data/LICENSE +20 -0
- data/README +26 -0
- data/init.rb +22 -0
- data/lib/.acts_as_ferret.rb.swp +0 -0
- data/lib/.class_methods.rb.swo +0 -0
- data/lib/.class_methods.rb.swp +0 -0
- data/lib/acts_as_ferret.rb +124 -0
- data/lib/class_methods.rb +505 -0
- data/lib/instance_methods.rb +157 -0
- data/lib/more_like_this.rb +203 -0
- data/lib/multi_index.rb +87 -0
- data/rakefile +191 -0
- metadata +59 -0
data/.init.rb.swp
ADDED
Binary file
|
data/.rakefile.swp
ADDED
Binary file
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2006 Kasper Weibel, Jens Kraemer
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
acts_as_ferret
|
2
|
+
|
3
|
+
This ActiveRecord mixin adds full text search capabilities to any Rails model.
|
4
|
+
|
5
|
+
It is heavily based on the original acts_as_ferret plugin done by
|
6
|
+
Kasper Weibel and a modified version done by Thomas Lockney, which
|
7
|
+
both can be found on http://ferret.davebalmain.com/trac/wiki/FerretOnRails
|
8
|
+
|
9
|
+
usage:
|
10
|
+
include the following in your model class (specifiying the fields you want to get indexed):
|
11
|
+
|
12
|
+
acts_as_ferret :fields => [ 'title', 'description' ]
|
13
|
+
|
14
|
+
now you can use ModelClass.find_by_contents(query) to find instances of your model
|
15
|
+
whose indexed fields match a given query. All query terms are required by default,
|
16
|
+
but explicit OR queries are possible. This differs from the ferret default, but
|
17
|
+
imho is the more often needed/expected behaviour (more query terms result in
|
18
|
+
less results).
|
19
|
+
|
20
|
+
Released under the MIT license.
|
21
|
+
|
22
|
+
Authors:
|
23
|
+
Kasper Weibel Nielsen-Refs (original author)
|
24
|
+
Jens Kraemer <jk@jkraemer.net>
|
25
|
+
|
26
|
+
|
data/init.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# Copyright (c) 2006 Kasper Weibel Nielsen-Refs, Thomas Lockney, Jens Krämer
|
2
|
+
#
|
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:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in all
|
11
|
+
# copies or substantial portions of the Software.
|
12
|
+
#
|
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
|
19
|
+
# SOFTWARE.
|
20
|
+
|
21
|
+
require 'acts_as_ferret'
|
22
|
+
|
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1,124 @@
|
|
1
|
+
# Copyright (c) 2006 Kasper Weibel Nielsen-Refs, Thomas Lockney, Jens Krämer
|
2
|
+
#
|
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:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in all
|
11
|
+
# copies or substantial portions of the Software.
|
12
|
+
#
|
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
|
19
|
+
# SOFTWARE.
|
20
|
+
|
21
|
+
require 'active_record'
|
22
|
+
require 'set'
|
23
|
+
require 'ferret'
|
24
|
+
|
25
|
+
require 'multi_index'
|
26
|
+
require 'more_like_this'
|
27
|
+
require 'class_methods'
|
28
|
+
require 'instance_methods'
|
29
|
+
|
30
|
+
# 0.10 problems
|
31
|
+
# Ferret::Search::Similarity, Ferret::Search::Similarity.default missing
|
32
|
+
# IndexReader#latest? segfaults when used on multiple indexes
|
33
|
+
|
34
|
+
# The Rails ActiveRecord Ferret Mixin.
|
35
|
+
#
|
36
|
+
# This mixin adds full text search capabilities to any Rails model.
|
37
|
+
#
|
38
|
+
# The current version emerged from on the original acts_as_ferret plugin done by
|
39
|
+
# Kasper Weibel and a modified version done by Thomas Lockney, which both can be
|
40
|
+
# found on the Ferret Wiki: http://ferret.davebalmain.com/trac/wiki/FerretOnRails.
|
41
|
+
#
|
42
|
+
# basic usage:
|
43
|
+
# include the following in your model class (specifiying the fields you want to get indexed):
|
44
|
+
# acts_as_ferret :fields => [ 'title', 'description' ]
|
45
|
+
#
|
46
|
+
# now you can use ModelClass.find_by_contents(query) to find instances of your model
|
47
|
+
# whose indexed fields match a given query. All query terms are required by default, but
|
48
|
+
# explicit OR queries are possible. This differs from the ferret default, but imho is the more
|
49
|
+
# often needed/expected behaviour (more query terms result in less results).
|
50
|
+
#
|
51
|
+
# Released under the MIT license.
|
52
|
+
#
|
53
|
+
# Authors:
|
54
|
+
# Kasper Weibel Nielsen-Refs (original author)
|
55
|
+
# Jens Kraemer <jk@jkraemer.net> (active maintainer)
|
56
|
+
#
|
57
|
+
module FerretMixin
|
58
|
+
module Acts #:nodoc:
|
59
|
+
module ARFerret #:nodoc:
|
60
|
+
|
61
|
+
# decorator that adds a total_hits accessor to search result arrays
|
62
|
+
class SearchResults
|
63
|
+
attr_reader :total_hits
|
64
|
+
def initialize(results, total_hits)
|
65
|
+
@results = results
|
66
|
+
@total_hits = total_hits
|
67
|
+
end
|
68
|
+
def method_missing(symbol, *args, &block)
|
69
|
+
@results.send(symbol, *args, &block)
|
70
|
+
end
|
71
|
+
def respond_to?(name)
|
72
|
+
self.methods.include?(name) || @results.respond_to?(name)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def self.ensure_directory(dir)
|
77
|
+
FileUtils.mkdir_p dir unless File.directory? dir
|
78
|
+
end
|
79
|
+
|
80
|
+
# make sure the default index base dir exists. by default, all indexes are created
|
81
|
+
# under RAILS_ROOT/index/RAILS_ENV
|
82
|
+
def self.init_index_basedir
|
83
|
+
index_base = "#{RAILS_ROOT}/index"
|
84
|
+
ensure_directory index_base
|
85
|
+
@@index_dir = "#{index_base}/#{RAILS_ENV}"
|
86
|
+
ensure_directory @@index_dir
|
87
|
+
end
|
88
|
+
|
89
|
+
mattr_accessor :index_dir
|
90
|
+
init_index_basedir
|
91
|
+
|
92
|
+
def self.append_features(base)
|
93
|
+
super
|
94
|
+
base.extend(ClassMethods)
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
# reopen ActiveRecord and include all the above to make
|
104
|
+
# them available to all our models if they want it
|
105
|
+
ActiveRecord::Base.class_eval do
|
106
|
+
include FerretMixin::Acts::ARFerret
|
107
|
+
end
|
108
|
+
|
109
|
+
|
110
|
+
class Ferret::Index::MultiReader
|
111
|
+
def latest?
|
112
|
+
# TODO: Exception handling added to resolve ticket #6.
|
113
|
+
# It should be clarified wether this is a bug in Ferret
|
114
|
+
# in which case a bug report should be posted on the Ferret Trac.
|
115
|
+
begin
|
116
|
+
@sub_readers.each { |r| return false unless r.latest? }
|
117
|
+
rescue
|
118
|
+
return false
|
119
|
+
end
|
120
|
+
true
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
# END acts_as_ferret.rb
|