To edit pages or tickets please login with username/password: aaf/aaf

Changeset 16

Show
Ignore:
Timestamp:
03/15/06 00:35:13 (3 years ago)
Author:
jk
Message:

Tests and documentation for multi_search capabilities

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/demo/app/models/comment.rb

    r14 r16  
    33class Comment < ActiveRecord::Base 
    44  belongs_to :parent, :class_name => 'Content' 
    5   # just index all fields: 
    6   acts_as_ferret  
     5   
     6  # simplest case: just index all fields of this model: 
     7  # acts_as_ferret 
     8   
     9  # we use :store_class_name => true so that we can use  
     10  # the multi_search method to run queries across multiple 
     11  # models 
     12  acts_as_ferret :store_class_name => true 
     13 
     14  # only index the named fields: 
    715  #acts_as_ferret :fields => ['author', 'content' ] 
    816 
  • trunk/demo/app/models/content.rb

    r5 r16  
    11class Content < ActiveRecord::Base 
    22  has_many :comments 
    3   # higher boost means more importance for the field --> documents having a 
     3   
     4  # a higher boost means more importance for the field --> documents having a 
    45  # match in a field with a higher boost value will be ranked better 
    5   acts_as_ferret :fields => { 'title' => { :boost => 2 }, 'description' => { :boost => 1 } } 
     6  acts_as_ferret :fields => { 'title' => { :boost => 2 }, 'description' => { :boost => 1 } }, :store_class_name => true 
     7 
    68  # use this instead to not assign special boost values: 
    79  #acts_as_ferret :fields => [ 'title', 'description' ] 
  • trunk/demo/test/unit/comment_test.rb

    r14 r16  
    8383    assert_equal 1, comments_from_ferret.size 
    8484    assert_equal comment.id, comments_from_ferret.first.id 
     85 
     86    comment.destroy 
     87    comment2.destroy 
    8588   end 
    8689 
  • trunk/demo/test/unit/content_test.rb

    r10 r16  
    22 
    33class ContentTest < Test::Unit::TestCase 
    4   fixtures :contents 
     4  include Ferret::Index 
     5  include Ferret::Search 
     6  fixtures :contents, :comments 
    57 
     8  def setup 
     9    @content = Content.new( :title => 'My Title', :description => 'A useless description' ) 
     10    @content.save 
     11    @another_content = Content.new( :title => 'Another Content item',  
     12                                    :description => 'this is not the title' ) 
     13    @another_content.save 
     14    @comment = Comment.new( :author => 'john doe', :content => 'This is a useless comment' ) 
     15    @comment.save 
     16    @comment2 = Comment.new( :author => 'another', :content => 'content' ) 
     17    @comment2.save 
     18  end 
     19   
     20  def teardown 
     21    @content.destroy if @content 
     22    @another_content.destroy if @another_content 
     23    @comment.destroy if @comment 
     24    @comment2.destroy if @comment2 
     25  end 
     26   
    627  def test_truth 
    728    assert_kind_of Content, contents(:first) 
     
    1233  end 
    1334 
     35  def test_multi_index 
     36    i =  FerretMixin::Acts::ARFerret::MultiIndex.new([Content, Comment]) 
     37    hits = i.search(TermQuery.new(Term.new("title","title"))) 
     38    assert_equal 1, hits.score_docs.size 
     39 
     40    qp = Ferret::QueryParser.new("title",  
     41                      :analyzer => Ferret::Analysis::WhiteSpaceAnalyzer.new) 
     42    hits = i.search(qp.parse("title")) 
     43    assert_equal 1, hits.score_docs.size 
     44     
     45    qp = Ferret::QueryParser.new("*",  
     46                      :analyzer => Ferret::Analysis::WhiteSpaceAnalyzer.new) 
     47    qp.fields = i.reader.get_field_names.to_a 
     48    hits = i.search(qp.parse("title")) 
     49    assert_equal 2, hits.score_docs.size 
     50 
     51    hits = i.search("title") 
     52    assert_equal 2, hits.score_docs.size 
     53     
     54    hits = i.search("title OR comment") 
     55    assert_equal 5, hits.score_docs.size 
     56  end 
     57 
     58  def test_multi_reader 
     59    r = MultiReader.new([IndexReader.open(Content.class_index_dir), IndexReader.open(Comment.class_index_dir)]) 
     60    s = IndexSearcher.new(r) 
     61    hits = s.search(TermQuery.new(Term.new("title","title"))) 
     62    assert_equal 1, hits.score_docs.size 
     63  end 
     64     
     65  def test_multi_search 
     66    assert_equal 4, Content.find(:all).size 
     67    contents_from_ferret = Content.multi_search('*:title') 
     68    assert_equal 2, contents_from_ferret.size 
     69    assert_equal @content.id, contents_from_ferret.first.id 
     70    assert_equal @another_content.id, contents_from_ferret.last.id 
     71     
     72    contents_from_ferret = Content.multi_search('title OR comment', [Comment]) 
     73    assert_equal 5, contents_from_ferret.size 
     74  end 
     75 
     76  def test_id_multi_search 
     77    assert_equal 4, Content.find(:all).size 
     78    contents_from_ferret = Content.id_multi_search('*:title') 
     79    assert_equal 2, contents_from_ferret.size 
     80    assert_equal @content.id, contents_from_ferret.first[:id].to_i 
     81    assert_equal @another_content.id, contents_from_ferret.last[:id].to_i 
     82     
     83    contents_from_ferret = Content.id_multi_search('title OR comment', [Comment]) 
     84    assert_equal 5, contents_from_ferret.size 
     85  end 
     86 
    1487  def test_find_by_contents 
    15     @content = Content.new( :title => 'My Title', :description => 'A useless description' ) 
    16     @content.save 
    17     @another_content = Content.new( :title => 'Another Content item',  
    18                                     :description => 'this is not the title' ) 
    19     @another_content.save 
    2088 
    2189    contents_from_ferret = Content.find_by_contents('title') 
     
    80148    # this time we find both 'Title' and 'title' 
    81149    assert_equal 2, contents_from_ferret.size 
    82      
     150 
     151    @content.destroy 
     152    contents_from_ferret = Content.find_by_contents('ti*') 
     153    # should find only one now 
     154    assert_equal 1, contents_from_ferret.size 
     155    assert_equal @another_content.id, contents_from_ferret.first.id 
    83156   end 
    84157 

To edit pages or tickets please login with username/password: aaf/aaf