Commit 5752a74d232254a8ee50a5eced188b519c575c01

Authored by Daniel Damasceno
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>
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
1 class ElasticsearchPluginController < ApplicationController 1 class ElasticsearchPluginController < ApplicationController
2 -  
3 no_design_blocks 2 no_design_blocks
4 - 3 +
5 def search 4 def search
6 @results = [] 5 @results = []
7 @query = params[:q] 6 @query = params[:q]
8 @checkbox = {} 7 @checkbox = {}
  8 + terms = get_terms(params)
  9 + puts "=" * 80, terms, "=" * 80
9 if params[:model].present? 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 end 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 end 26 end
19 27
20 private 28 private
@@ -37,4 +45,16 @@ class ElasticsearchPluginController &lt; ApplicationController @@ -37,4 +45,16 @@ class ElasticsearchPluginController &lt; ApplicationController
37 query 45 query
38 end 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 end 60 end
plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb
@@ -15,6 +15,8 @@ @@ -15,6 +15,8 @@
15 <%= check_box_tag 'model[articles]', 1, @checkbox[:articles] %> 15 <%= check_box_tag 'model[articles]', 1, @checkbox[:articles] %>
16 <%= label_tag('articles', _("articles")) %> 16 <%= label_tag('articles', _("articles")) %>
17 17
  18 + <%= check_box_tag 'filter[articles][gallery]', :type, @checkbox[:gallery] %>
  19 + <%= label_tag('gallery', _("gallery")) %>
18 <% end %> 20 <% end %>
19 21
20 <% for result in @results %> 22 <% for result in @results %>