Commit 3d9dadd362d81d6ae506d577fbe6f264164bcc46
1 parent
2e9a3f44
Exists in
elasticsearch_view
Refactored search controller and view
- Changed search-form to use get instead of post - Uses search method directly in models - Refactored process results - Adding pagination Signed-off-by: DylanGuedes <djmgguedes@gmail.com> Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
Showing
2 changed files
with
43 additions
and
25 deletions
Show diff stats
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
... | ... | @@ -21,33 +21,35 @@ class ElasticsearchPluginController < ApplicationController |
21 | 21 | define_searchable_types |
22 | 22 | define_search_fields_types |
23 | 23 | |
24 | + process_results | |
25 | + end | |
26 | + | |
27 | + def process_results | |
24 | 28 | @query = params[:q] |
25 | - @results = [] | |
26 | 29 | |
27 | 30 | if @selected_type == :all |
28 | - SEARCHABLE_TYPES.keys.each do |type| | |
29 | - results type.to_s | |
30 | - end | |
31 | + @results = search_from_all_models | |
31 | 32 | else |
32 | - results @selected_type | |
33 | + @results = search_from_model @selected_type | |
33 | 34 | end |
34 | 35 | end |
35 | 36 | |
36 | - | |
37 | 37 | private |
38 | 38 | |
39 | - def get_query text, klass | |
39 | + def fields_from_model | |
40 | + klass::SEARCHABLE_FIELDS.map do |key, value| | |
41 | + if value[:weight] | |
42 | + "#{key}^#{value[:weight]}" | |
43 | + else | |
44 | + "#{key}" | |
45 | + end | |
46 | + end | |
47 | + end | |
48 | + | |
49 | + def get_query text, klass=nil | |
40 | 50 | query = {} |
41 | 51 | unless text.blank? |
42 | 52 | |
43 | - fields = klass::SEARCHABLE_FIELDS.map do |key, value| | |
44 | - if value[:weight] | |
45 | - "#{key}^#{value[:weight]}" | |
46 | - else | |
47 | - "#{key}" | |
48 | - end | |
49 | - end | |
50 | - | |
51 | 53 | query = { |
52 | 54 | query: { |
53 | 55 | match_all: { |
... | ... | @@ -74,14 +76,23 @@ class ElasticsearchPluginController < ApplicationController |
74 | 76 | query |
75 | 77 | end |
76 | 78 | |
77 | - def results model | |
79 | + | |
80 | + def search_from_all_models | |
81 | + models = [] | |
82 | + query = get_query params[:q] | |
83 | + | |
84 | + SEARCHABLE_TYPES.keys.each {| model | models.append( model.to_s.classify.constantize) if model != :all } | |
85 | + Elasticsearch::Model.search(query, models, size: default_per_page).page(params[:page]).records | |
86 | + end | |
87 | + | |
88 | + def search_from_model model | |
78 | 89 | begin |
79 | 90 | klass = model.to_s.classify.constantize |
91 | + query = get_query params[:q], klass | |
92 | + klass.search(query, size: default_per_page).page(params[:page]).records | |
80 | 93 | rescue |
81 | - return | |
94 | + [] | |
82 | 95 | end |
83 | - query = get_query params[:q], klass | |
84 | - @results |= klass.__elasticsearch__.search(query).records.to_a | |
85 | 96 | end |
86 | 97 | |
87 | 98 | def define_searchable_types |
... | ... | @@ -94,4 +105,8 @@ class ElasticsearchPluginController < ApplicationController |
94 | 105 | @selected_filter_field = params[:selected_filter_field].nil? ? SEARCH_FILTERS.keys.first : params[:selected_filter_field].to_sym |
95 | 106 | end |
96 | 107 | |
108 | + def default_per_page | |
109 | + 10 | |
110 | + end | |
111 | + | |
97 | 112 | end | ... | ... |
plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb
... | ... | @@ -2,7 +2,7 @@ |
2 | 2 | |
3 | 3 | <div class="sidebar"> |
4 | 4 | <div class="results-count"> |
5 | - <%= @results.count %> <%= _(" results by") %> : | |
5 | + <%= @results.total %> <%= _(" results by") %> : | |
6 | 6 | </div> |
7 | 7 | <ul> |
8 | 8 | <% for type,value in @searchable_types %> |
... | ... | @@ -24,16 +24,16 @@ |
24 | 24 | |
25 | 25 | <div class="search_form"> |
26 | 26 | <div class="search_field"> |
27 | - <%= form_tag controller: "elasticsearch_plugin", action: "search" do %> | |
28 | - <%= text_field_tag(:q, @query) %> | |
29 | - <%= hidden_field_tag 'selected_type', @selected_type %> | |
30 | - <%= hidden_field_tag 'selected_filter_field', @selected_filter_field %> | |
27 | + <%= form_tag '/plugin/elasticsearch/search', method: :get do %> | |
28 | + <%= hidden_field_tag "selected_type", @selected_type %> | |
29 | + <%= hidden_field_tag "selected_filter_field", @selected_filter_field %> | |
30 | + <%= text_field_tag :q, @query %> | |
31 | 31 | <%= submit_tag _("Send") %> |
32 | 32 | <% end %> |
33 | 33 | </div> |
34 | 34 | |
35 | 35 | <div class="results"> |
36 | - <% for result in @results %> | |
36 | + <% for result in @results.to_a %> | |
37 | 37 | <% for klass in @searchable_types.keys %> |
38 | 38 | <% next if klass.to_s.include? "all" %> |
39 | 39 | <% if result.is_a? klass.to_s.classify.constantize %> |
... | ... | @@ -44,6 +44,9 @@ |
44 | 44 | <% end %> |
45 | 45 | <% end %> |
46 | 46 | <% end %> |
47 | + <div class="search_paginate"> | |
48 | + <%= pagination_links @results %> | |
49 | + </div> | |
47 | 50 | </div> |
48 | 51 | </div> |
49 | 52 | </div> | ... | ... |