| 1 |
require File.dirname(__FILE__) + '/../test_helper' |
|---|
| 2 |
require 'pp' |
|---|
| 3 |
require 'fileutils' |
|---|
| 4 |
|
|---|
| 5 |
class ContentTest < 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 |
def test_limit_all |
|---|
| 31 |
res = Content.find_with_ferret '*', { :limit => :all }, :conditions => ['lower(title) like ?', 'content'], :order => 'contents.description' |
|---|
| 32 |
end |
|---|
| 33 |
|
|---|
| 34 |
def test_find_with_ferret_on_has_many_assoc |
|---|
| 35 |
c = contents(:first) |
|---|
| 36 |
comments = c.comments.find_with_ferret 'second' |
|---|
| 37 |
assert_equal 1, comments.size |
|---|
| 38 |
assert_equal comments(:another), comments.first |
|---|
| 39 |
end |
|---|
| 40 |
|
|---|
| 41 |
def test_total_hits_on_has_many_assoc |
|---|
| 42 |
c = contents(:first) |
|---|
| 43 |
assert_equal 2, Comment.find_with_ferret('second OR regarding').total_hits |
|---|
| 44 |
assert_equal 1, c.comments.find_with_ferret('second OR regarding').total_hits |
|---|
| 45 |
end |
|---|
| 46 |
|
|---|
| 47 |
def test_include_option |
|---|
| 48 |
assert_equal 1, Content.find_with_ferret('description', {}, :include => :comments).size |
|---|
| 49 |
end |
|---|
| 50 |
|
|---|
| 51 |
def test_lazy_loading |
|---|
| 52 |
results = Content.find_with_ferret 'description', :lazy => true |
|---|
| 53 |
assert_equal 1, results.size |
|---|
| 54 |
result = results.first |
|---|
| 55 |
class << result |
|---|
| 56 |
attr_accessor :ar_record # so we have a chance to check if it's been loaded... |
|---|
| 57 |
end |
|---|
| 58 |
assert ActsAsFerret::FerretResult === result |
|---|
| 59 |
assert_equal 'A useless description', result.description |
|---|
| 60 |
assert_nil result.instance_variable_get(:@ar_record) |
|---|
| 61 |
assert_equal 'My Title', result.title |
|---|
| 62 |
assert_not_nil result.ar_record |
|---|
| 63 |
end |
|---|
| 64 |
|
|---|
| 65 |
def test_ticket_69 |
|---|
| 66 |
content = Content.create(:title => 'aksjeselskap test', |
|---|
| 67 |
:description => 'content about various norwegian companies. A.s. Haakon, Ã |
|---|
| 68 |
mot HÃ¥ndverksenter A/S, Ãye Trelast AS') |
|---|
| 69 |
|
|---|
| 70 |
# these still fail: 'A\S', 'AS' |
|---|
| 71 |
[ '"A.s. Haakon"', 'A.s. Haakon', 'Ã |
|---|
| 72 |
mot A/S', 'A/S' ].each do |query| |
|---|
| 73 |
assert_equal content, Content.find_with_ferret(query).first, query |
|---|
| 74 |
end |
|---|
| 75 |
end |
|---|
| 76 |
|
|---|
| 77 |
def test_highlight |
|---|
| 78 |
highlight = @another_content.highlight('title') |
|---|
| 79 |
assert_equal 1, highlight.size |
|---|
| 80 |
assert_equal "this is not the <em>title</em>", highlight.first |
|---|
| 81 |
|
|---|
| 82 |
highlight = @another_content.highlight('title', :field => :description) |
|---|
| 83 |
assert_equal 1, highlight.size |
|---|
| 84 |
assert_equal "this is not the <em>title</em>", highlight.first |
|---|
| 85 |
end |
|---|
| 86 |
|
|---|
| 87 |
def test_highlight_new_record |
|---|
| 88 |
c = Content.create :title => 'the title', :description => 'the new description' |
|---|
| 89 |
highlight = c.highlight('new') |
|---|
| 90 |
assert_equal 1, highlight.size |
|---|
| 91 |
assert_equal "the <em>new</em> description", highlight.first |
|---|
| 92 |
|
|---|
| 93 |
c1 = Content.find_with_ferret('new description').first |
|---|
| 94 |
assert_equal c, c1 |
|---|
| 95 |
highlight = c1.highlight('new') |
|---|
| 96 |
assert_equal 1, highlight.size |
|---|
| 97 |
assert_equal "the <em>new</em> description", highlight.first |
|---|
| 98 |
end |
|---|
| 99 |
|
|---|
| 100 |
def test_disable_ferret_once |
|---|
| 101 |
content = Content.new(:title => 'should not get saved', :description => 'do not find me') |
|---|
| 102 |
assert_raises (ArgumentError) do |
|---|
| 103 |
content.disable_ferret(:wrong) |
|---|
| 104 |
end |
|---|
| 105 |
assert content.ferret_enabled? |
|---|
| 106 |
content.disable_ferret |
|---|
| 107 |
assert !content.ferret_enabled? |
|---|
| 108 |
content.save |
|---|
| 109 |
assert content.ferret_enabled? |
|---|
| 110 |
assert Content.find_with_ferret('"find me"').empty? |
|---|
| 111 |
|
|---|
| 112 |
content.save |
|---|
| 113 |
assert content.ferret_enabled? |
|---|
| 114 |
assert_equal content, Content.find_with_ferret('"find me"').first |
|---|
| 115 |
end |
|---|
| 116 |
|
|---|
| 117 |
def test_ferret_disable_always |
|---|
| 118 |
content = Content.new(:title => 'should not get saved', :description => 'do not find me') |
|---|
| 119 |
assert content.ferret_enabled? |
|---|
| 120 |
content.disable_ferret(:always) |
|---|
| 121 |
assert !content.ferret_enabled? |
|---|
| 122 |
2.times do |
|---|
| 123 |
content.save |
|---|
| 124 |
assert Content.find_with_ferret('"find me"').empty? |
|---|
| 125 |
assert !content.ferret_enabled? |
|---|
| 126 |
end |
|---|
| 127 |
content.ferret_enable |
|---|
| 128 |
assert content.ferret_enabled? |
|---|
| 129 |
content.save |
|---|
| 130 |
assert content.ferret_enabled? |
|---|
| 131 |
assert_equal content, Content.find_with_ferret('"find me"').first |
|---|
| 132 |
end |
|---|
| 133 |
|
|---|
| 134 |
def test_disable_ferret_on_class_level |
|---|
| 135 |
Content.disable_ferret |
|---|
| 136 |
content = Content.new(:title => 'should not get saved', :description => 'do not find me') |
|---|
| 137 |
assert !content.ferret_enabled? |
|---|
| 138 |
assert !Content.ferret_enabled? |
|---|
| 139 |
2.times do |
|---|
| 140 |
content.save |
|---|
| 141 |
assert Content.find_with_ferret('"find me"').empty? |
|---|
| 142 |
assert !Content.ferret_enabled? |
|---|
| 143 |
assert !content.ferret_enabled? |
|---|
| 144 |
end |
|---|
| 145 |
content.enable_ferret # record level enabling should have no effect on class level |
|---|
| 146 |
assert !Content.ferret_enabled? |
|---|
| 147 |
assert !content.ferret_enabled? |
|---|
| 148 |
Content.enable_ferret |
|---|
| 149 |
assert Content.ferret_enabled? |
|---|
| 150 |
assert content.ferret_enabled? |
|---|
| 151 |
|
|---|
| 152 |
content.save |
|---|
| 153 |
assert content.ferret_enabled? |
|---|
| 154 |
assert Content.ferret_enabled? |
|---|
| 155 |
assert_equal content, Content.find_with_ferret('"find me"').first |
|---|
| 156 |
end |
|---|
| 157 |
|
|---|
| 158 |
def test_disable_ferret_block |
|---|
| 159 |
content = Content.new(:title => 'should not get saved', :description => 'do not find me') |
|---|
| 160 |
content.disable_ferret do |
|---|
| 161 |
2.times do |
|---|
| 162 |
content.save |
|---|
| 163 |
assert Content.find_with_ferret('"find me"').empty? |
|---|
| 164 |
assert !content.ferret_enabled? |
|---|
| 165 |
end |
|---|
| 166 |
end |
|---|
| 167 |
assert content.ferret_enabled? |
|---|
| 168 |
assert Content.find_with_ferret('"find me"').empty? |
|---|
| 169 |
|
|---|
| 170 |
content.disable_ferret(:index_when_finished) do |
|---|
| 171 |
2.times do |
|---|
| 172 |
content.save |
|---|
| 173 |
assert Content.find_with_ferret('"find me"').empty? |
|---|
| 174 |
assert !content.ferret_enabled? |
|---|
| 175 |
end |
|---|
| 176 |
end |
|---|
| 177 |
assert content.ferret_enabled? |
|---|
| 178 |
assert_equal content, Content.find_with_ferret('"find me"').first |
|---|
| 179 |
end |
|---|
| 180 |
|
|---|
| 181 |
def test_disable_ferret_on_class_level_block |
|---|
| 182 |
content = Content.new(:title => 'should not get saved', :description => 'do not find me') |
|---|
| 183 |
Content.disable_ferret do |
|---|
| 184 |
2.times do |
|---|
| 185 |
content.save |
|---|
| 186 |
assert Content.find_with_ferret('"find me"').empty? |
|---|
| 187 |
assert !content.ferret_enabled? |
|---|
| 188 |
assert !Content.ferret_enabled? |
|---|
| 189 |
end |
|---|
| 190 |
end |
|---|
| 191 |
assert content.ferret_enabled? |
|---|
| 192 |
assert Content.ferret_enabled? |
|---|
| 193 |
assert Content.find_with_ferret('"find me"').empty? |
|---|
| 194 |
content.save |
|---|
| 195 |
assert_equal content, Content.find_with_ferret('"find me"').first |
|---|
| 196 |
end |
|---|
| 197 |
|
|---|
| 198 |
# ticket 178 |
|---|
| 199 |
def test_records_for_rebuild_works_with_includes |
|---|
| 200 |
size = Content.count |
|---|
| 201 |
Content.send( :with_scope, :find => { :include => :comments } ) do |
|---|
| 202 |
Content.records_for_rebuild do |records, offset| |
|---|
| 203 |
assert_equal size, records.size |
|---|
| 204 |
end |
|---|
| 205 |
end |
|---|
| 206 |
end |
|---|
| 207 |
|
|---|
| 208 |
def test_records_for_bulk_index |
|---|
| 209 |
Content.disable_ferret do |
|---|
| 210 |
more_contents |
|---|
| 211 |
end |
|---|
| 212 |
min = Content.find(:all, :order => 'id asc').first.id |
|---|
| 213 |
Content.records_for_bulk_index([min, min+1, min+2, min+3, min+4, min+6], 10) do |records, offset| |
|---|
| 214 |
assert_equal 6, records.size |
|---|
| 215 |
end |
|---|
| 216 |
end |
|---|
| 217 |
|
|---|
| 218 |
def test_bulk_index_no_optimize |
|---|
| 219 |
Content.disable_ferret do |
|---|
| 220 |
more_contents |
|---|
| 221 |
end |
|---|
| 222 |
|
|---|
| 223 |
assert Content.find_with_ferret('title').empty? |
|---|
| 224 |
min = Content.find(:all, :order => 'id asc').first.id |
|---|
| 225 |
Content.bulk_index(min, min+1, min+2, min+3, min+4, min+6, :optimize => false) |
|---|
| 226 |
assert_equal 6, Content.find_with_ferret('title').size |
|---|
| 227 |
end |
|---|
| 228 |
|
|---|
| 229 |
def test_bulk_index |
|---|
| 230 |
Content.disable_ferret do |
|---|
| 231 |
more_contents |
|---|
| 232 |
end |
|---|
| 233 |
|
|---|
| 234 |
assert Content.find_with_ferret('title').empty? |
|---|
| 235 |
min = Content.find(:all, :order => 'id asc').first.id |
|---|
| 236 |
Content.bulk_index([min, min+1, min+2, min+3, min+4, min+6]) |
|---|
| 237 |
assert_equal 6, Content.find_with_ferret('title').size |
|---|
| 238 |
end |
|---|
| 239 |
|
|---|
| 240 |
|
|---|
| 241 |
def test_unicode |
|---|
| 242 |
content = Content.new(:title => 'Title with some ÃmlÀuts - ÀöÌ', |
|---|
| 243 |
:description => 'look - an Ã') |
|---|
| 244 |
content.save |
|---|
| 245 |
result = Content.find_with_ferret('ÀöÌ') |
|---|
| 246 |
assert_equal content, result.first |
|---|
| 247 |
result = Content.find_with_ferret('ÃŒml*') |
|---|
| 248 |
assert_equal content, result.first |
|---|
| 249 |
result = Content.find_with_ferret('Ã') |
|---|
| 250 |
assert_equal content, result.first |
|---|
| 251 |
end |
|---|
| 252 |
|
|---|
| 253 |
def test_content_for_field_name |
|---|
| 254 |
c = 'lorem ipsum dolor sit amet. lorem.' |
|---|
| 255 |
@c1 = Content.new( :title => 'Content item 1', |
|---|
| 256 |
:description => c ) |
|---|
| 257 |
assert_equal c, @c1.content_for_field_name(:description) |
|---|
| 258 |
end |
|---|
| 259 |
|
|---|
| 260 |
def test_document_number |
|---|
| 261 |
c = 'lorem ipsum dolor sit amet. lorem.' |
|---|
| 262 |
c1 = Content.new( :title => 'Content item 1', |
|---|
| 263 |
:description => c ) |
|---|
| 264 |
c1.save |
|---|
| 265 |
fi = Content.aaf_index.ferret_index |
|---|
| 266 |
assert fi |
|---|
| 267 |
hits = fi.search('title:"Content item 1"') |
|---|
| 268 |
assert_equal 1, hits.total_hits |
|---|
| 269 |
expected_doc_num = hits.hits.first.doc |
|---|
| 270 |
assert_equal c, fi[expected_doc_num][:description] |
|---|
| 271 |
doc_num = c1.document_number |
|---|
| 272 |
assert_equal expected_doc_num, doc_num |
|---|
| 273 |
assert_equal c, fi[doc_num][:description] |
|---|
| 274 |
end |
|---|
| 275 |
|
|---|
| 276 |
def test_more_like_this |
|---|
| 277 |
assert Content.find_with_ferret('lorem ipsum').empty? |
|---|
| 278 |
@c1 = Content.new( :title => 'Content item 1', |
|---|
| 279 |
:description => 'lorem ipsum dolor sit amet. lorem.' ) |
|---|
| 280 |
@c1.save |
|---|
| 281 |
@c2 = Content.new( :title => 'Content item 2', |
|---|
| 282 |
:description => 'lorem ipsum dolor sit amet. lorem ipsum.' ) |
|---|
| 283 |
@c2.save |
|---|
| 284 |
assert_equal 2, Content.find_with_ferret('lorem ipsum').size |
|---|
| 285 |
similar = @c1.more_like_this(:field_names => [:description], :min_doc_freq => 1, :min_term_freq => 1) |
|---|
| 286 |
assert_equal 1, similar.size |
|---|
| 287 |
assert_equal @c2, similar.first |
|---|
| 288 |
end |
|---|
| 289 |
|
|---|
| 290 |
def test_more_like_this_new_record |
|---|
| 291 |
assert Content.find_with_ferret('lorem ipsum').empty? |
|---|
| 292 |
@c1 = Content.new( :title => 'Content item 1', |
|---|
| 293 |
:description => 'lorem ipsum dolor sit amet. lorem.' ) |
|---|
| 294 |
@c2 = Content.new( :title => 'Content item 2', |
|---|
| 295 |
:description => 'lorem ipsum dolor sit amet. lorem ipsum.' ) |
|---|
| 296 |
@c2.save |
|---|
| 297 |
assert_equal 1, Content.find_with_ferret('lorem ipsum').size |
|---|
| 298 |
similar = @c1.more_like_this(:field_names => [:description], :min_doc_freq => 1, :min_term_freq => 1) |
|---|
| 299 |
assert_equal 1, similar.size |
|---|
| 300 |
assert_equal @c2, similar.first |
|---|
| 301 |
end |
|---|
| 302 |
|
|---|
| 303 |
def test_class_index_dir |
|---|
| 304 |
assert Content.aaf_configuration[:index_dir] =~ %r{^#{RAILS_ROOT}/index/test/content_base} |
|---|
| 305 |
end |
|---|
| 306 |
|
|---|
| 307 |
def test_update |
|---|
| 308 |
contents_from_ferret = Content.find_with_ferret('useless') |
|---|
| 309 |
assert_equal 1, contents_from_ferret.size |
|---|
| 310 |
assert_equal contents(:first).id, contents_from_ferret.first.id |
|---|
| 311 |
contents(:first).description = 'Updated description, still useless' |
|---|
| 312 |
contents(:first).save |
|---|
| 313 |
contents_from_ferret = Content.find_with_ferret('useless') |
|---|
| 314 |
assert_equal 1, contents_from_ferret.size |
|---|
| 315 |
assert_equal contents(:first).id, contents_from_ferret.first.id |
|---|
| 316 |
contents_from_ferret = Content.find_with_ferret('updated AND description') |
|---|
| 317 |
assert_equal 1, contents_from_ferret.size |
|---|
| 318 |
assert_equal contents(:first).id, contents_from_ferret.first.id |
|---|
| 319 |
contents_from_ferret = Content.find_with_ferret('updated OR description') |
|---|
| 320 |
assert_equal 1, contents_from_ferret.size |
|---|
| 321 |
assert_equal contents(:first).id, contents_from_ferret.first.id |
|---|
| 322 |
end |
|---|
| 323 |
|
|---|
| 324 |
def test_indexed_method |
|---|
| 325 |
assert_equal 2, @another_content.comment_count |
|---|
| 326 |
assert_equal 2, contents(:first).comment_count |
|---|
| 327 |
assert_equal 1, contents(:another).comment_count |
|---|
| 328 |
# retrieve all content objects having 2 comments |
|---|
| 329 |
result = Content.find_with_ferret('comment_count:2') |
|---|
| 330 |
# TODO check why this range query returns 3 results |
|---|
| 331 |
#result = Content.find_with_ferret('comment_count:[2 TO 1000]') |
|---|
| 332 |
# p result |
|---|
| 333 |
assert_equal 2, result.size |
|---|
| 334 |
assert result.include?(@another_content) |
|---|
| 335 |
assert result.include?(contents(:first)) |
|---|
| 336 |
end |
|---|
| 337 |
|
|---|
| 338 |
def test_sorting |
|---|
| 339 |
sorting = [ Ferret::Search::SortField.new(:id, :reverse => true) ] |
|---|
| 340 |
result = Content.find_with_ferret('comment_count:2', :sort => sorting) |
|---|
| 341 |
assert !result.empty? |
|---|
| 342 |
assert result.first.id > result.last.id |
|---|
| 343 |
|
|---|
| 344 |
sorting = [ Ferret::Search::SortField.new(:id) ] |
|---|
| 345 |
result = Content.find_with_ferret('comment_count:2', :sort => sorting) |
|---|
| 346 |
assert result.first.id < result.last.id |
|---|
| 347 |
|
|---|
| 348 |
sorting = Ferret::Search::Sort.new([ Ferret::Search::SortField.new(:id), |
|---|
| 349 |
Ferret::Search::SortField::SCORE ], |
|---|
| 350 |
:reverse => true) |
|---|
| 351 |
|
|---|
| 352 |
|
|---|
| 353 |
result = Content.find_with_ferret('comment_count:2', :sort => sorting) |
|---|
| 354 |
assert result.first.id > result.last.id |
|---|
| 355 |
end |
|---|
| 356 |
|
|---|
| 357 |
def test_sort_class |
|---|
| 358 |
sorting = Ferret::Search::Sort.new(Ferret::Search::SortField.new(:id, :reverse => true)) |
|---|
| 359 |
result = Content.find_with_ferret('comment_count:2 OR comment_count:1', :sort => sorting) |
|---|
| 360 |
assert result.size > 2 |
|---|
| 361 |
assert result.first.id > result.last.id |
|---|
| 362 |
result = Content.find_with_ferret('comment_count:2 OR comment_count:1', :sort => sorting, :limit => 2) |
|---|
| 363 |
assert_equal 2, result.size |
|---|
| 364 |
assert result.first.id > result.last.id |
|---|
| 365 |
end |
|---|
| 366 |
|
|---|
| 367 |
def test_sort_with_limit |
|---|
| 368 |
sorting = [ Ferret::Search::SortField.new(:id) ] |
|---|
| 369 |
result = Content.find_with_ferret('comment_count:2 OR comment_count:1', :sort => sorting) |
|---|
| 370 |
assert result.size > 2 |
|---|
| 371 |
assert result.first.id < result.last.id |
|---|
| 372 |
result = Content.find_with_ferret('comment_count:2 OR comment_count:1', :sort => sorting, :limit => 2) |
|---|
| 373 |
assert_equal 2, result.size |
|---|
| 374 |
assert result.first.id < result.last.id |
|---|
| 375 |
|
|---|
| 376 |
sorting = [ Ferret::Search::SortField.new(:id, :reverse => true) ] |
|---|
| 377 |
result = Content.find_with_ferret('comment_count:2 OR comment_count:1', :sort => sorting) |
|---|
| 378 |
assert result.size > 2 |
|---|
| 379 |
assert result.first.id > result.last.id |
|---|
| 380 |
result = Content.find_with_ferret('comment_count:2 OR comment_count:1', :sort => sorting, :limit => 2) |
|---|
| 381 |
assert_equal 2, result.size |
|---|
| 382 |
assert result.first.id > result.last.id |
|---|
| 383 |
end |
|---|
| 384 |
|
|---|
| 385 |
|
|---|
| 386 |
def test_add_rebuilds_index |
|---|
| 387 |
remove_index Content |
|---|
| 388 |
Content.create(:title => 'another one', :description => 'description') |
|---|
| 389 |
contents_from_ferret = Content.find_with_ferret('description:title') |
|---|
| 390 |
assert_equal 1, contents_from_ferret.size |
|---|
| 391 |
end |
|---|
| 392 |
def test_find_rebuilds_index |
|---|
| 393 |
remove_index Content |
|---|
| 394 |
contents_from_ferret = Content.find_with_ferret('description:title') |
|---|
| 395 |
assert_equal 1, contents_from_ferret.size |
|---|
| 396 |
end |
|---|
| 397 |
|
|---|
| 398 |
def test_total_hits |
|---|
| 399 |
assert_equal 2, Content.total_hits('title:title OR description:title') |
|---|
| 400 |
assert_equal 2, Content.total_hits('title:title OR description:title', :limit => 1) |
|---|
| 401 |
end |
|---|
| 402 |
|
|---|
| 403 |
def test_find_ids_with_ferret |
|---|
| 404 |
total_hits, contents_from_ferret = Content.find_ids_with_ferret('title:title OR description:title') |
|---|
| 405 |
assert_equal 2, contents_from_ferret.size |
|---|
| 406 |
assert_equal 2, total_hits |
|---|
| 407 |
#puts "first (id=#{contents_from_ferret.first[:id]}): #{contents_from_ferret.first[:score]}" |
|---|
| 408 |
#puts "last (id=#{contents_from_ferret.last[:id]}): #{contents_from_ferret.last[:score]}" |
|---|
| 409 |
assert_equal contents(:first).id, contents_from_ferret.first[:id].to_i |
|---|
| 410 |
assert_equal @another_content.id, contents_from_ferret.last[:id].to_i |
|---|
| 411 |
assert contents_from_ferret.first[:score] >= contents_from_ferret.last[:score] |
|---|
| 412 |
|
|---|
| 413 |
# give description field higher boost: |
|---|
| 414 |
total_hits, contents_from_ferret = Content.find_ids_with_ferret('title:title OR description:title^200') |
|---|
| 415 |
assert_equal 2, contents_from_ferret.size |
|---|
| 416 |
assert_equal 2, total_hits |
|---|
| 417 |
#puts "first (id=#{contents_from_ferret.first[:id]}): #{contents_from_ferret.first[:score]}" |
|---|
| 418 |
#puts "last (id=#{contents_from_ferret.last[:id]}): #{contents_from_ferret.last[:score]}" |
|---|
| 419 |
assert_equal @another_content.id, contents_from_ferret.first[:id].to_i |
|---|
| 420 |
assert_equal contents(:first).id, contents_from_ferret.last[:id].to_i |
|---|
| 421 |
assert contents_from_ferret.first[:score] > contents_from_ferret.last[:score] |
|---|
| 422 |
|
|---|
| 423 |
end |
|---|
| 424 |
|
|---|
| 425 |
def test_find_with_ferret_boost |
|---|
| 426 |
# give description field higher boost: |
|---|
| 427 |
contents_from_ferret = Content.find_with_ferret('title:title OR description:title^200') |
|---|
| 428 |
assert_equal 2, contents_from_ferret.size |
|---|
| 429 |
assert_equal @another_content.id, contents_from_ferret.first.id |
|---|
| 430 |
assert_equal contents(:first).id, contents_from_ferret.last.id |
|---|
| 431 |
end |
|---|
| 432 |
|
|---|
| 433 |
def test_default_and_queries |
|---|
| 434 |
# multiple terms are ANDed by default... |
|---|
| 435 |
contents_from_ferret = Content.find_with_ferret('monkey description') |
|---|
| 436 |
assert contents_from_ferret.empty? |
|---|
| 437 |
# ...unless you connect them by OR |
|---|
| 438 |
contents_from_ferret = Content.find_with_ferret('monkey OR description') |
|---|
| 439 |
assert_equal 1, contents_from_ferret.size |
|---|
| 440 |
assert_equal contents(:first).id, contents_from_ferret.first.id |
|---|
| 441 |
|
|---|
| 442 |
# multiple terms, each term has to occur in a document to be found, |
|---|
| 443 |
# but they may occur in different fields |
|---|
| 444 |
contents_from_ferret = Content.find_with_ferret('useless title') |
|---|
| 445 |
assert_equal 1, contents_from_ferret.size |
|---|
| 446 |
assert_equal contents(:first).id, contents_from_ferret.first.id |
|---|
| 447 |
end |
|---|
| 448 |
|
|---|
| 449 |
def test_find_with_ferret |
|---|
| 450 |
|
|---|
| 451 |
contents_from_ferret = Content.find_with_ferret('lorem ipsum not here') |
|---|
| 452 |
assert contents_from_ferret.empty? |
|---|
| 453 |
|
|---|
| 454 |
contents_from_ferret = Content.find_with_ferret('title') |
|---|
| 455 |
assert_equal 2, contents_from_ferret.size |
|---|
| 456 |
# the title field has a higher boost value, so contents(:first) must be first in the list |
|---|
| 457 |
assert_equal contents(:first).id, contents_from_ferret.first.id |
|---|
| 458 |
assert_equal @another_content.id, contents_from_ferret.last.id |
|---|
| 459 |
|
|---|
| 460 |
|
|---|
| 461 |
|
|---|
| 462 |
contents_from_ferret = Content.find_with_ferret('useless') |
|---|
| 463 |
assert_equal 1, contents_from_ferret.size |
|---|
| 464 |
assert_equal contents(:first).id, contents_from_ferret.first.id |
|---|
| 465 |
|
|---|
| 466 |
# no monkeys here |
|---|
| 467 |
contents_from_ferret = Content.find_with_ferret('monkey') |
|---|
| 468 |
assert contents_from_ferret.empty? |
|---|
| 469 |
|
|---|
| 470 |
|
|---|
| 471 |
|
|---|
| 472 |
# search for an exact string by enclosing it in " |
|---|
| 473 |
contents_from_ferret = Content.find_with_ferret('"useless title"') |
|---|
| 474 |
assert contents_from_ferret.empty? |
|---|
| 475 |
contents_from_ferret = Content.find_with_ferret('"useless description"') |
|---|
| 476 |
assert_equal 1, contents_from_ferret.size |
|---|
| 477 |
assert_equal contents(:first).id, contents_from_ferret.first.id |
|---|
| 478 |
|
|---|
| 479 |
# wildcard query |
|---|
| 480 |
contents_from_ferret = Content.find_with_ferret('use*') |
|---|
| 481 |
assert_equal 1, contents_from_ferret.size |
|---|
| 482 |
|
|---|
| 483 |
# ferret-bug ? wildcard queries don't seem to get lowercased even when |
|---|
| 484 |
# using StandardAnalyzer: |
|---|
| 485 |
# contents_from_ferret = Content.find_with_ferret('Ti*') |
|---|
| 486 |
# we should find both 'Title' and 'title' |
|---|
| 487 |
# assert_equal 2, contents_from_ferret.size |
|---|
| 488 |
# theory: :wild_lower parser option isn't used |
|---|
| 489 |
|
|---|
| 490 |
contents_from_ferret = Content.find_with_ferret('ti*') |
|---|
| 491 |
# this time we find both 'Title' and 'title' |
|---|
| 492 |
assert_equal 2, contents_from_ferret.size |
|---|
| 493 |
|
|---|
| 494 |
contents(:first).destroy |
|---|
| 495 |
contents_from_ferret = Content.find_with_ferret('ti*') |
|---|
| 496 |
# should find only one now |
|---|
| 497 |
assert_equal 1, contents_from_ferret.size |
|---|
| 498 |
assert_equal @another_content.id, contents_from_ferret.first.id |
|---|
| 499 |
end |
|---|
| 500 |
|
|---|
| 501 |
def test_find_with_ferret_options |
|---|
| 502 |
# find options |
|---|
| 503 |
contents_from_ferret = Content.find_with_ferret('title', {}, :conditions => ["id=?",contents(:first).id]) |
|---|
| 504 |
assert_equal 1, contents_from_ferret.size |
|---|
| 505 |
assert_equal contents(:first), contents_from_ferret.first |
|---|
| 506 |
|
|---|
| 507 |
# limit result set size to 1 |
|---|
| 508 |
contents_from_ferret = Content.find_with_ferret('title', :limit => 1) |
|---|
| 509 |
assert_equal 1, contents_from_ferret.size |
|---|
| 510 |
assert_equal contents(:first), contents_from_ferret.first |
|---|
| 511 |
|
|---|
| 512 |
# limit result set size to 1, starting with the second result |
|---|
| 513 |
contents_from_ferret = Content.find_with_ferret('title', :limit => 1, :offset => 1) |
|---|
| 514 |
assert_equal 1, contents_from_ferret.size |
|---|
| 515 |
assert_equal @another_content.id, contents_from_ferret.first.id |
|---|
| 516 |
|
|---|
| 517 |
end |
|---|
| 518 |
|
|---|
| 519 |
def test_pagination |
|---|
| 520 |
more_contents |
|---|
| 521 |
|
|---|
| 522 |
r = Content.find_with_ferret 'title', :per_page => 10, :sort => 'title' |
|---|
| 523 |
assert_equal 30, r.total_hits |
|---|
| 524 |
assert_equal 10, r.size |
|---|
| 525 |
assert_equal "0", r.first.description |
|---|
| 526 |
assert_equal "9", r.last.description |
|---|
| 527 |
assert_equal 1, r.current_page |
|---|
| 528 |
assert_equal 3, r.page_count |
|---|
| 529 |
|
|---|
| 530 |
r = Content.find_with_ferret 'title', :page => '2', :per_page => 10, :sort => 'title' |
|---|
| 531 |
assert_equal 30, r.total_hits |
|---|
| 532 |
assert_equal 10, r.size |
|---|
| 533 |
assert_equal "10", r.first.description |
|---|
| 534 |
assert_equal "19", r.last.description |
|---|
| 535 |
assert_equal 2, r.current_page |
|---|
| 536 |
assert_equal 3, r.page_count |
|---|
| 537 |
|
|---|
| 538 |
r = Content.find_with_ferret 'title', :page => 4, :per_page => 10, :sort => 'title' |
|---|
| 539 |
assert_equal 30, r.total_hits |
|---|
| 540 |
assert_equal 0, r.size |
|---|
| 541 |
end |
|---|
| 542 |
|
|---|
| 543 |
def test_limits_and_offsets |
|---|
| 544 |
more_contents |
|---|
| 545 |
r = Content.find_with_ferret 'title' |
|---|
| 546 |
assert_equal 30, r.total_hits |
|---|
| 547 |
assert_equal 10, r.size |
|---|
| 548 |
|
|---|
| 549 |
r = Content.find_with_ferret 'title', :limit => :all |
|---|
| 550 |
assert_equal 30, r.total_hits |
|---|
| 551 |
assert_equal 30, r.size |
|---|
| 552 |
end |
|---|
| 553 |
|
|---|
| 554 |
def test_limits_and_offsets_with_ar_conditions |
|---|
| 555 |
more_contents |
|---|
| 556 |
|
|---|
| 557 |
r = Content.find_with_ferret 'title', { :limit => 10, :offset => 0 }, |
|---|
| 558 |
{ :conditions => "description != '0'", :order => 'title ASC' } |
|---|
| 559 |
assert_equal 29, r.total_hits |
|---|
| 560 |
assert_equal 10, r.size |
|---|
| 561 |
assert_equal "1", r.first.description |
|---|
| 562 |
assert_equal "10", r.last.description |
|---|
| 563 |
|
|---|
| 564 |
r = Content.find_with_ferret 'title', { :limit => 10, :offset => 10 }, |
|---|
| 565 |
{ :conditions => "description != '0'", :order => 'title ASC' } |
|---|
| 566 |
assert_equal 29, r.total_hits |
|---|
| 567 |
assert_equal 10, r.size |
|---|
| 568 |
assert_equal "11", r.first.description |
|---|
| 569 |
assert_equal "20", r.last.description |
|---|
| 570 |
|
|---|
| 571 |
r = Content.find_with_ferret 'title', { }, |
|---|
| 572 |
{ :conditions => "description != '0'", :order => 'title ASC', |
|---|
| 573 |
:limit => 10, :offset => 0 } |
|---|
| 574 |
assert_equal 29, r.total_hits |
|---|
| 575 |
assert_equal 10, r.size |
|---|
| 576 |
assert_equal "1", r.first.description |
|---|
| 577 |
assert_equal "10", r.last.description |
|---|
| 578 |
|
|---|
| 579 |
r = Content.find_with_ferret 'title', { }, |
|---|
| 580 |
{ :conditions => "description != '0'", :order => 'title ASC', |
|---|
| 581 |
:limit => 10, :offset => 10 } |
|---|
| 582 |
assert_equal 29, r.total_hits |
|---|
| 583 |
assert_equal 10, r.size |
|---|
| 584 |
assert_equal "11", r.first.description |
|---|
| 585 |
assert_equal "20", r.last.description |
|---|
| 586 |
end |
|---|
| 587 |
|
|---|
| 588 |
def test_pagination_with_ar_conditions |
|---|
| 589 |
more_contents |
|---|
| 590 |
|
|---|
| 591 |
r = Content.find_with_ferret 'title', { :page => 1, :per_page => 10 }, |
|---|
| 592 |
{ :conditions => "description != '0'", :order => 'title ASC' } |
|---|
| 593 |
assert_equal 29, r.total_hits |
|---|
| 594 |
assert_equal 10, r.size |
|---|
| 595 |
assert_equal "1", r.first.description |
|---|
| 596 |
assert_equal "10", r.last.description |
|---|
| 597 |
assert_equal 1, r.current_page |
|---|
| 598 |
assert_equal 3, r.page_count |
|---|
| 599 |
|
|---|
| 600 |
r = Content.find_with_ferret 'title', { :page => 3, :per_page => 10 }, |
|---|
| 601 |
{ :conditions => "description != '0'", :order => 'title ASC' } |
|---|
| 602 |
assert_equal 9, r.size |
|---|
| 603 |
assert_equal 29, r.total_hits |
|---|
| 604 |
assert_equal "21", r.first.description |
|---|
| 605 |
assert_equal "29", r.last.description |
|---|
| 606 |
assert_equal 3, r.current_page |
|---|
| 607 |
assert_equal 3, r.page_count |
|---|
| 608 |
end |
|---|
| 609 |
|
|---|
| 610 |
def test_pagination_with_more_conditions |
|---|
| 611 |
more_contents |
|---|
| 612 |
|
|---|
| 613 |
r = Content.find_with_ferret 'title -description:0', { :page => 1, :per_page => 10 }, |
|---|
| 614 |
{ :conditions => "contents.description != '9'", :order => 'title ASC' } |
|---|
| 615 |
assert_equal 28, r.total_hits |
|---|
| 616 |
assert_equal 10, r.size |
|---|
| 617 |
assert_equal "1", r.first.description |
|---|
| 618 |
assert_equal "11", r.last.description |
|---|
| 619 |
assert_equal 1, r.current_page |
|---|
| 620 |
assert_equal 3, r.page_count |
|---|
| 621 |
end |
|---|
| 622 |
|
|---|
| 623 |
def test_reconnect_in_drb_mode |
|---|
| 624 |
if ENV['AAF_REMOTE'] && Content.connection.is_a?(ActiveRecord::ConnectionAdapters::MysqlAdapter) |
|---|
| 625 |
puts "have DRb and MySQL - doing db reconnect test" |
|---|
| 626 |
Content.aaf_index.send(:db_disconnect!) |
|---|
| 627 |
c = Content.create! :title => 'another one', :description => 'description' |
|---|
| 628 |
assert_equal c, Content.find_with_ferret('another').first |
|---|
| 629 |
else |
|---|
| 630 |
assert true |
|---|
| 631 |
end |
|---|
| 632 |
end |
|---|
| 633 |
|
|---|
| 634 |
def test_per_field_boost |
|---|
| 635 |
Content.destroy_all |
|---|
| 636 |
Content.create! :title => 'the title' |
|---|
| 637 |
boosted = Content.new :title => 'the title' |
|---|
| 638 |
boosted.title_boost = 100 |
|---|
| 639 |
boosted.save! |
|---|
| 640 |
Content.create! :title => 'the title' |
|---|
| 641 |
results = Content.find_with_ferret 'title:title' |
|---|
| 642 |
assert_equal 3, results.size |
|---|
| 643 |
assert_equal boosted.id, results.first.id |
|---|
| 644 |
end |
|---|
| 645 |
|
|---|
| 646 |
def test_per_document_boost |
|---|
| 647 |
Content.destroy_all |
|---|
| 648 |
Content.create! :title => 'the title' |
|---|
| 649 |
boosted = Content.new :title => 'the title' |
|---|
| 650 |
boosted.record_boost = 10 |
|---|
| 651 |
boosted.save! |
|---|
| 652 |
Content.create! :title => 'the title' |
|---|
| 653 |
results = Content.find_with_ferret 'title' |
|---|
| 654 |
assert_equal 3, results.size |
|---|
| 655 |
assert_equal boosted.id, results.first.id |
|---|
| 656 |
end |
|---|
| 657 |
|
|---|
| 658 |
|
|---|
| 659 |
protected |
|---|
| 660 |
|
|---|
| 661 |
def more_contents(with_comments = false) |
|---|
| 662 |
Comment.destroy_all if with_comments |
|---|
| 663 |
Content.destroy_all |
|---|
| 664 |
SpecialContent.destroy_all |
|---|
| 665 |
30.times do |i| |
|---|
| 666 |
c = Content.create! :title => sprintf("title of Content %02d", i), :description => "#{i}" |
|---|
| 667 |
c.comments.create! :content => sprintf("Comment for content %02d", i) if with_comments |
|---|
| 668 |
end |
|---|
| 669 |
end |
|---|
| 670 |
|
|---|
| 671 |
def remove_index(clazz) |
|---|
| 672 |
clazz.aaf_index.close # avoid io error when deleting the open index |
|---|
| 673 |
FileUtils.rm_rf clazz.aaf_configuration[:index_dir] |
|---|
| 674 |
assert !File.exists?("#{clazz.aaf_configuration[:index_dir]}/segments") |
|---|
| 675 |
end |
|---|
| 676 |
|
|---|
| 677 |
end |
|---|