Commit 5752a74d232254a8ee50a5eced188b519c575c01
1 parent
88a27663
Exists in
elasticsearch_api
Applyng filtering by type on articles
Signed-off-by: Daniel Henrique <danielhmarinho@gmail.com> Signed-off-by: Luciano Prestes <lucianopcbr@gmail.com>
Showing
2 changed files
with
30 additions
and
8 deletions
Show diff stats
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
1 | 1 | class ElasticsearchPluginController < ApplicationController |
2 | - | |
3 | 2 | no_design_blocks |
4 | - | |
3 | + | |
5 | 4 | def search |
6 | 5 | @results = [] |
7 | 6 | @query = params[:q] |
8 | 7 | @checkbox = {} |
8 | + terms = get_terms(params) | |
9 | + puts "=" * 80, terms, "=" * 80 | |
9 | 10 | if params[:model].present? |
10 | - params[:model].keys.each do |model| | |
11 | - @checkbox[model.to_sym] = true | |
12 | - klass = model.classify.constantize | |
13 | - query = get_query params[:q], klass | |
14 | - @results += klass.__elasticsearch__.search(query).records.to_a | |
15 | - end | |
11 | + params[:model].keys.each do |model| | |
12 | + @checkbox[model.to_sym] = true | |
13 | + klass = model.classify.constantize | |
14 | + query = get_query params[:q], klass | |
15 | + @results += klass.__elasticsearch__.search(query).records.to_a | |
16 | + end | |
16 | 17 | end |
17 | 18 | |
19 | + if params[:filter].present? | |
20 | + params[:filter].keys.each do |model| | |
21 | + params[:filter][model].keys.each do |filter| | |
22 | + @checkbox[filter.to_sym] = true | |
23 | + end | |
24 | + end | |
25 | + end | |
18 | 26 | end |
19 | 27 | |
20 | 28 | private |
... | ... | @@ -37,4 +45,16 @@ class ElasticsearchPluginController < ApplicationController |
37 | 45 | query |
38 | 46 | end |
39 | 47 | |
48 | + def get_terms params | |
49 | + terms = {} | |
50 | + return terms unless params[:filter].present? | |
51 | + params[:filter].keys.each do |model| | |
52 | + terms[model] = {} | |
53 | + params[:filter][model].keys.each do |filter| | |
54 | + @checkbox[filter.to_sym] = true | |
55 | + terms[model][params[:filter][model.to_sym][filter]] = filter | |
56 | + end | |
57 | + end | |
58 | + terms | |
59 | + end | |
40 | 60 | end | ... | ... |
plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb
... | ... | @@ -15,6 +15,8 @@ |
15 | 15 | <%= check_box_tag 'model[articles]', 1, @checkbox[:articles] %> |
16 | 16 | <%= label_tag('articles', _("articles")) %> |
17 | 17 | |
18 | + <%= check_box_tag 'filter[articles][gallery]', :type, @checkbox[:gallery] %> | |
19 | + <%= label_tag('gallery', _("gallery")) %> | |
18 | 20 | <% end %> |
19 | 21 | |
20 | 22 | <% for result in @results %> | ... | ... |