Changeset 78
- Timestamp:
- 08/23/06 16:45:28 (2 years ago)
- Files:
-
- trunk/demo/app/models/comment.rb (modified) (2 diffs)
- trunk/demo/app/models/content.rb (modified) (1 diff)
- trunk/demo/test/unit/comment_test.rb (modified) (4 diffs)
- trunk/demo/test/unit/content_test.rb (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/demo/app/models/comment.rb
r60 r78 8 8 # the multi_search method to run queries across multiple 9 9 # models (where each model has it's own index directory) 10 acts_as_ferret :store_class_name => true 10 # 11 # use the :additional_fields property to specify fields you intend 12 # to add in addition to those fields from your database table (which will be 13 # autodiscovered by acts_as_ferret) 14 # the :ignore flag tells aaf to not try to set this field's value itself (we 15 # do this in our custom to_doc method) 16 acts_as_ferret :store_class_name => true, :additional_fields => { 17 :added => { :index => :untokenized, :store => :yes, :ignore => true }, 18 } 11 19 12 20 # only index the named fields: … … 20 28 doc = super 21 29 # add a field containing the current time 22 doc << Ferret::Document::Field.new( 23 'added', Time.now.to_i.to_s, 24 Ferret::Document::Field::Store::YES, 25 Ferret::Document::Field::Index::UNTOKENIZED) 30 doc['added'] = Time.now.to_i.to_s 31 # Ferret::Document::Field::Store::YES, 32 # Ferret::Document::Field::Index::UNTOKENIZED) 26 33 return doc 27 34 end trunk/demo/app/models/content.rb
r51 r78 11 11 # 'more like this' queries to find other content instances with similar 12 12 # descriptions 13 acts_as_ferret :fields => { :comment_count => {}, 'title' => { :boost => 2 }, 'description' => { :boost => 1, :store => Ferret::Document::Field::Store::YES }, :special => {} }, :store_class_name => true 13 acts_as_ferret( :fields => { 14 :comment_count => {}, 15 :title => { :boost => 2 }, 16 :description => { :boost => 1, :store => :yes }, 17 :special => {} 18 }, :store_class_name => true) 14 19 15 20 # use this instead to not assign special boost values: trunk/demo/test/unit/comment_test.rb
r76 r78 23 23 # maybe different analyzers at index creation and searching time ? 24 24 #comments_from_ferret = Comment.find_by_contents('"comment from fixture"') 25 comments_from_ferret = Comment.find_by_contents('comment ANDfixture')25 comments_from_ferret = Comment.find_by_contents('comment fixture') 26 26 assert_equal 2, comments_from_ferret.size 27 27 assert comments_from_ferret.include?(comments(:first)) … … 50 50 assert_equal 10, (res = Comment.find_by_contents('multicomment')).size 51 51 assert_equal 20, res.total_hits 52 assert_equal 15, (res = Comment.find_by_contents('multicomment', : num_docs=> 15)).size52 assert_equal 15, (res = Comment.find_by_contents('multicomment', :limit => 15)).size 53 53 assert_equal 20, res.total_hits 54 54 assert_equal 20, (res = Comment.find_by_contents('multicomment', :num_docs => :all)).size … … 64 64 top_docs = Comment.ferret_index.search('"from fixture"') 65 65 #top_docs = Comment.ferret_index.search('"comment from fixture"') 66 assert_equal 2, top_docs. score_docs.size67 doc = Comment.ferret_index.doc(top_docs. score_docs[0].doc)66 assert_equal 2, top_docs.total_hits 67 doc = Comment.ferret_index.doc(top_docs.hits[0].doc) 68 68 # check for the special field added by the custom to_doc method 69 69 assert_not_nil doc[:added] … … 136 136 def test_stopwords_ferret_bug 137 137 i = Ferret::Index::Index.new( 138 :o ccur_default => Ferret::Search::BooleanClause::Occur::MUST,139 :default_ search_field => '*'138 :or_default => false, 139 :default_field => '*' 140 140 ) 141 141 d = Ferret::Document::Document.new 142 d << Ferret::Document::Field.new('id', '1', 143 Ferret::Document::Field::Store::YES, 144 Ferret::Document::Field::Index::UNTOKENIZED, 145 Ferret::Document::Field::TermVector::NO, 146 false, 1.0) 147 d << Ferret::Document::Field.new('content', 'Move or shake', 148 Ferret::Document::Field::Store::NO, 149 Ferret::Document::Field::Index::TOKENIZED, 150 Ferret::Document::Field::TermVector::NO, 151 false, 1.0) 142 d[:id] = '1' 143 d[:content] = 'Move or shake' 152 144 i << d 153 145 hits = i.search 'move nothere shake' trunk/demo/test/unit/content_test.rb
r60 r78 94 94 def test_multi_index 95 95 i = FerretMixin::Acts::ARFerret::MultiIndex.new([Content, Comment]) 96 hits = i.search(TermQuery.new( Term.new("title","title")))97 assert_equal 1, hits. score_docs.size98 99 qp = Ferret::QueryParser.new( "title",100 :analyzer => Ferret::Analysis::WhiteSpaceAnalyzer.new)96 hits = i.search(TermQuery.new(:title,"title")) 97 assert_equal 1, hits.total_hits 98 99 qp = Ferret::QueryParser.new(:default_field => "title", 100 :analyzer => Ferret::Analysis::WhiteSpaceAnalyzer.new) 101 101 hits = i.search(qp.parse("title")) 102 assert_equal 1, hits. score_docs.size103 104 qp = Ferret::QueryParser.new( "*",:fields => ['title', 'content', 'description'],102 assert_equal 1, hits.total_hits 103 104 qp = Ferret::QueryParser.new(:fields => ['title', 'content', 'description'], 105 105 :analyzer => Ferret::Analysis::WhiteSpaceAnalyzer.new) 106 106 # TODO '*' doesn't seem to work in 0.9 anymore - there's no .fields method … … 109 109 #qp.fields = ['title','content'] 110 110 hits = i.search(qp.parse("title")) 111 assert_equal 2, hits. score_docs.size111 assert_equal 2, hits.total_hits 112 112 hits = i.search(qp.parse("title:title OR description:title")) 113 assert_equal 2, hits. score_docs.size113 assert_equal 2, hits.total_hits 114 114 115 115 hits = i.search("title:title OR description:title OR title:comment OR description:comment OR content:comment") 116 assert_equal 5, hits. score_docs.size116 assert_equal 5, hits.total_hits 117 117 118 118 hits = i.search("title OR comment") 119 assert_equal 5, hits. score_docs.size119 assert_equal 5, hits.total_hits 120 120 end 121 121 122 122 def test_multi_searcher 123 s = MultiSearcher.new([ IndexSearcher.new(Content.class_index_dir), IndexSearcher.new(Comment.class_index_dir)])124 hits = s.search(TermQuery.new( Term.new("title","title")))125 assert_equal 1, hits. score_docs.size123 s = MultiSearcher.new([Searcher.new(Content.class_index_dir), Searcher.new(Comment.class_index_dir)]) 124 hits = s.search(TermQuery.new(:title,"title")) 125 assert_equal 1, hits.total_hits 126 126 end 127 127 … … 189 189 190 190 def test_find_by_contents_boost 191 192 191 # give description field higher boost: 193 192 contents_from_ferret = Content.find_by_contents('title:title OR description:title^10') … … 216 215 contents_from_ferret = Content.find_by_contents('title', :num_docs => 1) 217 216 assert_equal 1, contents_from_ferret.size 218 assert_equal contents(:first) .id, contents_from_ferret.first.id217 assert_equal contents(:first), contents_from_ferret.first 219 218 220 219 # limit result set size to 1, starting with the second result
