From b4d274ac026384670984bf265046549a9d3c39e6 Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Thu, 20 Dec 2012 23:02:31 -0300 Subject: [PATCH] [otw] Deicinding whether to keed index or not... =( --- app/controllers/public/search_controller.rb | 72 ++++-------------------------------------------------------------------- app/helpers/search_helper.rb | 47 +---------------------------------------------- app/models/article.rb | 6 ------ app/models/environment.rb | 2 -- app/models/profile.rb | 6 ------ app/views/search/_facets_unselect_menu.rhtml | 6 ------ app/views/search/_search_form.rhtml | 37 ------------------------------------- app/views/search/category_index.rhtml | 1 - app/views/search/facets_browse.rhtml | 8 -------- app/views/search/index.rhtml | 29 ----------------------------- plugins/solr/lib/ext/article.rb | 6 ++++++ plugins/solr/lib/ext/profile.rb | 9 ++++++++- plugins/solr/lib/solr_plugin.rb | 1 + plugins/solr/lib/solr_plugin/results_helper.rb | 9 +++++++++ plugins/solr/lib/solr_plugin/search_helper.rb | 23 +++++++++++++++++++++++ 15 files changed, 52 insertions(+), 210 deletions(-) delete mode 100644 app/views/search/_facets_unselect_menu.rhtml delete mode 100644 app/views/search/_search_form.rhtml delete mode 120000 app/views/search/category_index.rhtml delete mode 100644 app/views/search/facets_browse.rhtml delete mode 100644 app/views/search/index.rhtml diff --git a/app/controllers/public/search_controller.rb b/app/controllers/public/search_controller.rb index 84774f8..7f1dcd7 100644 --- a/app/controllers/public/search_controller.rb +++ b/app/controllers/public/search_controller.rb @@ -4,7 +4,7 @@ class SearchController < PublicController include SearchHelper include ActionView::Helpers::NumberHelper - before_filter :redirect_asset_param, :except => [:facets_browse, :assets] + before_filter :redirect_asset_param, :except => :assets before_filter :load_category before_filter :load_search_assets before_filter :load_query @@ -18,19 +18,6 @@ class SearchController < PublicController no_design_blocks - def facets_browse - @asset = params[:asset].to_sym - @asset_class = asset_class(@asset) - - @facets_only = true - send(@asset) - - @facet = @asset_class.map_facets_for(environment).find { |facet| facet[:id] == params[:facet_id] } - raise 'Facet not found' if @facet.nil? - - render :layout => false - end - def articles if @search_engine && !@empty_query full_text_search @@ -54,21 +41,10 @@ class SearchController < PublicController end def products - if @search_engine && !@empty_query + if @search_engine full_text_search else - @geosearch = logged_in? && current_user.person.lat && current_user.person.lng - - extra_limit = LIST_SEARCH_LIMIT*5 - sql_options = {:limit => LIST_SEARCH_LIMIT, :order => 'random()'} - if @geosearch - full_text_search :sql_options => sql_options, :extra_limit => extra_limit, - :alternate_query => "{!boost b=recip(geodist(),#{"%e" % (1.to_f/DistBoost)},1,1)}", - :radius => DistFilt, :latitude => current_user.person.lat, :longitude => current_user.person.lng - else - full_text_search :sql_options => sql_options, :extra_limit => extra_limit, - :boost_functions => ['recip(ms(NOW/HOUR,updated_at),1.3e-10,1,1)'] - end + @results[@asset] = @environment.products.send(@filter).paginate(paginate_options) end render :template => 'search/search_page' end @@ -120,52 +96,12 @@ class SearchController < PublicController @next_calendar = populate_calendar(date + 1.month, events) end - def index - @results = {} - @order = [] - @names = {} - @results_only = true - - @enabled_searches.select { |key,description| @searching[key] }.each do |key, description| - load_query - @asset = key - send(key) - @order << key - @names[key] = getterm(description) - end - @asset = nil - @facets = {} - - render :action => @results.keys.first if @results.keys.size == 1 - end - # keep old URLs workings def assets params[:action] = params[:asset].is_a?(Array) ? :index : params.delete(:asset) redirect_to params end - # view the summary of one category - def category_index - @results = {} - @order = [] - @names = {} - limit = MULTIPLE_SEARCH_LIMIT - [ - [ :people, _('People'), :recent_people ], - [ :enterprises, _('Enterprises'), :recent_enterprises ], - [ :products, _('Products'), :recent_products ], - [ :events, _('Upcoming events'), :upcoming_events ], - [ :communities, _('Communities'), :recent_communities ], - [ :articles, _('Contents'), :recent_articles ] - ].each do |asset, name, filter| - @order << asset - @results[asset] = @category.send(filter, limit) - raise "No total_entries for: #{asset}" unless @results[asset].respond_to?(:total_entries) - @names[asset] = name - end - end - def tags @tags_cache_key = "tags_env_#{environment.id.to_s}" if is_cache_expired?(@tags_cache_key) @@ -260,7 +196,7 @@ class SearchController < PublicController @titles = {} @enabled_searches.each do |key, name| @titles[key] = _(name) - @searching[key] = params[:action] == 'index' || params[:action] == key.to_s + @searching[key] = params[:action] == key.to_s end @names = @titles if @names.nil? end diff --git a/app/helpers/search_helper.rb b/app/helpers/search_helper.rb index aac7d45..18910e0 100644 --- a/app/helpers/search_helper.rb +++ b/app/helpers/search_helper.rb @@ -4,8 +4,6 @@ module SearchHelper LIST_SEARCH_LIMIT = 20 BLOCKS_SEARCH_LIMIT = 24 MULTIPLE_SEARCH_LIMIT = 8 - DistFilt = 200 - DistBoost = 50 Searches = ActiveSupport::OrderedHash[ :articles, _('Contents'), @@ -16,37 +14,11 @@ module SearchHelper :events, _('Events'), ] - SortOptions = { - :products => ActiveSupport::OrderedHash[ :none, {:label => _('Relevance')}, - :more_recent, {:label => _('More recent'), :solr_opts => {:sort => 'updated_at desc, score desc'}}, - :name, {:label => _('Name'), :solr_opts => {:sort => 'solr_plugin_name_sortable asc'}}, - :closest, {:label => _('Closest to me'), :if => proc{ logged_in? && (profile=current_user.person).lat && profile.lng }, - :solr_opts => {:sort => "geodist() asc", - :latitude => proc{ current_user.person.lat }, :longitude => proc{ current_user.person.lng }}}, - ], - :events => ActiveSupport::OrderedHash[ :none, {:label => _('Relevance')}, - :name, {:label => _('Name'), :solr_opts => {:sort => 'solr_plugin_name_sortable asc'}}, - ], - :articles => ActiveSupport::OrderedHash[ :none, {:label => _('Relevance')}, - :name, {:label => _('Name'), :solr_opts => {:sort => 'solr_plugin_name_sortable asc'}}, - :more_recent, {:label => _('More recent'), :solr_opts => {:sort => 'updated_at desc, score desc'}}, - ], - :enterprises => ActiveSupport::OrderedHash[ :none, {:label => _('Relevance')}, - :name, {:label => _('Name'), :solr_opts => {:sort => 'solr_plugin_name_sortable asc'}}, - ], - :people => ActiveSupport::OrderedHash[ :none, {:label => _('Relevance')}, - :name, {:label => _('Name'), :solr_opts => {:sort => 'solr_plugin_name_sortable asc'}}, - ], - :communities => ActiveSupport::OrderedHash[ :none, {:label => _('Relevance')}, - :name, {:label => _('Name'), :solr_opts => {:sort => 'solr_plugin_name_sortable asc'}}, - ], - } - # FIXME remove it after search_controler refactored include EventsHelper def multiple_search? - ['index', 'category_index'].include?(params[:action]) or @results.size > 1 + @results.size > 1 end def map_search? @@ -95,23 +67,6 @@ module SearchHelper end end - def facet_javascript(input_id, facet, array) - array = [] if array.nil? - hintText = _('Type in an option') - text_field_tag('facet['+input_id+']', '', :id => input_id) + - javascript_tag("jQuery.TokenList(jQuery('##{input_id}'), #{array.to_json}, - {searchDelay: 0, permanentDropdown: true, theme: 'facet', dontAdd: true, preventDuplicates: true, - #{jquery_token_input_messages_json(hintText)}});") - end - - def asset_class(asset) - asset.to_s.singularize.camelize.constantize - end - - def asset_table(asset) - asset_class(asset).table_name - end - def display_filter(asset, display, float = 'right') if map_capable?(asset) list_link = display == 'list' ? _('List') : link_to(_('List'), params.merge(:display => 'list')) diff --git a/app/models/article.rb b/app/models/article.rb index 1ce2dc2..cbd0389 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -2,12 +2,6 @@ require 'hpricot' class Article < ActiveRecord::Base - # use for internationalizable human type names in search facets - # reimplement on subclasses - def self.type_name - _('Content') - end - track_actions :create_article, :after_create, :keep_params => [:name, :url, :lead, :first_image], :if => Proc.new { |a| a.is_trackable? && !a.image? } # xss_terminate plugin can't sanitize array fields diff --git a/app/models/environment.rb b/app/models/environment.rb index 9e36191..7714dea 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -266,8 +266,6 @@ class Environment < ActiveRecord::Base settings_items :search_hints, :type => Hash, :default => {} - settings_items :top_level_category_as_facet_ids, :type => Array, :default => [] - def news_amount_by_folder=(amount) settings[:news_amount_by_folder] = amount.to_i end diff --git a/app/models/profile.rb b/app/models/profile.rb index f14d666..4d3524c 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -3,12 +3,6 @@ # which by default is the one returned by Environment:default. class Profile < ActiveRecord::Base - # use for internationalizable human type names in search facets - # reimplement on subclasses - def self.type_name - _('Profile') - end - module Roles def self.admin(env_id) find_role('admin', env_id) diff --git a/app/views/search/_facets_unselect_menu.rhtml b/app/views/search/_facets_unselect_menu.rhtml deleted file mode 100644 index db1393b..0000000 --- a/app/views/search/_facets_unselect_menu.rhtml +++ /dev/null @@ -1,6 +0,0 @@ -
- <% if params[:facet] and params[:facet].count > 0 %> - <%= _("Applied filters") %> - <%= facet_selecteds_html_for(environment, asset_class(@asset), params) %> - <% end %> -
diff --git a/app/views/search/_search_form.rhtml b/app/views/search/_search_form.rhtml deleted file mode 100644 index e298996..0000000 --- a/app/views/search/_search_form.rhtml +++ /dev/null @@ -1,37 +0,0 @@ -
- - <% form_tag( { :controller => 'search', :action => @asset ? @asset : 'index', :asset => nil, :category_path => ( @category ? @category.explode_path : [] ) }, - :method => 'get', :class => 'search_form' ) do %> - - <%= hidden_field_tag :display, params[:display] %> - - <% params_uri = CGI::unescape(request.request_uri) %> - <% if params_uri.index('?') %> - <% params_uri[(params_uri.index('?')+1)..-1].to_s.split("&").each do |part| %> - <% if part.start_with? "facet" %> - <% name_value = part.split("=") %> - <%= hidden_field_tag name_value[0], name_value[1] %> - <% end %> - <% end %> - <% end %> - -
- - <%= text_field_tag 'query', @query, :id => 'search-input', :size => 50 %> - <%= javascript_tag "jQuery('#search-input').attr('title', \"#{hint}\").hint()" if defined?(hint) %> - - - <%= submit_button(:search, _('Search')) %> -
- - <% end %> - - <% if @empty_query %> - <% hint = environment.search_hints[@asset] %> - <% if hint and !hint.blank? %> -
<%= hint %>
- <% end %> - <% end %> - -
-
diff --git a/app/views/search/category_index.rhtml b/app/views/search/category_index.rhtml deleted file mode 120000 index 3dd5e55..0000000 --- a/app/views/search/category_index.rhtml +++ /dev/null @@ -1 +0,0 @@ -index.rhtml \ No newline at end of file diff --git a/app/views/search/facets_browse.rhtml b/app/views/search/facets_browse.rhtml deleted file mode 100644 index 7178788..0000000 --- a/app/views/search/facets_browse.rhtml +++ /dev/null @@ -1,8 +0,0 @@ -<% results = @asset_class.map_facet_results(@facet, params[:facet], @facets, @all_facets) %> - -<% array = [] %> -<% @asset_class.facet_result_sort(@facet, results, :alphabetically).each do |id, label, count| %> - <% array << {:id => id, :name => facet_link_html(@facet, params.merge(:controller => 'search', :action => @asset), id, label, count)} %> -<% end %> - -<%= facet_javascript('facet-input-'+@facet[:id].to_s, @facet, array) %> diff --git a/app/views/search/index.rhtml b/app/views/search/index.rhtml deleted file mode 100644 index ec950d0..0000000 --- a/app/views/search/index.rhtml +++ /dev/null @@ -1,29 +0,0 @@ -
"> - - <% if @category %> -
<%= image_tag(@category.image.public_filename(:thumb), :id => 'category-image') if @category.image %>
- <% end %> - - <%= search_page_title(_('Search Results'), @category) %> - <%= render :partial => 'search_form', :locals => { :hint => '' } %> - <%= category_context(@category, params) %> - <%= display_results(@asset) %> - -
- <% if @category %> -

<%= _('Sub-categories') %>

- <% if @category.children.empty? %> - <%= _('No sub-categories') %> - <% else %> -
    - <% @category.children.each do |c| %> -
  • <%= link_to_category c, false %>
  • - <% end %> -
- <% end %> - <% end %> -
- -
- -
diff --git a/plugins/solr/lib/ext/article.rb b/plugins/solr/lib/ext/article.rb index 3968b76..53d23d2 100644 --- a/plugins/solr/lib/ext/article.rb +++ b/plugins/solr/lib/ext/article.rb @@ -2,6 +2,12 @@ require_dependency 'article' class Article + # use for internationalizable human type names in search facets + # reimplement on subclasses + def self.type_name + _('Content') + end + acts_as_faceted :fields => { :solr_plugin_f_type => {:label => _('Type'), :proc => proc{|klass| solr_plugin_f_type_proc(klass)}}, :solr_plugin_f_published_at => {:type => :date, :label => _('Published date'), :queries => {'[* TO NOW-1YEARS/DAY]' => _("Older than one year"), diff --git a/plugins/solr/lib/ext/profile.rb b/plugins/solr/lib/ext/profile.rb index 96ebb2a..20eba29 100644 --- a/plugins/solr/lib/ext/profile.rb +++ b/plugins/solr/lib/ext/profile.rb @@ -1,6 +1,13 @@ require_dependency 'profile' class Profile + + # use for internationalizable human type names in search facets + # reimplement on subclasses + def self.type_name + _('Profile') + end + after_save_reindex [:articles], :with => :delayed_job acts_as_faceted :fields => { @@ -57,7 +64,7 @@ class Profile private def self.solr_plugin_f_categories_label_proc(environment) - ids = environment.top_level_category_as_facet_ids + ids = environment.solr_plugin_top_level_category_as_facet_ids r = Category.find(ids) map = {} ids.map{ |id| map[id.to_s] = r.detect{|c| c.id == id}.name } diff --git a/plugins/solr/lib/solr_plugin.rb b/plugins/solr/lib/solr_plugin.rb index d760d27..2e8ea22 100644 --- a/plugins/solr/lib/solr_plugin.rb +++ b/plugins/solr/lib/solr_plugin.rb @@ -19,6 +19,7 @@ class SolrPlugin < Noosfero::Plugin def full_text_search(asset, query, category, paginate_options) asset_class = asset_class(asset) solr_options = solr_options(asset, category) + solr_options.merge!(products_options(context.send(:user))) if asset == :products && empty_query?(query, category) asset_class.find_by_contents(query, paginate_options, solr_options) end diff --git a/plugins/solr/lib/solr_plugin/results_helper.rb b/plugins/solr/lib/solr_plugin/results_helper.rb index 2311394..96b92b6 100644 --- a/plugins/solr/lib/solr_plugin/results_helper.rb +++ b/plugins/solr/lib/solr_plugin/results_helper.rb @@ -109,5 +109,14 @@ class SolrPlugin < Noosfero::Plugin (count > 0 ? content_tag('span', " (#{count})", :class => 'facet-result-count') : ''), :class => 'facet-menu-item' + (selected ? ' facet-result-link-selected' : '') end + + def facet_javascript(input_id, facet, array) + array = [] if array.nil? + hintText = _('Type in an option') + text_field_tag('facet['+input_id+']', '', :id => input_id) + + javascript_tag("jQuery.TokenList(jQuery('##{input_id}'), #{array.to_json}, + {searchDelay: 0, permanentDropdown: true, theme: 'facet', dontAdd: true, preventDuplicates: true, + #{jquery_token_input_messages_json(hintText)}});") + end end end diff --git a/plugins/solr/lib/solr_plugin/search_helper.rb b/plugins/solr/lib/solr_plugin/search_helper.rb index 0d0dac9..d467bf7 100644 --- a/plugins/solr/lib/solr_plugin/search_helper.rb +++ b/plugins/solr/lib/solr_plugin/search_helper.rb @@ -1,5 +1,9 @@ class SolrPlugin < Noosfero::Plugin + LIST_SEARCH_LIMIT = 20 + DistFilt = 200 + DistBoost = 50 + SortOptions = { :products => ActiveSupport::OrderedHash[ :none, {:label => _('Relevance')}, :more_recent, {:label => _('More recent'), :solr_opts => {:sort => 'updated_at desc, score desc'}}, @@ -54,6 +58,10 @@ class SolrPlugin < Noosfero::Plugin context.params[:action] == 'index' end + def empty_query?(query, category) + category.nil? && query.blank? + end + def solr_options(asset, category) asset_class = asset_class(asset) solr_options = {} @@ -79,5 +87,20 @@ class SolrPlugin < Noosfero::Plugin end solr_options end + + def products_options(person) + geosearch = person && person.lat && person.lng + + extra_limit = LIST_SEARCH_LIMIT*5 + sql_options = {:limit => LIST_SEARCH_LIMIT, :order => 'random()'} + if geosearch + {:sql_options => sql_options, :extra_limit => extra_limit, + :alternate_query => "{!boost b=recip(geodist(),#{"%e" % (1.to_f/DistBoost)},1,1)}", + :radius => DistFilt, :latitude => person.lat, :longitude => person.lng} + else + { :sql_options => sql_options, :extra_limit => extra_limit, + :boost_functions => ['recip(ms(NOW/HOUR,updated_at),1.3e-10,1,1)']} + end + end end end -- libgit2 0.21.2