watson-acts_as_ferret 0.4.8.2
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +20 -0
- data/README +104 -0
- data/acts_as_ferret.gemspec +58 -0
- data/bin/aaf_install +29 -0
- data/config/ferret_server.yml +24 -0
- data/doc/README.win32 +23 -0
- data/doc/demo/README +154 -0
- data/doc/demo/README_DEMO +23 -0
- data/doc/demo/Rakefile +10 -0
- data/doc/demo/app/controllers/admin/backend_controller.rb +14 -0
- data/doc/demo/app/controllers/admin_area_controller.rb +4 -0
- data/doc/demo/app/controllers/application.rb +5 -0
- data/doc/demo/app/controllers/contents_controller.rb +49 -0
- data/doc/demo/app/controllers/searches_controller.rb +8 -0
- data/doc/demo/app/helpers/admin/backend_helper.rb +2 -0
- data/doc/demo/app/helpers/application_helper.rb +3 -0
- data/doc/demo/app/helpers/content_helper.rb +2 -0
- data/doc/demo/app/helpers/search_helper.rb +2 -0
- data/doc/demo/app/models/comment.rb +48 -0
- data/doc/demo/app/models/content.rb +12 -0
- data/doc/demo/app/models/content_base.rb +28 -0
- data/doc/demo/app/models/search.rb +19 -0
- data/doc/demo/app/models/shared_index1.rb +3 -0
- data/doc/demo/app/models/shared_index2.rb +3 -0
- data/doc/demo/app/models/special_content.rb +3 -0
- data/doc/demo/app/models/stats.rb +20 -0
- data/doc/demo/app/views/admin/backend/search.rhtml +18 -0
- data/doc/demo/app/views/contents/_form.rhtml +10 -0
- data/doc/demo/app/views/contents/edit.rhtml +9 -0
- data/doc/demo/app/views/contents/index.rhtml +24 -0
- data/doc/demo/app/views/contents/new.rhtml +8 -0
- data/doc/demo/app/views/contents/show.rhtml +8 -0
- data/doc/demo/app/views/layouts/application.html.erb +17 -0
- data/doc/demo/app/views/searches/_content.html.erb +2 -0
- data/doc/demo/app/views/searches/search.html.erb +20 -0
- data/doc/demo/config/boot.rb +109 -0
- data/doc/demo/config/database.yml +38 -0
- data/doc/demo/config/environment.rb +69 -0
- data/doc/demo/config/environments/development.rb +16 -0
- data/doc/demo/config/environments/production.rb +19 -0
- data/doc/demo/config/environments/test.rb +21 -0
- data/doc/demo/config/ferret_server.yml +18 -0
- data/doc/demo/config/lighttpd.conf +40 -0
- data/doc/demo/config/routes.rb +9 -0
- data/doc/demo/db/development_structure.sql +15 -0
- data/doc/demo/db/migrate/001_initial_migration.rb +18 -0
- data/doc/demo/db/migrate/002_add_type_to_contents.rb +9 -0
- data/doc/demo/db/migrate/003_create_shared_index1s.rb +11 -0
- data/doc/demo/db/migrate/004_create_shared_index2s.rb +11 -0
- data/doc/demo/db/migrate/005_special_field.rb +9 -0
- data/doc/demo/db/migrate/006_create_stats.rb +15 -0
- data/doc/demo/db/schema.sql +18 -0
- data/doc/demo/db/schema.sqlite +14 -0
- data/doc/demo/doc/README_FOR_APP +2 -0
- data/doc/demo/doc/howto.txt +70 -0
- data/doc/demo/public/404.html +8 -0
- data/doc/demo/public/500.html +8 -0
- data/doc/demo/public/dispatch.cgi +10 -0
- data/doc/demo/public/dispatch.fcgi +24 -0
- data/doc/demo/public/dispatch.rb +10 -0
- data/doc/demo/public/favicon.ico +0 -0
- data/doc/demo/public/images/rails.png +0 -0
- data/doc/demo/public/index.html +277 -0
- data/doc/demo/public/robots.txt +1 -0
- data/doc/demo/public/stylesheets/scaffold.css +74 -0
- data/doc/demo/script/about +3 -0
- data/doc/demo/script/breakpointer +3 -0
- data/doc/demo/script/console +3 -0
- data/doc/demo/script/destroy +3 -0
- data/doc/demo/script/ferret_server +10 -0
- data/doc/demo/script/generate +3 -0
- data/doc/demo/script/performance/benchmarker +3 -0
- data/doc/demo/script/performance/profiler +3 -0
- data/doc/demo/script/plugin +3 -0
- data/doc/demo/script/process/inspector +3 -0
- data/doc/demo/script/process/reaper +3 -0
- data/doc/demo/script/process/spawner +3 -0
- data/doc/demo/script/process/spinner +3 -0
- data/doc/demo/script/runner +3 -0
- data/doc/demo/script/server +3 -0
- data/doc/demo/test/fixtures/comments.yml +12 -0
- data/doc/demo/test/fixtures/contents.yml +13 -0
- data/doc/demo/test/fixtures/remote_contents.yml +9 -0
- data/doc/demo/test/fixtures/shared_index1s.yml +7 -0
- data/doc/demo/test/fixtures/shared_index2s.yml +7 -0
- data/doc/demo/test/functional/admin/backend_controller_test.rb +35 -0
- data/doc/demo/test/functional/contents_controller_test.rb +81 -0
- data/doc/demo/test/functional/searches_controller_test.rb +71 -0
- data/doc/demo/test/smoke/drb_smoke_test.rb +321 -0
- data/doc/demo/test/smoke/process_stats.rb +21 -0
- data/doc/demo/test/test_helper.rb +30 -0
- data/doc/demo/test/unit/comment_test.rb +217 -0
- data/doc/demo/test/unit/content_test.rb +705 -0
- data/doc/demo/test/unit/ferret_result_test.rb +24 -0
- data/doc/demo/test/unit/multi_index_test.rb +329 -0
- data/doc/demo/test/unit/remote_index_test.rb +23 -0
- data/doc/demo/test/unit/shared_index1_test.rb +108 -0
- data/doc/demo/test/unit/shared_index2_test.rb +13 -0
- data/doc/demo/test/unit/sort_test.rb +21 -0
- data/doc/demo/test/unit/special_content_test.rb +25 -0
- data/doc/demo/vendor/plugins/will_paginate/LICENSE +18 -0
- data/doc/demo/vendor/plugins/will_paginate/README +108 -0
- data/doc/demo/vendor/plugins/will_paginate/Rakefile +23 -0
- data/doc/demo/vendor/plugins/will_paginate/init.rb +21 -0
- data/doc/demo/vendor/plugins/will_paginate/lib/will_paginate/collection.rb +45 -0
- data/doc/demo/vendor/plugins/will_paginate/lib/will_paginate/core_ext.rb +44 -0
- data/doc/demo/vendor/plugins/will_paginate/lib/will_paginate/finder.rb +159 -0
- data/doc/demo/vendor/plugins/will_paginate/lib/will_paginate/view_helpers.rb +95 -0
- data/doc/demo/vendor/plugins/will_paginate/test/array_pagination_test.rb +23 -0
- data/doc/demo/vendor/plugins/will_paginate/test/boot.rb +27 -0
- data/doc/demo/vendor/plugins/will_paginate/test/console +10 -0
- data/doc/demo/vendor/plugins/will_paginate/test/finder_test.rb +219 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/admin.rb +3 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/companies.yml +24 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/company.rb +23 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/developer.rb +11 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/developers_projects.yml +13 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/project.rb +4 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/projects.yml +7 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/replies.yml +20 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/reply.rb +5 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/schema.sql +44 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/topic.rb +19 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/topics.yml +30 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/user.rb +2 -0
- data/doc/demo/vendor/plugins/will_paginate/test/fixtures/users.yml +35 -0
- data/doc/demo/vendor/plugins/will_paginate/test/helper.rb +42 -0
- data/doc/demo/vendor/plugins/will_paginate/test/lib/activerecord_test_connector.rb +64 -0
- data/doc/demo/vendor/plugins/will_paginate/test/lib/load_fixtures.rb +10 -0
- data/doc/demo/vendor/plugins/will_paginate/test/pagination_test.rb +136 -0
- data/doc/monit-example +22 -0
- data/init.rb +24 -0
- data/install.rb +18 -0
- data/lib/act_methods.rb +147 -0
- data/lib/acts_as_ferret.rb +593 -0
- data/lib/ar_mysql_auto_reconnect_patch.rb +41 -0
- data/lib/blank_slate.rb +54 -0
- data/lib/bulk_indexer.rb +56 -0
- data/lib/class_methods.rb +279 -0
- data/lib/ferret_extensions.rb +192 -0
- data/lib/ferret_find_methods.rb +142 -0
- data/lib/ferret_result.rb +58 -0
- data/lib/ferret_server.rb +238 -0
- data/lib/index.rb +99 -0
- data/lib/instance_methods.rb +172 -0
- data/lib/local_index.rb +202 -0
- data/lib/more_like_this.rb +217 -0
- data/lib/multi_index.rb +133 -0
- data/lib/rdig_adapter.rb +149 -0
- data/lib/remote_functions.rb +43 -0
- data/lib/remote_index.rb +54 -0
- data/lib/remote_multi_index.rb +20 -0
- data/lib/search_results.rb +50 -0
- data/lib/server_manager.rb +71 -0
- data/lib/unix_daemon.rb +86 -0
- data/lib/without_ar.rb +52 -0
- data/recipes/aaf_recipes.rb +116 -0
- data/script/ferret_daemon +94 -0
- data/script/ferret_server +12 -0
- data/script/ferret_service +178 -0
- data/tasks/ferret.rake +39 -0
- metadata +246 -0
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
require 'pp'
|
3
|
+
require 'fileutils'
|
4
|
+
|
5
|
+
class FerretResultTest < Test::Unit::TestCase
|
6
|
+
fixtures :contents
|
7
|
+
|
8
|
+
def teardown
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_get_prefetched_fields_from_hash
|
12
|
+
fr = ActsAsFerret::FerretResult.new 'Content', '1', 0.5, 1, :description => 'description from ferret index'
|
13
|
+
assert_equal 'description from ferret index', fr.description
|
14
|
+
assert_equal 0.5, fr.ferret_score
|
15
|
+
assert_equal 1, fr.ferret_rank
|
16
|
+
assert_equal 'My Title', fr.title # triggers auto-load of the record
|
17
|
+
assert_equal 'A useless description', fr.description # description now comes from DB
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_to_param
|
21
|
+
fr = ActsAsFerret::FerretResult.new 'Content', '1', 0.5, 1, :description => 'description from ferret index'
|
22
|
+
assert_equal '1', fr.to_param
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,329 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
require 'pp'
|
3
|
+
require 'fileutils'
|
4
|
+
|
5
|
+
class MultiIndexTest < Test::Unit::TestCase
|
6
|
+
include Ferret::Index
|
7
|
+
include Ferret::Search
|
8
|
+
fixtures :contents, :comments
|
9
|
+
|
10
|
+
def setup
|
11
|
+
#make sure the fixtures are in the index
|
12
|
+
FileUtils.rm_f 'index/test/'
|
13
|
+
Comment.rebuild_index
|
14
|
+
ContentBase.rebuild_index
|
15
|
+
raise "missing fixtures" unless ContentBase.count > 2
|
16
|
+
|
17
|
+
@another_content = Content.new( :title => 'Another Content item',
|
18
|
+
:description => 'this is not the title' )
|
19
|
+
@another_content.save
|
20
|
+
@comment = @another_content.comments.create(:author => 'john doe', :content => 'This is a useless comment')
|
21
|
+
@comment2 = @another_content.comments.create(:author => 'another', :content => 'content')
|
22
|
+
@another_content.save # to update comment_count in ferret-index
|
23
|
+
end
|
24
|
+
|
25
|
+
def teardown
|
26
|
+
ContentBase.find(:all).each { |c| c.destroy }
|
27
|
+
Comment.find(:all).each { |c| c.destroy }
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
# weiter: single index / multisearch lazy loading
|
32
|
+
# def test_lazy_loading
|
33
|
+
# results = Content.find_with_ferret 'description', :lazy => true
|
34
|
+
# assert_equal 1, results.size
|
35
|
+
# result = results.first
|
36
|
+
# class << result
|
37
|
+
# attr_accessor :ar_record # so we have a chance to check if it's been loaded...
|
38
|
+
# end
|
39
|
+
# assert ActsAsFerret::FerretResult === result
|
40
|
+
# assert_equal 'A useless description', result.description
|
41
|
+
# assert_nil result.instance_variable_get(:@ar_record)
|
42
|
+
# assert_equal 'My Title', result.title
|
43
|
+
# assert_not_nil result.ar_record
|
44
|
+
# end
|
45
|
+
|
46
|
+
|
47
|
+
def test_total_hits
|
48
|
+
q = '*:title OR *:comment'
|
49
|
+
assert_equal 3, Comment.total_hits(q)
|
50
|
+
assert_equal 2, Content.total_hits(q)
|
51
|
+
assert_equal 5, ActsAsFerret::total_hits(q, [ Comment, Content ])
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_sorting
|
55
|
+
sorting = [ Ferret::Search::SortField.new(:id) ]
|
56
|
+
result = ActsAsFerret::find('*:title OR *:comment', [Content, Comment], :sort => sorting)
|
57
|
+
assert_equal result.map(&:id).sort, result.map(&:id)
|
58
|
+
|
59
|
+
sorting = [ Ferret::Search::SortField.new(:title) ]
|
60
|
+
result = ActsAsFerret::find('*:title OR *:comment', [Content, Comment], :sort => sorting)
|
61
|
+
sorting = [ Ferret::Search::SortField.new(:title, :reverse => true) ]
|
62
|
+
result2 = ActsAsFerret::find('*:title OR *:comment', [Content, Comment], :sort => sorting)
|
63
|
+
assert result.any?
|
64
|
+
assert result.map(&:id) != result2.map(&:id)
|
65
|
+
|
66
|
+
result = ActsAsFerret::find('*:title OR *:comment', [Content, Comment ])
|
67
|
+
assert result.any?
|
68
|
+
assert_equal result.map(&:ferret_score).sort.reverse, result.map(&:ferret_score)
|
69
|
+
|
70
|
+
sorting = [ Ferret::Search::SortField::SCORE ]
|
71
|
+
result = ActsAsFerret::find('*:title OR *:comment', [Content, Comment ], :sort => sorting)
|
72
|
+
assert result.any?
|
73
|
+
assert_equal result.map(&:ferret_score).sort.reverse, result.map(&:ferret_score)
|
74
|
+
|
75
|
+
sorting = [ Ferret::Search::SortField::SCORE_REV ]
|
76
|
+
result2 = ActsAsFerret::find('*:title OR *:comment', [Content, Comment], :sort => sorting)
|
77
|
+
assert_equal result2.map(&:ferret_score).sort, result2.map(&:ferret_score)
|
78
|
+
assert_equal result.map(&:ferret_score), result2.map(&:ferret_score).reverse
|
79
|
+
end
|
80
|
+
|
81
|
+
|
82
|
+
# remote index rebuilds will create an index in a directory with a timestamped name.
|
83
|
+
# the local MultiIndex instance doesn't know about this (because it's running in
|
84
|
+
# another interpreter instance than the server) and therefore fails to use the
|
85
|
+
# correct index directories.
|
86
|
+
# TODO strange, still doesn't work but it should now...
|
87
|
+
unless Content.aaf_configuration[:remote]
|
88
|
+
def test_multi_index
|
89
|
+
i = ActsAsFerret::get_index_for Content, Comment
|
90
|
+
assert ActsAsFerret::MultiIndex === i
|
91
|
+
hits = i.search(TermQuery.new(:title,"title"))
|
92
|
+
assert_equal 1, hits.total_hits
|
93
|
+
|
94
|
+
qp = Ferret::QueryParser.new(:default_field => "title",
|
95
|
+
:analyzer => Ferret::Analysis::WhiteSpaceAnalyzer.new)
|
96
|
+
hits = i.search(qp.parse("title"))
|
97
|
+
assert_equal 1, hits.total_hits
|
98
|
+
|
99
|
+
qp = Ferret::QueryParser.new(:fields => ['title', 'content', 'description'],
|
100
|
+
:analyzer => Ferret::Analysis::WhiteSpaceAnalyzer.new)
|
101
|
+
hits = i.search(qp.parse("title"))
|
102
|
+
assert_equal 2, hits.total_hits
|
103
|
+
hits = i.search(qp.parse("title:title OR description:title"))
|
104
|
+
assert_equal 2, hits.total_hits
|
105
|
+
|
106
|
+
hits = i.search("title:title OR description:title OR title:comment OR description:comment OR content:comment")
|
107
|
+
assert_equal 5, hits.total_hits
|
108
|
+
|
109
|
+
hits = i.search("title OR comment")
|
110
|
+
assert_equal 5, hits.total_hits
|
111
|
+
|
112
|
+
hits = i.search("title OR comment", :limit => 2)
|
113
|
+
count = 0
|
114
|
+
hits.hits.each { |hit, score| count += 1 }
|
115
|
+
assert_equal 2, count
|
116
|
+
|
117
|
+
hits = i.search("title OR comment", :offset => 2)
|
118
|
+
count = 0
|
119
|
+
hits.hits.each { |hit, score| count += 1 }
|
120
|
+
assert_equal 3, count
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_search_rebuilds_index
|
125
|
+
remove_index Content
|
126
|
+
contents_from_ferret = ActsAsFerret::find('description:title', [Content, Comment])
|
127
|
+
assert_equal 1, contents_from_ferret.size
|
128
|
+
end
|
129
|
+
|
130
|
+
# remote index rebuilds will create an index in a directory with a timestamped name...
|
131
|
+
unless Content.aaf_configuration[:remote]
|
132
|
+
def test_rebuilds_index
|
133
|
+
remove_index Content
|
134
|
+
idx = ActsAsFerret.get_index_for( Content )
|
135
|
+
i = ActsAsFerret::MultiIndex.new([idx])
|
136
|
+
assert File.exists?("#{idx.index_definition[:index_dir]}/segments")
|
137
|
+
hits = i.search("description:title")
|
138
|
+
assert_equal 1, hits.total_hits, hits.inspect
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
def test_find_options
|
143
|
+
contents_from_ferret = ActsAsFerret::find('title', [Content, Comment ], { }, :order => 'id desc')
|
144
|
+
assert_equal 2, contents_from_ferret.size
|
145
|
+
assert contents_from_ferret.first.id > contents_from_ferret.last.id
|
146
|
+
contents_from_ferret = ActsAsFerret::find('title', [Content, Comment ], { }, :order => 'id asc')
|
147
|
+
assert contents_from_ferret.first.id < contents_from_ferret.last.id
|
148
|
+
|
149
|
+
contents_from_ferret = ActsAsFerret::find('title', [Content, Comment], :limit => 1)
|
150
|
+
assert_equal 1, contents_from_ferret.size
|
151
|
+
contents_from_ferret = ActsAsFerret::find('title', [Content, Comment ], { }, :limit => 1)
|
152
|
+
assert_equal 1, contents_from_ferret.size
|
153
|
+
|
154
|
+
|
155
|
+
more_contents(true)
|
156
|
+
r = ActsAsFerret::find('title OR comment', [Content, Comment], { :limit => :all } )
|
157
|
+
assert_equal 60, r.size
|
158
|
+
assert_equal 60, r.total_hits
|
159
|
+
|
160
|
+
id = Content.find_with_ferret('title').first.id
|
161
|
+
r = ActsAsFerret::find('title OR comment', [Content, Comment], { :limit => :all },
|
162
|
+
{ :conditions => { :content => ["id != ?", id] }})
|
163
|
+
assert_equal 59, r.size
|
164
|
+
assert_equal 59, r.total_hits
|
165
|
+
|
166
|
+
r = ActsAsFerret::find('title OR comment', [Content, Comment], { :limit => 20 },
|
167
|
+
{ :conditions => { :content => ["id != ?", id] }})
|
168
|
+
assert_equal 20, r.size
|
169
|
+
assert_equal 59, r.total_hits
|
170
|
+
|
171
|
+
r = ActsAsFerret::find('title OR comment', [Content, Comment], { :limit => 20 },
|
172
|
+
{ :conditions => { :comment => 'content is null',
|
173
|
+
:content => ["id != ?", id] }})
|
174
|
+
assert_equal 20, r.size
|
175
|
+
assert_equal 29, r.total_hits
|
176
|
+
|
177
|
+
r = ActsAsFerret::find('title OR comment', [Content, Comment ], { },
|
178
|
+
{ :conditions => { :content => ["id != ?", id] }, :limit => 20 })
|
179
|
+
assert_equal 20, r.size
|
180
|
+
assert_equal 59, r.total_hits
|
181
|
+
end
|
182
|
+
|
183
|
+
def test_multi_search
|
184
|
+
assert_equal 4, ContentBase.find(:all).size
|
185
|
+
|
186
|
+
Content.aaf_index.ferret_index.flush
|
187
|
+
contents_from_ferret = ActsAsFerret::find('description:title', [Content])
|
188
|
+
assert_equal 1, contents_from_ferret.size
|
189
|
+
contents_from_ferret = ActsAsFerret::find('title:title OR description:title', [Content])
|
190
|
+
assert_equal 2, contents_from_ferret.size
|
191
|
+
contents_from_ferret = ActsAsFerret::find('title:title', [Content])
|
192
|
+
assert_equal 1, contents_from_ferret.size
|
193
|
+
contents_from_ferret = ActsAsFerret::find('*:title', [Content])
|
194
|
+
assert_equal 2, contents_from_ferret.size
|
195
|
+
contents_from_ferret = ActsAsFerret::find('title', [Content])
|
196
|
+
assert_equal 2, contents_from_ferret.size
|
197
|
+
|
198
|
+
assert_equal contents(:first).id, contents_from_ferret.first.id
|
199
|
+
assert_equal @another_content.id, contents_from_ferret.last.id
|
200
|
+
|
201
|
+
contents_from_ferret = ActsAsFerret::find('title', [Content])
|
202
|
+
assert_equal 2, contents_from_ferret.size
|
203
|
+
contents_from_ferret = ActsAsFerret::find('title', [Content], :limit => 1)
|
204
|
+
assert_equal 1, contents_from_ferret.size
|
205
|
+
contents_from_ferret = ActsAsFerret::find('title', [Content], :offset => 1)
|
206
|
+
assert_equal 1, contents_from_ferret.size
|
207
|
+
|
208
|
+
contents_from_ferret = ActsAsFerret::find('title:title OR content:comment OR description:title', [Content, Comment])
|
209
|
+
assert_equal 5, contents_from_ferret.size
|
210
|
+
contents_from_ferret = ActsAsFerret::find('title:title OR content:comment OR description:title', [Content, Comment], :limit => 2)
|
211
|
+
assert_equal 2, contents_from_ferret.size
|
212
|
+
|
213
|
+
contents_from_ferret = ActsAsFerret::find('*:title OR *:comment', [Content, Comment])
|
214
|
+
assert_equal 5, contents_from_ferret.size
|
215
|
+
contents_from_ferret = ActsAsFerret::find('*:title OR *:comment', [Content, Comment])
|
216
|
+
assert_equal 5, contents_from_ferret.size
|
217
|
+
contents_from_ferret = ActsAsFerret::find('title:(title OR comment) OR description:(title OR comment) OR content:(title OR comment)', [Content, Comment])
|
218
|
+
assert_equal 5, contents_from_ferret.size
|
219
|
+
end
|
220
|
+
|
221
|
+
def test_lazy_search
|
222
|
+
contents_from_ferret = ActsAsFerret.find('title', [Content, Comment], :lazy => true)
|
223
|
+
assert_equal 2, contents_from_ferret.size
|
224
|
+
contents_from_ferret.each do |record|
|
225
|
+
assert ActsAsFerret::FerretResult === record, record.inspect
|
226
|
+
assert !record.description.blank?
|
227
|
+
assert_nil record.instance_variable_get(:"@ar_record")
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
231
|
+
def test_find_ids
|
232
|
+
assert_equal 4, ContentBase.find(:all).size
|
233
|
+
|
234
|
+
[ 'title:title OR description:title OR content:title', 'title', '*:title'].each do |query|
|
235
|
+
total_hits, contents_from_ferret = ActsAsFerret.find_ids(query, Content)
|
236
|
+
assert_equal 2, contents_from_ferret.size, query
|
237
|
+
assert_equal 2, total_hits, query
|
238
|
+
assert_equal contents(:first).id, contents_from_ferret.first[:id].to_i
|
239
|
+
assert_equal @another_content.id, contents_from_ferret.last[:id].to_i
|
240
|
+
end
|
241
|
+
|
242
|
+
ContentBase.rebuild_index
|
243
|
+
Comment.rebuild_index
|
244
|
+
['title OR comment', 'title:(title OR comment) OR description:(title OR comment) OR content:(title OR comment)'].each do |query|
|
245
|
+
total_hits, contents_from_ferret = ActsAsFerret.find_ids(query, [Comment, Content])
|
246
|
+
assert_equal 5, contents_from_ferret.size, query
|
247
|
+
assert_equal 5, total_hits
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
def test_find_ids_lazy
|
252
|
+
total_hits, contents_from_ferret = ActsAsFerret.find_ids('title', [Comment, Content], :lazy => true)
|
253
|
+
assert_equal 2, contents_from_ferret.size
|
254
|
+
assert_equal 2, total_hits
|
255
|
+
found = 0
|
256
|
+
contents_from_ferret.each do |data|
|
257
|
+
next if data[:model] != 'Content'
|
258
|
+
found += 1
|
259
|
+
assert !data[:data][:description].blank?
|
260
|
+
end
|
261
|
+
assert_equal 2, found
|
262
|
+
end
|
263
|
+
|
264
|
+
def test_pagination
|
265
|
+
more_contents(true)
|
266
|
+
|
267
|
+
r = ActsAsFerret.find 'title OR comment', [ Content, Comment ], :per_page => 10, :sort => 'title'
|
268
|
+
assert_equal 60, r.total_hits
|
269
|
+
assert_equal 10, r.size
|
270
|
+
assert_equal "0", r.first.description
|
271
|
+
assert_equal "9", r.last.description
|
272
|
+
assert_equal 1, r.current_page
|
273
|
+
assert_equal 6, r.page_count
|
274
|
+
|
275
|
+
r = ActsAsFerret.find 'title OR comment', [ Content, Comment ], :page => '2', :per_page => 10, :sort => 'title'
|
276
|
+
assert_equal 60, r.total_hits
|
277
|
+
assert_equal 10, r.size
|
278
|
+
assert_equal "10", r.first.description
|
279
|
+
assert_equal "19", r.last.description
|
280
|
+
assert_equal 2, r.current_page
|
281
|
+
assert_equal 6, r.page_count
|
282
|
+
|
283
|
+
r = ActsAsFerret.find 'title OR comment', [ Content, Comment ], :page => 7, :per_page => 10, :sort => 'title'
|
284
|
+
assert_equal 60, r.total_hits
|
285
|
+
assert_equal 0, r.size
|
286
|
+
end
|
287
|
+
|
288
|
+
def test_pagination_with_ar_conditions
|
289
|
+
more_contents(true)
|
290
|
+
id = Content.find_with_ferret('title').first.id
|
291
|
+
r = ActsAsFerret.find 'title OR comment', [Content, Comment], { :page => 1, :per_page => 10 },
|
292
|
+
{ :conditions => { :content => ["id != ?", id] }, :order => 'id ASC' }
|
293
|
+
assert_equal 59, r.total_hits
|
294
|
+
assert_equal 10, r.size
|
295
|
+
assert_equal "Comment for content 00", r.first.content
|
296
|
+
assert_equal "Comment for content 09", r.last.content
|
297
|
+
assert_equal 1, r.current_page
|
298
|
+
assert_equal 6, r.page_count
|
299
|
+
|
300
|
+
r = ActsAsFerret.find 'title OR comment', [Content, Comment], { :page => 6, :per_page => 10 },
|
301
|
+
{ :conditions => { :content => [ "id != ?", id ] }, :order => 'id ASC' }
|
302
|
+
assert_equal 59, r.total_hits
|
303
|
+
assert_equal 9, r.size
|
304
|
+
assert_equal "21", r.first.description
|
305
|
+
assert_equal "29", r.last.description
|
306
|
+
assert_equal 6, r.current_page
|
307
|
+
assert_equal 6, r.page_count
|
308
|
+
end
|
309
|
+
|
310
|
+
protected
|
311
|
+
|
312
|
+
def more_contents(with_comments = false)
|
313
|
+
Comment.destroy_all if with_comments
|
314
|
+
Content.destroy_all
|
315
|
+
SpecialContent.destroy_all
|
316
|
+
30.times do |i|
|
317
|
+
c = Content.create! :title => sprintf("title of Content %02d", i), :description => "#{i}"
|
318
|
+
c.comments.create! :content => sprintf("Comment for content %02d", i) if with_comments
|
319
|
+
end
|
320
|
+
end
|
321
|
+
|
322
|
+
def remove_index(clazz)
|
323
|
+
clazz.aaf_index.close # avoid io error when deleting the open index
|
324
|
+
FileUtils.rm_rf clazz.aaf_configuration[:index_dir]
|
325
|
+
assert !File.exists?("#{clazz.aaf_configuration[:index_dir]}/segments")
|
326
|
+
end
|
327
|
+
|
328
|
+
end
|
329
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class RemoteIndexTest < Test::Unit::TestCase
|
4
|
+
def setup
|
5
|
+
ActsAsFerret::remote = 'druby://localhost:9999'
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_raises_drb_errors
|
9
|
+
ActsAsFerret::raise_drb_errors = true
|
10
|
+
@srv = ActsAsFerret::RemoteIndex.new :name => 'idx'
|
11
|
+
assert_raise DRb::DRbConnError do
|
12
|
+
@srv.find_ids 'some query'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_does_not_raise_drb_errors
|
17
|
+
ActsAsFerret::raise_drb_errors = false
|
18
|
+
@srv = ActsAsFerret::RemoteIndex.new :name => 'idx'
|
19
|
+
total_hits, results = @srv.find_ids( 'some query' )
|
20
|
+
assert_equal 0, total_hits
|
21
|
+
assert results.empty?
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class SharedIndex1Test < Test::Unit::TestCase
|
4
|
+
fixtures :shared_index1s, :shared_index2s
|
5
|
+
|
6
|
+
def setup
|
7
|
+
SharedIndex1.rebuild_index
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_lazy_loading
|
11
|
+
results = ActsAsFerret::find 'first', 'shared', :lazy => [ :name ]
|
12
|
+
assert_equal 2, results.size
|
13
|
+
found_lazy_result = false
|
14
|
+
results.each { |r|
|
15
|
+
assert ActsAsFerret::FerretResult === r
|
16
|
+
assert !r.name.blank?
|
17
|
+
assert_nil r.instance_variable_get(:@ar_record) # lazy, AR record has not been fetched
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_find
|
22
|
+
assert_equal shared_index1s(:first), SharedIndex1.find(1)
|
23
|
+
assert_equal shared_index2s(:first), SharedIndex2.find(1)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_find_ids_with_ferret
|
27
|
+
result = SharedIndex1.find_ids_with_ferret("first")
|
28
|
+
assert_equal 2, result.size
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_find_with_ferret_one_class
|
32
|
+
result = SharedIndex1.find_with_ferret("first")
|
33
|
+
assert_equal 1, result.size, result.inspect
|
34
|
+
assert_equal shared_index1s(:first), result.first
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_custom_query
|
38
|
+
result = SharedIndex1.find_with_ferret("name:first class_name:SharedIndex1")
|
39
|
+
assert_equal 1, result.size
|
40
|
+
assert_equal shared_index1s(:first), result.first
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_find_with_index_name
|
44
|
+
result = ActsAsFerret::find("first", 'shared')
|
45
|
+
assert_equal 2, result.size
|
46
|
+
assert result.include?(shared_index1s(:first))
|
47
|
+
assert result.include?(shared_index2s(:first))
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_find_with_class_list
|
51
|
+
result = ActsAsFerret::find("name:first", [SharedIndex1, SharedIndex2])
|
52
|
+
assert_equal 2, result.size
|
53
|
+
assert result.include?(shared_index1s(:first))
|
54
|
+
assert result.include?(shared_index2s(:first))
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_query_for_record
|
58
|
+
assert_match /SharedIndex1/, shared_index1s(:first).query_for_record.to_s
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_destroy
|
62
|
+
result = ActsAsFerret::find("first OR another", 'shared')
|
63
|
+
assert_equal 4, result.size
|
64
|
+
SharedIndex1.destroy(shared_index1s(:first))
|
65
|
+
result = ActsAsFerret::find("first OR another", 'shared')
|
66
|
+
assert_equal 3, result.size
|
67
|
+
shared_index2s(:first).destroy
|
68
|
+
result = ActsAsFerret::find("first OR another", 'shared')
|
69
|
+
assert_equal 2, result.size
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_ferret_destroy
|
73
|
+
SharedIndex1.rebuild_index
|
74
|
+
result = SharedIndex1.find_ids_with_ferret("first OR another", :models => :all)
|
75
|
+
assert_equal 4, result.first
|
76
|
+
shared_index1s(:first).ferret_destroy
|
77
|
+
result = SharedIndex1.find_ids_with_ferret("first OR another", :models => :all)
|
78
|
+
assert_equal 3, result.first
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_ferret_destroy_ticket_88
|
82
|
+
SharedIndex1.rebuild_index
|
83
|
+
result = SharedIndex1.find_ids_with_ferret("first OR another", :models => :all)
|
84
|
+
assert_equal 4, result.first
|
85
|
+
result = SharedIndex2.find_ids_with_ferret("first OR another", :models => :all)
|
86
|
+
assert_equal 4, result.first
|
87
|
+
SharedIndex1.destroy(shared_index1s(:first))
|
88
|
+
result = SharedIndex1.find_ids_with_ferret("first OR another", :models => :all)
|
89
|
+
assert_equal 3, result.first
|
90
|
+
result = SharedIndex2.find_ids_with_ferret("first OR another", :models => :all)
|
91
|
+
assert_equal 3, result.first
|
92
|
+
shared_index2s(:first).destroy
|
93
|
+
result = SharedIndex1.find_ids_with_ferret("first OR another", :models => :all)
|
94
|
+
assert_equal 2, result.first
|
95
|
+
result = SharedIndex2.find_ids_with_ferret("first OR another", :models => :all)
|
96
|
+
assert_equal 2, result.first
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_update
|
100
|
+
assert SharedIndex1.find_with_ferret("new").empty?
|
101
|
+
shared_index1s(:first).name = "new name"
|
102
|
+
shared_index1s(:first).save
|
103
|
+
assert_equal 1, SharedIndex1.find_with_ferret("new").size
|
104
|
+
assert_equal 1, SharedIndex1.find_with_ferret("new").size
|
105
|
+
assert_equal 1, SharedIndex1.find_with_ferret("new", :models => [SharedIndex2]).size
|
106
|
+
assert_equal 0, SharedIndex2.find_with_ferret("new").size
|
107
|
+
end
|
108
|
+
end
|