Changeset 16
- Timestamp:
- 03/15/06 00:35:13 (3 years ago)
- Files:
-
- trunk/demo/app/models/comment.rb (modified) (1 diff)
- trunk/demo/app/models/content.rb (modified) (1 diff)
- trunk/demo/test/unit/comment_test.rb (modified) (1 diff)
- trunk/demo/test/unit/content_test.rb (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/demo/app/models/comment.rb
r14 r16 3 3 class Comment < ActiveRecord::Base 4 4 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: 7 15 #acts_as_ferret :fields => ['author', 'content' ] 8 16 trunk/demo/app/models/content.rb
r5 r16 1 1 class Content < ActiveRecord::Base 2 2 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 4 5 # 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 6 8 # use this instead to not assign special boost values: 7 9 #acts_as_ferret :fields => [ 'title', 'description' ] trunk/demo/test/unit/comment_test.rb
r14 r16 83 83 assert_equal 1, comments_from_ferret.size 84 84 assert_equal comment.id, comments_from_ferret.first.id 85 86 comment.destroy 87 comment2.destroy 85 88 end 86 89 trunk/demo/test/unit/content_test.rb
r10 r16 2 2 3 3 class ContentTest < Test::Unit::TestCase 4 fixtures :contents 4 include Ferret::Index 5 include Ferret::Search 6 fixtures :contents, :comments 5 7 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 6 27 def test_truth 7 28 assert_kind_of Content, contents(:first) … … 12 33 end 13 34 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 14 87 def test_find_by_contents 15 @content = Content.new( :title => 'My Title', :description => 'A useless description' )16 @content.save17 @another_content = Content.new( :title => 'Another Content item',18 :description => 'this is not the title' )19 @another_content.save20 88 21 89 contents_from_ferret = Content.find_by_contents('title') … … 80 148 # this time we find both 'Title' and 'title' 81 149 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 83 156 end 84 157
