Commit cebb76c40b27d5d4d58d2300a2f808ac9697ab1c
Committed by
Macartur Sousa
1 parent
174fea9a
Exists in
fix_sign_up_form
Elasticsearch: Fixes categories
* Adding method categories_data to return a json with categories list * Adds category params to controller * Adds dynamic categories via javascript url * Merges test changes from filter branch * Fixes plugin javascript to use & in regex and uses categories Signed-off-by: DylanGuedes <djmgguedes@gmail.com> Signed-off-by: Macartur de Sousa <macartur.sc@gmail.com>
Showing
9 changed files
with
117 additions
and
17 deletions
Show diff stats
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
1 | require_relative '../helpers/elasticsearch_helper' | 1 | require_relative '../helpers/elasticsearch_helper' |
2 | +require_relative '../helpers/elasticsearch_plugin_helper' | ||
2 | 3 | ||
3 | class ElasticsearchPluginController < ApplicationController | 4 | class ElasticsearchPluginController < ApplicationController |
4 | no_design_blocks | 5 | no_design_blocks |
5 | include ElasticsearchHelper | 6 | include ElasticsearchHelper |
7 | + helper ElasticsearchPluginHelper | ||
6 | 8 | ||
7 | def index | 9 | def index |
8 | search() | 10 | search() |
@@ -12,6 +14,7 @@ class ElasticsearchPluginController < ApplicationController | @@ -12,6 +14,7 @@ class ElasticsearchPluginController < ApplicationController | ||
12 | def search | 14 | def search |
13 | define_searchable_types | 15 | define_searchable_types |
14 | define_sort_types | 16 | define_sort_types |
17 | + define_categories | ||
15 | define_results | 18 | define_results |
16 | end | 19 | end |
17 | 20 | ||
@@ -23,7 +26,7 @@ class ElasticsearchPluginController < ApplicationController | @@ -23,7 +26,7 @@ class ElasticsearchPluginController < ApplicationController | ||
23 | 26 | ||
24 | def define_searchable_types | 27 | def define_searchable_types |
25 | @searchable_types = searchable_types | 28 | @searchable_types = searchable_types |
26 | - @selected_type = (params[:selected_type]|| :all ).to_sym | 29 | + @selected_type = (params[:selected_type] || :all).to_sym |
27 | end | 30 | end |
28 | 31 | ||
29 | def define_sort_types | 32 | def define_sort_types |
@@ -31,4 +34,9 @@ class ElasticsearchPluginController < ApplicationController | @@ -31,4 +34,9 @@ class ElasticsearchPluginController < ApplicationController | ||
31 | @selected_sort = (params[:filter] || :relevance).to_sym | 34 | @selected_sort = (params[:filter] || :relevance).to_sym |
32 | end | 35 | end |
33 | 36 | ||
37 | + def define_categories | ||
38 | + @categories = Category.where(parent: nil) | ||
39 | + @selected_categories = (params[:categories] || "").split(",") | ||
40 | + end | ||
41 | + | ||
34 | end | 42 | end |
plugins/elasticsearch/helpers/elasticsearch_helper.rb
@@ -37,7 +37,7 @@ module ElasticsearchHelper | @@ -37,7 +37,7 @@ module ElasticsearchHelper | ||
37 | def search_from_all_models | 37 | def search_from_all_models |
38 | begin | 38 | begin |
39 | filter = (params[:filter] || "" ).to_sym | 39 | filter = (params[:filter] || "" ).to_sym |
40 | - query = get_query params[:query], sort_by: get_sort_by(filter) | 40 | + query = get_query params[:query], sort_by: get_sort_by(filter), categories: params[:categories] |
41 | Elasticsearch::Model.search(query,searchable_models, size: default_per_page(params[:per_page])).page(params[:page]).records | 41 | Elasticsearch::Model.search(query,searchable_models, size: default_per_page(params[:per_page])).page(params[:page]).records |
42 | rescue | 42 | rescue |
43 | [] | 43 | [] |
@@ -48,7 +48,7 @@ module ElasticsearchHelper | @@ -48,7 +48,7 @@ module ElasticsearchHelper | ||
48 | begin | 48 | begin |
49 | klass = model.to_s.classify.constantize | 49 | klass = model.to_s.classify.constantize |
50 | filter = (params[:filter] || "" ).to_sym | 50 | filter = (params[:filter] || "" ).to_sym |
51 | - query = get_query params[:query], klass: klass, sort_by: get_sort_by(filter,klass) | 51 | + query = get_query params[:query], klass: klass, sort_by: get_sort_by(filter ,klass), categories: params[:categories] |
52 | klass.search(query, size: default_per_page(params[:per_page])).page(params[:page]).records | 52 | klass.search(query, size: default_per_page(params[:per_page])).page(params[:page]).records |
53 | rescue | 53 | rescue |
54 | [] | 54 | [] |
@@ -87,28 +87,37 @@ module ElasticsearchHelper | @@ -87,28 +87,37 @@ module ElasticsearchHelper | ||
87 | end | 87 | end |
88 | 88 | ||
89 | 89 | ||
90 | - def query_method expression="", models=[] | ||
91 | - { | ||
92 | - query: { | ||
93 | - filtered: { | ||
94 | - query: query_string(expression,models), | ||
95 | - filter: { | ||
96 | - bool: { | ||
97 | - should: models.map {|model| model.filter(environment: @environment.id) } | ||
98 | - } | ||
99 | - } | 90 | + def query_method expression="", models=[], categories=[] |
91 | + query = {} | ||
92 | + query[:query] = { | ||
93 | + filtered: { | ||
94 | + query: query_string(expression, models), | ||
95 | + filter: { | ||
96 | + bool: {} | ||
100 | } | 97 | } |
101 | } | 98 | } |
102 | } | 99 | } |
100 | + | ||
101 | + query[:query][:filtered][:filter][:bool] = { | ||
102 | + should: models.map {|model| model.filter(environment: @environment.id) } | ||
103 | + } | ||
104 | + | ||
105 | + unless categories.blank? | ||
106 | + query[:query][:filtered][:filter][:bool][:must] = models.first.filter_category(categories) | ||
107 | + end | ||
108 | + | ||
109 | + query | ||
103 | end | 110 | end |
104 | 111 | ||
105 | def get_query text="", options={} | 112 | def get_query text="", options={} |
106 | klass = options[:klass] | 113 | klass = options[:klass] |
107 | sort_by = options[:sort_by] | 114 | sort_by = options[:sort_by] |
115 | + categories = (options[:categories] || "").split(",") | ||
116 | + categories = categories.map(&:to_i) | ||
108 | 117 | ||
109 | models = (klass.nil?) ? searchable_models : [klass] | 118 | models = (klass.nil?) ? searchable_models : [klass] |
110 | 119 | ||
111 | - query = query_method(text, models) | 120 | + query = query_method(text, models, categories) |
112 | query[:sort] = sort_by if sort_by | 121 | query[:sort] = sort_by if sort_by |
113 | 122 | ||
114 | query | 123 | query |
plugins/elasticsearch/helpers/elasticsearch_plugin_helper.rb
0 → 100644
@@ -0,0 +1,20 @@ | @@ -0,0 +1,20 @@ | ||
1 | +module ElasticsearchPluginHelper | ||
2 | + | ||
3 | + def render_categories(collection, selected_collections) | ||
4 | + content_tag :ul, class: "category-ident" do | ||
5 | + if collection.respond_to? :each | ||
6 | + collection.collect do |item| | ||
7 | + concat ("<li>".html_safe) | ||
8 | + concat (check_box_tag(item.name, item.id, selected_collections.include?(item.id.to_s)) ) | ||
9 | + concat (label_tag item.name) | ||
10 | + concat (render_categories(item.children, selected_collections)) if item.children_count > 0 | ||
11 | + concat ("</li>".html_safe) | ||
12 | + end | ||
13 | + else | ||
14 | + check_box_tag collection.name, collection.id, selected_collections.include?(collection.id) | ||
15 | + label_tag collection.name | ||
16 | + end | ||
17 | + end | ||
18 | + end | ||
19 | + | ||
20 | +end |
plugins/elasticsearch/lib/elasticsearch_plugin.rb
@@ -16,6 +16,10 @@ class ElasticsearchPlugin < Noosfero::Plugin | @@ -16,6 +16,10 @@ class ElasticsearchPlugin < Noosfero::Plugin | ||
16 | true | 16 | true |
17 | end | 17 | end |
18 | 18 | ||
19 | + def js_files | ||
20 | + ['categories'].map{ |j| "javascripts/#{j}" } | ||
21 | + end | ||
22 | + | ||
19 | def search_controller_filters | 23 | def search_controller_filters |
20 | block = proc do | 24 | block = proc do |
21 | 25 |
plugins/elasticsearch/lib/ext/text_article.rb
@@ -11,7 +11,7 @@ class TextArticle | @@ -11,7 +11,7 @@ class TextArticle | ||
11 | def self.control_fields | 11 | def self.control_fields |
12 | { | 12 | { |
13 | :advertise => { type: :boolean }, | 13 | :advertise => { type: :boolean }, |
14 | - :published => { type: 'boolean'}, | 14 | + :published => { type: :boolean }, |
15 | :comments_count => { type: :integer }, | 15 | :comments_count => { type: :integer }, |
16 | :hits => { type: :integer }, | 16 | :hits => { type: :integer }, |
17 | :profile => { type: :nested , hash: NestedProfile.hash } | 17 | :profile => { type: :nested , hash: NestedProfile.hash } |
plugins/elasticsearch/lib/searchable_model/filter.rb
@@ -32,6 +32,15 @@ module Filter | @@ -32,6 +32,15 @@ module Filter | ||
32 | 32 | ||
33 | result_filter | 33 | result_filter |
34 | end | 34 | end |
35 | + | ||
36 | + def filter_category selected_categories | ||
37 | + { | ||
38 | + query: { | ||
39 | + terms: { category_ids: selected_categories } | ||
40 | + } | ||
41 | + } | ||
42 | + end | ||
43 | + | ||
35 | end | 44 | end |
36 | 45 | ||
37 | module InstanceMethods | 46 | module InstanceMethods |
@@ -0,0 +1,27 @@ | @@ -0,0 +1,27 @@ | ||
1 | +var main = function() { | ||
2 | + var categories = [] | ||
3 | + var categoryParam = ""; | ||
4 | + var url = window.location.href; | ||
5 | + var indexOfCategories; | ||
6 | + | ||
7 | + url = url.replace(/&categories.*/g, ""); | ||
8 | + url += "&categories="; | ||
9 | + | ||
10 | + $(".categories ul li input[checked]").map(function(idx, element) { | ||
11 | + categories.push(element.value); | ||
12 | + }); | ||
13 | + | ||
14 | + $('.categories ul li input[type=checkbox]').on('click', function(){ | ||
15 | + var idx = categories.indexOf(this.value); | ||
16 | + if (idx == -1) { | ||
17 | + categories.push(this.value); | ||
18 | + } else { | ||
19 | + categories.splice(idx, 1); | ||
20 | + } | ||
21 | + | ||
22 | + url += categories.join(","); | ||
23 | + window.location.href = url; | ||
24 | + }); | ||
25 | +}; | ||
26 | + | ||
27 | +$(document).ready(main); |
plugins/elasticsearch/public/style.css
@@ -50,6 +50,7 @@ | @@ -50,6 +50,7 @@ | ||
50 | font-size: 18px; | 50 | font-size: 18px; |
51 | } | 51 | } |
52 | 52 | ||
53 | +.controller-elasticsearch_plugin #content-inner .categories, | ||
53 | .controller-elasticsearch_plugin #content-inner .search-filter, | 54 | .controller-elasticsearch_plugin #content-inner .search-filter, |
54 | .controller-elasticsearch_plugin #content-inner ul.search-options { | 55 | .controller-elasticsearch_plugin #content-inner ul.search-options { |
55 | background: #fff; | 56 | background: #fff; |
@@ -145,3 +146,19 @@ | @@ -145,3 +146,19 @@ | ||
145 | text-decoration: none; | 146 | text-decoration: none; |
146 | } | 147 | } |
147 | 148 | ||
149 | +.controller-elasticsearch_plugin #content-inner .categories .category-ident { | ||
150 | + margin-left: 10px; | ||
151 | +} | ||
152 | + | ||
153 | +.controller-elasticsearch_plugin #content-inner .categories .box-title { | ||
154 | + margin-bottom: 20px; | ||
155 | +} | ||
156 | + | ||
157 | +.controller-elasticsearch_plugin #content-inner .categories ul li { | ||
158 | + padding: 5px; | ||
159 | + font-size: 15px; | ||
160 | +} | ||
161 | + | ||
162 | +.controller-elasticsearch_plugin #content-inner .categories li input { | ||
163 | + margin-right: 5px; | ||
164 | +} |
plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb
@@ -27,7 +27,7 @@ | @@ -27,7 +27,7 @@ | ||
27 | <ul class="search-options"> | 27 | <ul class="search-options"> |
28 | <% for type,value in @searchable_types %> | 28 | <% for type,value in @searchable_types %> |
29 | <li class="select-search-type <%= "active" if type == @selected_type %>"> | 29 | <li class="select-search-type <%= "active" if type == @selected_type %>"> |
30 | - <%= link_to value[:label], "?selected_type=#{type}&query=#{@query}"%> | 30 | + <%= link_to value[:label], "?selected_type=#{type}&query=#{@query}&categories=#{@selected_categories.join(',')}"%> |
31 | </li> | 31 | </li> |
32 | <% end %> | 32 | <% end %> |
33 | </ul> | 33 | </ul> |
@@ -36,11 +36,16 @@ | @@ -36,11 +36,16 @@ | ||
36 | <ul> | 36 | <ul> |
37 | <% for type, value in @sort_types %> | 37 | <% for type, value in @sort_types %> |
38 | <li class="select-search-type <%= "active" if type == @selected_sort %>"> | 38 | <li class="select-search-type <%= "active" if type == @selected_sort %>"> |
39 | - <%= link_to value[:label], "?selected_type=#{@selected_type}&query=#{@query}&filter=#{type}" %> | 39 | + <%= link_to value[:label], "?selected_type=#{@selected_type}&query=#{@query}&filter=#{type}&categories=#{@selected_categories.join(',')}" %> |
40 | </li> | 40 | </li> |
41 | <% end %> | 41 | <% end %> |
42 | </ul> | 42 | </ul> |
43 | </div> | 43 | </div> |
44 | + | ||
45 | + <div class="categories"> | ||
46 | + <h3 class="box-title"><%= _("Categories") %></h3> | ||
47 | + <%= render_categories(@categories, @selected_categories) %> | ||
48 | + </div> | ||
44 | </div> | 49 | </div> |
45 | 50 | ||
46 | <div class="results"> | 51 | <div class="results"> |
@@ -59,5 +64,6 @@ | @@ -59,5 +64,6 @@ | ||
59 | <%= pagination_links @results if @results.count > 0 %> | 64 | <%= pagination_links @results if @results.count > 0 %> |
60 | </div> | 65 | </div> |
61 | </div> | 66 | </div> |
67 | + | ||
62 | </div> | 68 | </div> |
63 | </div> | 69 | </div> |