Commit b4d274ac026384670984bf265046549a9d3c39e6

Authored by Rodrigo Souto
1 parent 6e82bdc7

[otw] Deicinding whether to keed index or not... =(

app/controllers/public/search_controller.rb
@@ -4,7 +4,7 @@ class SearchController < PublicController @@ -4,7 +4,7 @@ class SearchController < PublicController
4 include SearchHelper 4 include SearchHelper
5 include ActionView::Helpers::NumberHelper 5 include ActionView::Helpers::NumberHelper
6 6
7 - before_filter :redirect_asset_param, :except => [:facets_browse, :assets] 7 + before_filter :redirect_asset_param, :except => :assets
8 before_filter :load_category 8 before_filter :load_category
9 before_filter :load_search_assets 9 before_filter :load_search_assets
10 before_filter :load_query 10 before_filter :load_query
@@ -18,19 +18,6 @@ class SearchController < PublicController @@ -18,19 +18,6 @@ class SearchController < PublicController
18 18
19 no_design_blocks 19 no_design_blocks
20 20
21 - def facets_browse  
22 - @asset = params[:asset].to_sym  
23 - @asset_class = asset_class(@asset)  
24 -  
25 - @facets_only = true  
26 - send(@asset)  
27 -  
28 - @facet = @asset_class.map_facets_for(environment).find { |facet| facet[:id] == params[:facet_id] }  
29 - raise 'Facet not found' if @facet.nil?  
30 -  
31 - render :layout => false  
32 - end  
33 -  
34 def articles 21 def articles
35 if @search_engine && !@empty_query 22 if @search_engine && !@empty_query
36 full_text_search 23 full_text_search
@@ -54,21 +41,10 @@ class SearchController < PublicController @@ -54,21 +41,10 @@ class SearchController < PublicController
54 end 41 end
55 42
56 def products 43 def products
57 - if @search_engine && !@empty_query 44 + if @search_engine
58 full_text_search 45 full_text_search
59 else 46 else
60 - @geosearch = logged_in? && current_user.person.lat && current_user.person.lng  
61 -  
62 - extra_limit = LIST_SEARCH_LIMIT*5  
63 - sql_options = {:limit => LIST_SEARCH_LIMIT, :order => 'random()'}  
64 - if @geosearch  
65 - full_text_search :sql_options => sql_options, :extra_limit => extra_limit,  
66 - :alternate_query => "{!boost b=recip(geodist(),#{"%e" % (1.to_f/DistBoost)},1,1)}",  
67 - :radius => DistFilt, :latitude => current_user.person.lat, :longitude => current_user.person.lng  
68 - else  
69 - full_text_search :sql_options => sql_options, :extra_limit => extra_limit,  
70 - :boost_functions => ['recip(ms(NOW/HOUR,updated_at),1.3e-10,1,1)']  
71 - end 47 + @results[@asset] = @environment.products.send(@filter).paginate(paginate_options)
72 end 48 end
73 render :template => 'search/search_page' 49 render :template => 'search/search_page'
74 end 50 end
@@ -120,52 +96,12 @@ class SearchController < PublicController @@ -120,52 +96,12 @@ class SearchController < PublicController
120 @next_calendar = populate_calendar(date + 1.month, events) 96 @next_calendar = populate_calendar(date + 1.month, events)
121 end 97 end
122 98
123 - def index  
124 - @results = {}  
125 - @order = []  
126 - @names = {}  
127 - @results_only = true  
128 -  
129 - @enabled_searches.select { |key,description| @searching[key] }.each do |key, description|  
130 - load_query  
131 - @asset = key  
132 - send(key)  
133 - @order << key  
134 - @names[key] = getterm(description)  
135 - end  
136 - @asset = nil  
137 - @facets = {}  
138 -  
139 - render :action => @results.keys.first if @results.keys.size == 1  
140 - end  
141 -  
142 # keep old URLs workings 99 # keep old URLs workings
143 def assets 100 def assets
144 params[:action] = params[:asset].is_a?(Array) ? :index : params.delete(:asset) 101 params[:action] = params[:asset].is_a?(Array) ? :index : params.delete(:asset)
145 redirect_to params 102 redirect_to params
146 end 103 end
147 104
148 - # view the summary of one category  
149 - def category_index  
150 - @results = {}  
151 - @order = []  
152 - @names = {}  
153 - limit = MULTIPLE_SEARCH_LIMIT  
154 - [  
155 - [ :people, _('People'), :recent_people ],  
156 - [ :enterprises, _('Enterprises'), :recent_enterprises ],  
157 - [ :products, _('Products'), :recent_products ],  
158 - [ :events, _('Upcoming events'), :upcoming_events ],  
159 - [ :communities, _('Communities'), :recent_communities ],  
160 - [ :articles, _('Contents'), :recent_articles ]  
161 - ].each do |asset, name, filter|  
162 - @order << asset  
163 - @results[asset] = @category.send(filter, limit)  
164 - raise "No total_entries for: #{asset}" unless @results[asset].respond_to?(:total_entries)  
165 - @names[asset] = name  
166 - end  
167 - end  
168 -  
169 def tags 105 def tags
170 @tags_cache_key = "tags_env_#{environment.id.to_s}" 106 @tags_cache_key = "tags_env_#{environment.id.to_s}"
171 if is_cache_expired?(@tags_cache_key) 107 if is_cache_expired?(@tags_cache_key)
@@ -260,7 +196,7 @@ class SearchController &lt; PublicController @@ -260,7 +196,7 @@ class SearchController &lt; PublicController
260 @titles = {} 196 @titles = {}
261 @enabled_searches.each do |key, name| 197 @enabled_searches.each do |key, name|
262 @titles[key] = _(name) 198 @titles[key] = _(name)
263 - @searching[key] = params[:action] == 'index' || params[:action] == key.to_s 199 + @searching[key] = params[:action] == key.to_s
264 end 200 end
265 @names = @titles if @names.nil? 201 @names = @titles if @names.nil?
266 end 202 end
app/helpers/search_helper.rb
@@ -4,8 +4,6 @@ module SearchHelper @@ -4,8 +4,6 @@ module SearchHelper
4 LIST_SEARCH_LIMIT = 20 4 LIST_SEARCH_LIMIT = 20
5 BLOCKS_SEARCH_LIMIT = 24 5 BLOCKS_SEARCH_LIMIT = 24
6 MULTIPLE_SEARCH_LIMIT = 8 6 MULTIPLE_SEARCH_LIMIT = 8
7 - DistFilt = 200  
8 - DistBoost = 50  
9 7
10 Searches = ActiveSupport::OrderedHash[ 8 Searches = ActiveSupport::OrderedHash[
11 :articles, _('Contents'), 9 :articles, _('Contents'),
@@ -16,37 +14,11 @@ module SearchHelper @@ -16,37 +14,11 @@ module SearchHelper
16 :events, _('Events'), 14 :events, _('Events'),
17 ] 15 ]
18 16
19 - SortOptions = {  
20 - :products => ActiveSupport::OrderedHash[ :none, {:label => _('Relevance')},  
21 - :more_recent, {:label => _('More recent'), :solr_opts => {:sort => 'updated_at desc, score desc'}},  
22 - :name, {:label => _('Name'), :solr_opts => {:sort => 'solr_plugin_name_sortable asc'}},  
23 - :closest, {:label => _('Closest to me'), :if => proc{ logged_in? && (profile=current_user.person).lat && profile.lng },  
24 - :solr_opts => {:sort => "geodist() asc",  
25 - :latitude => proc{ current_user.person.lat }, :longitude => proc{ current_user.person.lng }}},  
26 - ],  
27 - :events => ActiveSupport::OrderedHash[ :none, {:label => _('Relevance')},  
28 - :name, {:label => _('Name'), :solr_opts => {:sort => 'solr_plugin_name_sortable asc'}},  
29 - ],  
30 - :articles => ActiveSupport::OrderedHash[ :none, {:label => _('Relevance')},  
31 - :name, {:label => _('Name'), :solr_opts => {:sort => 'solr_plugin_name_sortable asc'}},  
32 - :more_recent, {:label => _('More recent'), :solr_opts => {:sort => 'updated_at desc, score desc'}},  
33 - ],  
34 - :enterprises => ActiveSupport::OrderedHash[ :none, {:label => _('Relevance')},  
35 - :name, {:label => _('Name'), :solr_opts => {:sort => 'solr_plugin_name_sortable asc'}},  
36 - ],  
37 - :people => ActiveSupport::OrderedHash[ :none, {:label => _('Relevance')},  
38 - :name, {:label => _('Name'), :solr_opts => {:sort => 'solr_plugin_name_sortable asc'}},  
39 - ],  
40 - :communities => ActiveSupport::OrderedHash[ :none, {:label => _('Relevance')},  
41 - :name, {:label => _('Name'), :solr_opts => {:sort => 'solr_plugin_name_sortable asc'}},  
42 - ],  
43 - }  
44 -  
45 # FIXME remove it after search_controler refactored 17 # FIXME remove it after search_controler refactored
46 include EventsHelper 18 include EventsHelper
47 19
48 def multiple_search? 20 def multiple_search?
49 - ['index', 'category_index'].include?(params[:action]) or @results.size > 1 21 + @results.size > 1
50 end 22 end
51 23
52 def map_search? 24 def map_search?
@@ -95,23 +67,6 @@ module SearchHelper @@ -95,23 +67,6 @@ module SearchHelper
95 end 67 end
96 end 68 end
97 69
98 - def facet_javascript(input_id, facet, array)  
99 - array = [] if array.nil?  
100 - hintText = _('Type in an option')  
101 - text_field_tag('facet['+input_id+']', '', :id => input_id) +  
102 - javascript_tag("jQuery.TokenList(jQuery('##{input_id}'), #{array.to_json},  
103 - {searchDelay: 0, permanentDropdown: true, theme: 'facet', dontAdd: true, preventDuplicates: true,  
104 - #{jquery_token_input_messages_json(hintText)}});")  
105 - end  
106 -  
107 - def asset_class(asset)  
108 - asset.to_s.singularize.camelize.constantize  
109 - end  
110 -  
111 - def asset_table(asset)  
112 - asset_class(asset).table_name  
113 - end  
114 -  
115 def display_filter(asset, display, float = 'right') 70 def display_filter(asset, display, float = 'right')
116 if map_capable?(asset) 71 if map_capable?(asset)
117 list_link = display == 'list' ? _('List') : link_to(_('List'), params.merge(:display => 'list')) 72 list_link = display == 'list' ? _('List') : link_to(_('List'), params.merge(:display => 'list'))
app/models/article.rb
@@ -2,12 +2,6 @@ require &#39;hpricot&#39; @@ -2,12 +2,6 @@ require &#39;hpricot&#39;
2 2
3 class Article < ActiveRecord::Base 3 class Article < ActiveRecord::Base
4 4
5 - # use for internationalizable human type names in search facets  
6 - # reimplement on subclasses  
7 - def self.type_name  
8 - _('Content')  
9 - end  
10 -  
11 track_actions :create_article, :after_create, :keep_params => [:name, :url, :lead, :first_image], :if => Proc.new { |a| a.is_trackable? && !a.image? } 5 track_actions :create_article, :after_create, :keep_params => [:name, :url, :lead, :first_image], :if => Proc.new { |a| a.is_trackable? && !a.image? }
12 6
13 # xss_terminate plugin can't sanitize array fields 7 # xss_terminate plugin can't sanitize array fields
app/models/environment.rb
@@ -266,8 +266,6 @@ class Environment &lt; ActiveRecord::Base @@ -266,8 +266,6 @@ class Environment &lt; ActiveRecord::Base
266 266
267 settings_items :search_hints, :type => Hash, :default => {} 267 settings_items :search_hints, :type => Hash, :default => {}
268 268
269 - settings_items :top_level_category_as_facet_ids, :type => Array, :default => []  
270 -  
271 def news_amount_by_folder=(amount) 269 def news_amount_by_folder=(amount)
272 settings[:news_amount_by_folder] = amount.to_i 270 settings[:news_amount_by_folder] = amount.to_i
273 end 271 end
app/models/profile.rb
@@ -3,12 +3,6 @@ @@ -3,12 +3,6 @@
3 # which by default is the one returned by Environment:default. 3 # which by default is the one returned by Environment:default.
4 class Profile < ActiveRecord::Base 4 class Profile < ActiveRecord::Base
5 5
6 - # use for internationalizable human type names in search facets  
7 - # reimplement on subclasses  
8 - def self.type_name  
9 - _('Profile')  
10 - end  
11 -  
12 module Roles 6 module Roles
13 def self.admin(env_id) 7 def self.admin(env_id)
14 find_role('admin', env_id) 8 find_role('admin', env_id)
app/views/search/_facets_unselect_menu.rhtml
@@ -1,6 +0,0 @@ @@ -1,6 +0,0 @@
1 -<div class="facets-applied">  
2 - <% if params[:facet] and params[:facet].count > 0 %>  
3 - <span class="facets-applied-label"><%= _("Applied filters") %> </span>  
4 - <%= facet_selecteds_html_for(environment, asset_class(@asset), params) %>  
5 - <% end %>  
6 -</div>  
app/views/search/_search_form.rhtml
@@ -1,37 +0,0 @@ @@ -1,37 +0,0 @@
1 -<div class='search-form'>  
2 -  
3 - <% form_tag( { :controller => 'search', :action => @asset ? @asset : 'index', :asset => nil, :category_path => ( @category ? @category.explode_path : [] ) },  
4 - :method => 'get', :class => 'search_form' ) do %>  
5 -  
6 - <%= hidden_field_tag :display, params[:display] %>  
7 -  
8 - <% params_uri = CGI::unescape(request.request_uri) %>  
9 - <% if params_uri.index('?') %>  
10 - <% params_uri[(params_uri.index('?')+1)..-1].to_s.split("&").each do |part| %>  
11 - <% if part.start_with? "facet" %>  
12 - <% name_value = part.split("=") %>  
13 - <%= hidden_field_tag name_value[0], name_value[1] %>  
14 - <% end %>  
15 - <% end %>  
16 - <% end %>  
17 -  
18 - <div class="search-field">  
19 - <span class="formfield">  
20 - <%= text_field_tag 'query', @query, :id => 'search-input', :size => 50 %>  
21 - <%= javascript_tag "jQuery('#search-input').attr('title', \"#{hint}\").hint()" if defined?(hint) %>  
22 - </span>  
23 -  
24 - <%= submit_button(:search, _('Search')) %>  
25 - </div>  
26 -  
27 - <% end %>  
28 -  
29 - <% if @empty_query %>  
30 - <% hint = environment.search_hints[@asset] %>  
31 - <% if hint and !hint.blank? %>  
32 - <div class="search-hint"><%= hint %></div>  
33 - <% end %>  
34 - <% end %>  
35 -  
36 - <div style="clear: both"></div>  
37 -</div>  
app/views/search/category_index.rhtml
@@ -1 +0,0 @@ @@ -1 +0,0 @@
1 -index.rhtml  
2 \ No newline at end of file 0 \ No newline at end of file
app/views/search/facets_browse.rhtml
@@ -1,8 +0,0 @@ @@ -1,8 +0,0 @@
1 -<% results = @asset_class.map_facet_results(@facet, params[:facet], @facets, @all_facets) %>  
2 -  
3 -<% array = [] %>  
4 -<% @asset_class.facet_result_sort(@facet, results, :alphabetically).each do |id, label, count| %>  
5 - <% array << {:id => id, :name => facet_link_html(@facet, params.merge(:controller => 'search', :action => @asset), id, label, count)} %>  
6 -<% end %>  
7 -  
8 -<%= facet_javascript('facet-input-'+@facet[:id].to_s, @facet, array) %>  
app/views/search/index.rhtml
@@ -1,29 +0,0 @@ @@ -1,29 +0,0 @@
1 -<div id="search-page" class="<%= "view-category" if @category %>">  
2 -  
3 - <% if @category %>  
4 - <div id="category-image"><%= image_tag(@category.image.public_filename(:thumb), :id => 'category-image') if @category.image %></div>  
5 - <% end %>  
6 -  
7 - <%= search_page_title(_('Search Results'), @category) %>  
8 - <%= render :partial => 'search_form', :locals => { :hint => '' } %>  
9 - <%= category_context(@category, params) %>  
10 - <%= display_results(@asset) %>  
11 -  
12 - <div id="category-childs">  
13 - <% if @category %>  
14 - <h2> <%= _('Sub-categories') %> </h2>  
15 - <% if @category.children.empty? %>  
16 - <strong id="cat-no-child"><%= _('No sub-categories') %></strong>  
17 - <% else %>  
18 - <ul>  
19 - <% @category.children.each do |c| %>  
20 - <li> <%= link_to_category c, false %> </li>  
21 - <% end %>  
22 - </ul>  
23 - <% end %>  
24 - <% end %>  
25 - </div>  
26 -  
27 -</div>  
28 -  
29 -<div style="clear: both"></div>  
plugins/solr/lib/ext/article.rb
@@ -2,6 +2,12 @@ require_dependency &#39;article&#39; @@ -2,6 +2,12 @@ require_dependency &#39;article&#39;
2 2
3 class Article 3 class Article
4 4
  5 + # use for internationalizable human type names in search facets
  6 + # reimplement on subclasses
  7 + def self.type_name
  8 + _('Content')
  9 + end
  10 +
5 acts_as_faceted :fields => { 11 acts_as_faceted :fields => {
6 :solr_plugin_f_type => {:label => _('Type'), :proc => proc{|klass| solr_plugin_f_type_proc(klass)}}, 12 :solr_plugin_f_type => {:label => _('Type'), :proc => proc{|klass| solr_plugin_f_type_proc(klass)}},
7 :solr_plugin_f_published_at => {:type => :date, :label => _('Published date'), :queries => {'[* TO NOW-1YEARS/DAY]' => _("Older than one year"), 13 :solr_plugin_f_published_at => {:type => :date, :label => _('Published date'), :queries => {'[* TO NOW-1YEARS/DAY]' => _("Older than one year"),
plugins/solr/lib/ext/profile.rb
1 require_dependency 'profile' 1 require_dependency 'profile'
2 2
3 class Profile 3 class Profile
  4 +
  5 + # use for internationalizable human type names in search facets
  6 + # reimplement on subclasses
  7 + def self.type_name
  8 + _('Profile')
  9 + end
  10 +
4 after_save_reindex [:articles], :with => :delayed_job 11 after_save_reindex [:articles], :with => :delayed_job
5 12
6 acts_as_faceted :fields => { 13 acts_as_faceted :fields => {
@@ -57,7 +64,7 @@ class Profile @@ -57,7 +64,7 @@ class Profile
57 private 64 private
58 65
59 def self.solr_plugin_f_categories_label_proc(environment) 66 def self.solr_plugin_f_categories_label_proc(environment)
60 - ids = environment.top_level_category_as_facet_ids 67 + ids = environment.solr_plugin_top_level_category_as_facet_ids
61 r = Category.find(ids) 68 r = Category.find(ids)
62 map = {} 69 map = {}
63 ids.map{ |id| map[id.to_s] = r.detect{|c| c.id == id}.name } 70 ids.map{ |id| map[id.to_s] = r.detect{|c| c.id == id}.name }
plugins/solr/lib/solr_plugin.rb
@@ -19,6 +19,7 @@ class SolrPlugin &lt; Noosfero::Plugin @@ -19,6 +19,7 @@ class SolrPlugin &lt; Noosfero::Plugin
19 def full_text_search(asset, query, category, paginate_options) 19 def full_text_search(asset, query, category, paginate_options)
20 asset_class = asset_class(asset) 20 asset_class = asset_class(asset)
21 solr_options = solr_options(asset, category) 21 solr_options = solr_options(asset, category)
  22 + solr_options.merge!(products_options(context.send(:user))) if asset == :products && empty_query?(query, category)
22 asset_class.find_by_contents(query, paginate_options, solr_options) 23 asset_class.find_by_contents(query, paginate_options, solr_options)
23 end 24 end
24 25
plugins/solr/lib/solr_plugin/results_helper.rb
@@ -109,5 +109,14 @@ class SolrPlugin &lt; Noosfero::Plugin @@ -109,5 +109,14 @@ class SolrPlugin &lt; Noosfero::Plugin
109 (count > 0 ? content_tag('span', " (#{count})", :class => 'facet-result-count') : ''), 109 (count > 0 ? content_tag('span', " (#{count})", :class => 'facet-result-count') : ''),
110 :class => 'facet-menu-item' + (selected ? ' facet-result-link-selected' : '') 110 :class => 'facet-menu-item' + (selected ? ' facet-result-link-selected' : '')
111 end 111 end
  112 +
  113 + def facet_javascript(input_id, facet, array)
  114 + array = [] if array.nil?
  115 + hintText = _('Type in an option')
  116 + text_field_tag('facet['+input_id+']', '', :id => input_id) +
  117 + javascript_tag("jQuery.TokenList(jQuery('##{input_id}'), #{array.to_json},
  118 + {searchDelay: 0, permanentDropdown: true, theme: 'facet', dontAdd: true, preventDuplicates: true,
  119 + #{jquery_token_input_messages_json(hintText)}});")
  120 + end
112 end 121 end
113 end 122 end
plugins/solr/lib/solr_plugin/search_helper.rb
1 class SolrPlugin < Noosfero::Plugin 1 class SolrPlugin < Noosfero::Plugin
2 2
  3 + LIST_SEARCH_LIMIT = 20
  4 + DistFilt = 200
  5 + DistBoost = 50
  6 +
3 SortOptions = { 7 SortOptions = {
4 :products => ActiveSupport::OrderedHash[ :none, {:label => _('Relevance')}, 8 :products => ActiveSupport::OrderedHash[ :none, {:label => _('Relevance')},
5 :more_recent, {:label => _('More recent'), :solr_opts => {:sort => 'updated_at desc, score desc'}}, 9 :more_recent, {:label => _('More recent'), :solr_opts => {:sort => 'updated_at desc, score desc'}},
@@ -54,6 +58,10 @@ class SolrPlugin &lt; Noosfero::Plugin @@ -54,6 +58,10 @@ class SolrPlugin &lt; Noosfero::Plugin
54 context.params[:action] == 'index' 58 context.params[:action] == 'index'
55 end 59 end
56 60
  61 + def empty_query?(query, category)
  62 + category.nil? && query.blank?
  63 + end
  64 +
57 def solr_options(asset, category) 65 def solr_options(asset, category)
58 asset_class = asset_class(asset) 66 asset_class = asset_class(asset)
59 solr_options = {} 67 solr_options = {}
@@ -79,5 +87,20 @@ class SolrPlugin &lt; Noosfero::Plugin @@ -79,5 +87,20 @@ class SolrPlugin &lt; Noosfero::Plugin
79 end 87 end
80 solr_options 88 solr_options
81 end 89 end
  90 +
  91 + def products_options(person)
  92 + geosearch = person && person.lat && person.lng
  93 +
  94 + extra_limit = LIST_SEARCH_LIMIT*5
  95 + sql_options = {:limit => LIST_SEARCH_LIMIT, :order => 'random()'}
  96 + if geosearch
  97 + {:sql_options => sql_options, :extra_limit => extra_limit,
  98 + :alternate_query => "{!boost b=recip(geodist(),#{"%e" % (1.to_f/DistBoost)},1,1)}",
  99 + :radius => DistFilt, :latitude => person.lat, :longitude => person.lng}
  100 + else
  101 + { :sql_options => sql_options, :extra_limit => extra_limit,
  102 + :boost_functions => ['recip(ms(NOW/HOUR,updated_at),1.3e-10,1,1)']}
  103 + end
  104 + end
82 end 105 end
83 end 106 end