| 358 | | def test_total_hits_multi |
|---|
| 359 | | q = '*:title OR *:comment' |
|---|
| 360 | | assert_equal 3, Comment.total_hits(q) |
|---|
| 361 | | assert_equal 2, Content.total_hits(q) |
|---|
| 362 | | assert_equal 5, ActsAsFerret::total_hits(q, [ Comment, Content ]) |
|---|
| 363 | | end |
|---|
| 364 | | |
|---|
| 365 | | def test_multi_search_sorting |
|---|
| 366 | | sorting = [ Ferret::Search::SortField.new(:id) ] |
|---|
| 367 | | result = Content.find_with_ferret('*:title OR *:comment', :multi => [Comment], :sort => sorting) |
|---|
| 368 | | assert_equal result.map(&:id).sort, result.map(&:id) |
|---|
| 369 | | |
|---|
| 370 | | sorting = [ Ferret::Search::SortField.new(:title) ] |
|---|
| 371 | | result = Content.find_with_ferret('*:title OR *:comment', :multi => [Comment], :sort => sorting) |
|---|
| 372 | | sorting = [ Ferret::Search::SortField.new(:title, :reverse => true) ] |
|---|
| 373 | | result2 = Content.find_with_ferret('*:title OR *:comment', :multi => [Comment], :sort => sorting) |
|---|
| 374 | | assert result.any? |
|---|
| 375 | | assert result.map(&:id) != result2.map(&:id) |
|---|
| 376 | | |
|---|
| 377 | | sorting = [ Ferret::Search::SortField::SCORE ] |
|---|
| 378 | | result = Content.find_with_ferret('*:title OR *:comment', :multi => Comment, :sort => sorting) |
|---|
| 379 | | assert result.any? |
|---|
| 380 | | assert_equal result.map(&:ferret_score).sort.reverse, result.map(&:ferret_score) |
|---|
| 381 | | |
|---|
| 382 | | sorting = [ Ferret::Search::SortField::SCORE_REV ] |
|---|
| 383 | | result2 = Content.find_with_ferret('*:title OR *:comment', :multi => Comment, :sort => sorting) |
|---|
| 384 | | assert_equal result2.map(&:ferret_score).sort, result2.map(&:ferret_score) |
|---|
| 385 | | assert_equal result.map(&:ferret_score), result2.map(&:ferret_score).reverse |
|---|
| 386 | | end |
|---|
| 387 | | |
|---|
| 416 | | # remote index rebuilds will create an index in a directory with a timestamped name. |
|---|
| 417 | | # the local MultiIndex instance doesn't know about this (because it's running in |
|---|
| 418 | | # another interpreter instance than the server) and therefore fails to use the |
|---|
| 419 | | # correct index directories. |
|---|
| 420 | | # TODO strange, still doesn't work but it should now... |
|---|
| 421 | | unless Content.aaf_configuration[:remote] |
|---|
| 422 | | def test_multi_index |
|---|
| 423 | | i = ActsAsFerret::MultiIndex.new([Content, Comment]) |
|---|
| 424 | | hits = i.search(TermQuery.new(:title,"title")) |
|---|
| 425 | | assert_equal 1, hits.total_hits |
|---|
| 426 | | |
|---|
| 427 | | qp = Ferret::QueryParser.new(:default_field => "title", |
|---|
| 428 | | :analyzer => Ferret::Analysis::WhiteSpaceAnalyzer.new) |
|---|
| 429 | | hits = i.search(qp.parse("title")) |
|---|
| 430 | | assert_equal 1, hits.total_hits |
|---|
| 431 | | |
|---|
| 432 | | qp = Ferret::QueryParser.new(:fields => ['title', 'content', 'description'], |
|---|
| 433 | | :analyzer => Ferret::Analysis::WhiteSpaceAnalyzer.new) |
|---|
| 434 | | hits = i.search(qp.parse("title")) |
|---|
| 435 | | assert_equal 2, hits.total_hits |
|---|
| 436 | | hits = i.search(qp.parse("title:title OR description:title")) |
|---|
| 437 | | assert_equal 2, hits.total_hits |
|---|
| 438 | | |
|---|
| 439 | | hits = i.search("title:title OR description:title OR title:comment OR description:comment OR content:comment") |
|---|
| 440 | | assert_equal 5, hits.total_hits |
|---|
| 441 | | |
|---|
| 442 | | hits = i.search("title OR comment") |
|---|
| 443 | | assert_equal 5, hits.total_hits |
|---|
| 444 | | |
|---|
| 445 | | hits = i.search("title OR comment", :limit => 2) |
|---|
| 446 | | count = 0 |
|---|
| 447 | | hits.hits.each { |hit, score| count += 1 } |
|---|
| 448 | | assert_equal 2, count |
|---|
| 449 | | |
|---|
| 450 | | hits = i.search("title OR comment", :offset => 2) |
|---|
| 451 | | count = 0 |
|---|
| 452 | | hits.hits.each { |hit, score| count += 1 } |
|---|
| 453 | | assert_equal 3, count |
|---|
| 454 | | end |
|---|
| 455 | | end |
|---|
| 469 | | def test_multi_search_rebuilds_index |
|---|
| 470 | | remove_index Content |
|---|
| 471 | | contents_from_ferret = Content.find_with_ferret('description:title', :multi => Comment) |
|---|
| 472 | | assert_equal 1, contents_from_ferret.size |
|---|
| 473 | | end |
|---|
| 474 | | |
|---|
| 475 | | # remote index rebuilds will create an index in a directory with a timestamped name... |
|---|
| 476 | | unless Content.aaf_configuration[:remote] |
|---|
| 477 | | def test_multi_index_rebuilds_index |
|---|
| 478 | | remove_index Content |
|---|
| 479 | | i = ActsAsFerret::MultiIndex.new([Content]) |
|---|
| 480 | | assert File.exists?("#{ActsAsFerret::index_definition(Content)[:index_dir]}/segments") |
|---|
| 481 | | hits = i.search("description:title") |
|---|
| 482 | | assert_equal 1, hits.total_hits, hits.inspect |
|---|
| 483 | | end |
|---|
| 484 | | end |
|---|
| 485 | | |
|---|
| 486 | | def test_multi_search_find_options |
|---|
| 487 | | contents_from_ferret = Content.find_with_ferret('title', { :multi => Comment }, :order => 'id desc') |
|---|
| 488 | | assert_equal 2, contents_from_ferret.size |
|---|
| 489 | | assert contents_from_ferret.first.id > contents_from_ferret.last.id |
|---|
| 490 | | contents_from_ferret = Content.find_with_ferret('title', { :multi => Comment }, :order => 'id asc') |
|---|
| 491 | | assert contents_from_ferret.first.id < contents_from_ferret.last.id |
|---|
| 492 | | |
|---|
| 493 | | contents_from_ferret = Content.find_with_ferret('title', :multi => Comment, :limit => 1) |
|---|
| 494 | | assert_equal 1, contents_from_ferret.size |
|---|
| 495 | | contents_from_ferret = Content.find_with_ferret('title', { :multi => Comment }, :limit => 1) |
|---|
| 496 | | assert_equal 1, contents_from_ferret.size |
|---|
| 497 | | |
|---|
| 498 | | |
|---|
| 499 | | more_contents(true) |
|---|
| 500 | | r = Content.find_with_ferret('title OR comment', { :multi => Comment, :limit => :all } ) |
|---|
| 501 | | assert_equal 60, r.size |
|---|
| 502 | | assert_equal 60, r.total_hits |
|---|
| 503 | | |
|---|
| 504 | | id = Content.find_with_ferret('title').first.id |
|---|
| 505 | | r = Content.find_with_ferret('title OR comment', { :multi => Comment, :limit => :all }, |
|---|
| 506 | | { :conditions => { :content => ["id != ?", id] }}) |
|---|
| 507 | | assert_equal 59, r.size |
|---|
| 508 | | assert_equal 59, r.total_hits |
|---|
| 509 | | |
|---|
| 510 | | r = Content.find_with_ferret('title OR comment', { :multi => Comment, :limit => 20 }, |
|---|
| 511 | | { :conditions => { :content => ["id != ?", id] }}) |
|---|
| 512 | | assert_equal 20, r.size |
|---|
| 513 | | assert_equal 59, r.total_hits |
|---|
| 514 | | |
|---|
| 515 | | r = Content.find_with_ferret('title OR comment', { :multi => Comment, :limit => 20 }, |
|---|
| 516 | | { :conditions => { :comment => 'content is null', |
|---|
| 517 | | :content => ["id != ?", id] }}) |
|---|
| 518 | | assert_equal 20, r.size |
|---|
| 519 | | assert_equal 29, r.total_hits |
|---|
| 520 | | |
|---|
| 521 | | r = Content.find_with_ferret('title OR comment', { :multi => Comment }, |
|---|
| 522 | | { :conditions => { :content => ["id != ?", id] }, :limit => 20 }) |
|---|
| 523 | | assert_equal 20, r.size |
|---|
| 524 | | assert_equal 59, r.total_hits |
|---|
| 525 | | end |
|---|
| 526 | | |
|---|
| 527 | | def test_multi_search |
|---|
| 528 | | assert_equal 4, ContentBase.find(:all).size |
|---|
| 529 | | |
|---|
| 530 | | Content.aaf_index.ferret_index.flush |
|---|
| 531 | | contents_from_ferret = Content.find_with_ferret('description:title', :multi => []) |
|---|
| 532 | | assert_equal 1, contents_from_ferret.size |
|---|
| 533 | | contents_from_ferret = Content.find_with_ferret('title:title OR description:title', :multi => []) |
|---|
| 534 | | assert_equal 2, contents_from_ferret.size |
|---|
| 535 | | contents_from_ferret = Content.find_with_ferret('title:title', :multi => []) |
|---|
| 536 | | assert_equal 1, contents_from_ferret.size |
|---|
| 537 | | contents_from_ferret = Content.find_with_ferret('*:title', :multi => []) |
|---|
| 538 | | assert_equal 2, contents_from_ferret.size |
|---|
| 539 | | contents_from_ferret = Content.find_with_ferret('title', :multi => []) |
|---|
| 540 | | assert_equal 2, contents_from_ferret.size |
|---|
| 541 | | |
|---|
| 542 | | assert_equal contents(:first).id, contents_from_ferret.first.id |
|---|
| 543 | | assert_equal @another_content.id, contents_from_ferret.last.id |
|---|
| 544 | | |
|---|
| 545 | | contents_from_ferret = Content.find_with_ferret('title', :multi => []) |
|---|
| 546 | | assert_equal 2, contents_from_ferret.size |
|---|
| 547 | | contents_from_ferret = Content.find_with_ferret('title', :multi => [], :limit => 1) |
|---|
| 548 | | assert_equal 1, contents_from_ferret.size |
|---|
| 549 | | contents_from_ferret = Content.find_with_ferret('title', :multi => [], :offset => 1) |
|---|
| 550 | | assert_equal 1, contents_from_ferret.size |
|---|
| 551 | | |
|---|
| 552 | | contents_from_ferret = Content.find_with_ferret('title:title OR content:comment OR description:title', :multi => [Comment]) |
|---|
| 553 | | assert_equal 5, contents_from_ferret.size |
|---|
| 554 | | contents_from_ferret = Content.find_with_ferret('title:title OR content:comment OR description:title', :multi => [Comment], :limit => 2) |
|---|
| 555 | | assert_equal 2, contents_from_ferret.size |
|---|
| 556 | | |
|---|
| 557 | | contents_from_ferret = Content.find_with_ferret('*:title OR *:comment', :multi => Comment) |
|---|
| 558 | | assert_equal 5, contents_from_ferret.size |
|---|
| 559 | | contents_from_ferret = Content.find_with_ferret('*:title OR *:comment', :multi => [Comment]) |
|---|
| 560 | | assert_equal 5, contents_from_ferret.size |
|---|
| 561 | | contents_from_ferret = Content.find_with_ferret('title:(title OR comment) OR description:(title OR comment) OR content:(title OR comment)', :multi => [Comment]) |
|---|
| 562 | | assert_equal 5, contents_from_ferret.size |
|---|
| 563 | | end |
|---|
| 564 | | |
|---|
| 565 | | def test_multi_search_lazy |
|---|
| 566 | | contents_from_ferret = Content.find_with_ferret('title', :multi => Comment, :lazy => true) |
|---|
| 567 | | assert_equal 2, contents_from_ferret.size |
|---|
| 568 | | contents_from_ferret.each do |record| |
|---|
| 569 | | assert ActsAsFerret::FerretResult === record, record.inspect |
|---|
| 570 | | assert !record.description.blank? |
|---|
| 571 | | assert_nil record.instance_variable_get(:"@ar_record") |
|---|
| 572 | | end |
|---|
| 573 | | end |
|---|
| 574 | | |
|---|
| 575 | | def test_id_multi_search |
|---|
| 576 | | assert_equal 4, ContentBase.find(:all).size |
|---|
| 577 | | |
|---|
| 578 | | [ 'title:title OR description:title OR content:title', 'title', '*:title'].each do |query| |
|---|
| 579 | | total_hits, contents_from_ferret = Content.id_multi_search(query) |
|---|
| 580 | | assert_equal 2, contents_from_ferret.size, query |
|---|
| 581 | | assert_equal 2, total_hits, query |
|---|
| 582 | | assert_equal contents(:first).id, contents_from_ferret.first[:id].to_i |
|---|
| 583 | | assert_equal @another_content.id, contents_from_ferret.last[:id].to_i |
|---|
| 584 | | end |
|---|
| 585 | | |
|---|
| 586 | | ContentBase.rebuild_index |
|---|
| 587 | | Comment.rebuild_index |
|---|
| 588 | | ['title OR comment', 'title:(title OR comment) OR description:(title OR comment) OR content:(title OR comment)'].each do |query| |
|---|
| 589 | | total_hits, contents_from_ferret = Content.id_multi_search(query, [Comment]) |
|---|
| 590 | | assert_equal 5, contents_from_ferret.size, query |
|---|
| 591 | | assert_equal 5, total_hits |
|---|
| 592 | | end |
|---|
| 593 | | end |
|---|
| 594 | | |
|---|
| 595 | | def test_id_multi_search_lazy |
|---|
| 596 | | total_hits, contents_from_ferret = Content.id_multi_search('title', [Comment], :lazy => true) |
|---|
| 597 | | assert_equal 2, contents_from_ferret.size |
|---|
| 598 | | assert_equal 2, total_hits |
|---|
| 599 | | found = 0 |
|---|
| 600 | | contents_from_ferret.each do |data| |
|---|
| 601 | | next if data[:model] != 'Content' |
|---|
| 602 | | found += 1 |
|---|
| 603 | | assert !data[:data][:description].blank? |
|---|
| 604 | | end |
|---|
| 605 | | assert_equal 2, found |
|---|
| 606 | | end |
|---|
| 607 | | |
|---|