From 21f412d31a35f0a531a6de5d5fdbd14a3dde9730 Mon Sep 17 00:00:00 2001 From: Braulio Bhavamitra Date: Wed, 20 Apr 2011 22:58:13 +0400 Subject: [PATCH] Adapt search controllers to Solr --- app/controllers/public/profile_search_controller.rb | 3 +-- app/controllers/public/search_controller.rb | 5 ++--- app/helpers/search_helper.rb | 9 --------- test/factories.rb | 2 +- test/functional/profile_search_controller_test.rb | 13 +++---------- test/functional/search_controller_test.rb | 16 +--------------- vendor/plugins/acts_as_solr/lib/parser_methods.rb | 9 ++++----- 7 files changed, 12 insertions(+), 45 deletions(-) diff --git a/app/controllers/public/profile_search_controller.rb b/app/controllers/public/profile_search_controller.rb index 4bf9075..e678ab6 100644 --- a/app/controllers/public/profile_search_controller.rb +++ b/app/controllers/public/profile_search_controller.rb @@ -8,11 +8,10 @@ class ProfileSearchController < PublicController def index @q = params[:q] unless @q.blank? - @filtered_query = remove_stop_words(@q) if params[:where] == 'environment' redirect_to :controller => 'search', :query => @q else - @results = profile.articles.published.find_by_contents(@filtered_query).paginate(:per_page => 10, :page => params[:page]) + @results = profile.articles.published.find_by_contents(@q)[:results].paginate(:per_page => 10, :page => params[:page]) end end end diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index 094ae07..9a98db4 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -89,7 +89,7 @@ class SearchController < PublicController # REFACTOR DUPLICATED CODE inner loop doing the same thing that outter loop if !@query.blank? || @region && !params[:radius].blank? - @result_ids = @noosfero_finder.find(asset, @filtered_query, calculate_find_options(asset, nil, params[:page], @product_category, @region, params[:radius], params[:year], params[:month]).merge({:limit => :all})) + @result_ids = @noosfero_finder.find(asset, @query, calculate_find_options(asset, nil, params[:page], @product_category, @region, params[:radius], params[:year], params[:month]).merge({:limit => :all})) end end @@ -149,7 +149,6 @@ class SearchController < PublicController def index @query = params[:query] || '' - @filtered_query = remove_stop_words(@query) @product_category = ProductCategory.find(params[:product_category]) if params[:product_category] @region = City.find_by_id(params[:city]) if !params[:city].blank? && params[:city] =~ /^\d+$/ @@ -163,7 +162,7 @@ class SearchController < PublicController where_to_search.select { |key,description| @searching[key] }.each do |key, description| @order << key - @results[key] = @noosfero_finder.find(key, @filtered_query, calculate_find_options(key, limit, params[:page], @product_category, @region, params[:radius], params[:year], params[:month])) + @results[key] = @noosfero_finder.find(key, @query, calculate_find_options(key, limit, params[:page], @product_category, @region, params[:radius], params[:year], params[:month])) @names[key] = getterm(description) end diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb index c1f6fde..3ba9ae1 100644 --- a/app/helpers/search_helper.rb +++ b/app/helpers/search_helper.rb @@ -3,21 +3,12 @@ module SearchHelper # FIXME remove it after search_controler refactored include EventsHelper - STOP_WORDS = { - 'pt_BR' => Ferret::Analysis::FULL_PORTUGUESE_STOP_WORDS, - 'en' => Ferret::Analysis::FULL_ENGLISH_STOP_WORDS, - } - def relevance_for(hit) n = (hit.ferret_score if hit.respond_to?(:ferret_score)) n ||= 1.0 (n * 100.0).round end - def remove_stop_words(query) - (query.downcase.scan(/"[^"]*"?|'[^']*'?|[^'"\s]+/) - (STOP_WORDS[locale] || [])).join(' ') - end - def display_results(use_map = true) unless use_map && GoogleMaps.enabled?(environment.default_hostname) diff --git a/test/factories.rb b/test/factories.rb index d1b10c2..389840a 100644 --- a/test/factories.rb +++ b/test/factories.rb @@ -22,7 +22,7 @@ module Noosfero::Factory end end if options[:search] - obj.ferret_create + obj.solr_save end obj end diff --git a/test/functional/profile_search_controller_test.rb b/test/functional/profile_search_controller_test.rb index 0feb6f9..dd10b83 100644 --- a/test/functional/profile_search_controller_test.rb +++ b/test/functional/profile_search_controller_test.rb @@ -6,6 +6,7 @@ class ProfileSearchController; def rescue_action(e) raise e end; end class ProfileSearchControllerTest < Test::Unit::TestCase def setup + Test::Unit::TestCase::setup @controller = ProfileSearchController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new @@ -14,14 +15,6 @@ class ProfileSearchControllerTest < Test::Unit::TestCase end attr_reader :person - should 'filter stop words' do - @controller.expects(:locale).returns('en').at_least_once - get 'index', :profile => person.identifier, :q => 'an article about something' - assert_response :success - assert_template 'index' - assert_equal 'article something', assigns('filtered_query') - end - should 'espape xss attack' do @controller.expects(:profile).returns(person).at_least_once get 'index', :profile => person.identifier, :q => '' @@ -41,8 +34,8 @@ class ProfileSearchControllerTest < Test::Unit::TestCase end should 'display search results' do - article1 = fast_create(Article, :body => '

Article to test profile search

', :profile_id => person.id) - article2 = fast_create(Article, :body => '

Another article to test profile search

', :profile_id => person.id) + article1 = fast_create(Article, {:body => '

Article to test profile search

', :profile_id => person.id}, :search => true) + article2 = fast_create(Article, {:body => '

Another article to test profile search

', :profile_id => person.id}, :search => true) get 'index', :profile => person.identifier, :q => 'article' diff --git a/test/functional/search_controller_test.rb b/test/functional/search_controller_test.rb index a9cc95c..9b46e1c 100644 --- a/test/functional/search_controller_test.rb +++ b/test/functional/search_controller_test.rb @@ -6,6 +6,7 @@ class SearchController; def rescue_action(e) raise e end; end class SearchControllerTest < Test::Unit::TestCase def setup + Test::Unit::TestCase::setup @controller = SearchController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new @@ -35,21 +36,6 @@ class SearchControllerTest < Test::Unit::TestCase assert_valid_xhtml end - should 'filter stop words' do - @controller.expects(:locale).returns('pt_BR').at_least_once - get 'index', :query => 'a carne da vaca' - assert_response :success - assert_template 'index' - assert_equal 'carne vaca', assigns('filtered_query') - end - - should 'search with filtered query' do - @controller.expects(:locale).returns('pt_BR').at_least_once - get 'index', :query => 'a carne da vaca' - - assert_equal 'carne vaca', assigns('filtered_query') - end - should 'espape xss attack' do get 'index', :query => '' assert_no_tag :tag => 'wslite' diff --git a/vendor/plugins/acts_as_solr/lib/parser_methods.rb b/vendor/plugins/acts_as_solr/lib/parser_methods.rb index bb46e9b..579a928 100644 --- a/vendor/plugins/acts_as_solr/lib/parser_methods.rb +++ b/vendor/plugins/acts_as_solr/lib/parser_methods.rb @@ -135,11 +135,10 @@ module ActsAsSolr #:nodoc: # Reorders the instances keeping the order returned from Solr def reorder(things, ids) - ordered_things = Array.new(things.size) - raise "Out of sync! Found #{ids.size} items in index, but only #{things.size} were found in database!" unless things.size == ids.size - things.each do |thing| - position = ids.index(thing.id) - ordered_things[position] = thing + ordered_things = [] + ids.each do |id| + thing = things.find { |t| t.id.to_s == id.to_s } + ordered_things |= [thing] if thing end ordered_things end -- libgit2 0.21.2