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,33 +21,35 @@ class ElasticsearchPluginController < ApplicationController | ||
21 | define_searchable_types | 21 | define_searchable_types |
22 | define_search_fields_types | 22 | define_search_fields_types |
23 | 23 | ||
24 | + process_results | ||
25 | + end | ||
26 | + | ||
27 | + def process_results | ||
24 | @query = params[:q] | 28 | @query = params[:q] |
25 | - @results = [] | ||
26 | 29 | ||
27 | if @selected_type == :all | 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 | else | 32 | else |
32 | - results @selected_type | 33 | + @results = search_from_model @selected_type |
33 | end | 34 | end |
34 | end | 35 | end |
35 | 36 | ||
36 | - | ||
37 | private | 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 | query = {} | 50 | query = {} |
41 | unless text.blank? | 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 | query = { | 53 | query = { |
52 | query: { | 54 | query: { |
53 | match_all: { | 55 | match_all: { |
@@ -74,14 +76,23 @@ class ElasticsearchPluginController < ApplicationController | @@ -74,14 +76,23 @@ class ElasticsearchPluginController < ApplicationController | ||
74 | query | 76 | query |
75 | end | 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 | begin | 89 | begin |
79 | klass = model.to_s.classify.constantize | 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 | rescue | 93 | rescue |
81 | - return | 94 | + [] |
82 | end | 95 | end |
83 | - query = get_query params[:q], klass | ||
84 | - @results |= klass.__elasticsearch__.search(query).records.to_a | ||
85 | end | 96 | end |
86 | 97 | ||
87 | def define_searchable_types | 98 | def define_searchable_types |
@@ -94,4 +105,8 @@ class ElasticsearchPluginController < ApplicationController | @@ -94,4 +105,8 @@ class ElasticsearchPluginController < ApplicationController | ||
94 | @selected_filter_field = params[:selected_filter_field].nil? ? SEARCH_FILTERS.keys.first : params[:selected_filter_field].to_sym | 105 | @selected_filter_field = params[:selected_filter_field].nil? ? SEARCH_FILTERS.keys.first : params[:selected_filter_field].to_sym |
95 | end | 106 | end |
96 | 107 | ||
108 | + def default_per_page | ||
109 | + 10 | ||
110 | + end | ||
111 | + | ||
97 | end | 112 | end |
plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb
@@ -2,7 +2,7 @@ | @@ -2,7 +2,7 @@ | ||
2 | 2 | ||
3 | <div class="sidebar"> | 3 | <div class="sidebar"> |
4 | <div class="results-count"> | 4 | <div class="results-count"> |
5 | - <%= @results.count %> <%= _(" results by") %> : | 5 | + <%= @results.total %> <%= _(" results by") %> : |
6 | </div> | 6 | </div> |
7 | <ul> | 7 | <ul> |
8 | <% for type,value in @searchable_types %> | 8 | <% for type,value in @searchable_types %> |
@@ -24,16 +24,16 @@ | @@ -24,16 +24,16 @@ | ||
24 | 24 | ||
25 | <div class="search_form"> | 25 | <div class="search_form"> |
26 | <div class="search_field"> | 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 | <%= submit_tag _("Send") %> | 31 | <%= submit_tag _("Send") %> |
32 | <% end %> | 32 | <% end %> |
33 | </div> | 33 | </div> |
34 | 34 | ||
35 | <div class="results"> | 35 | <div class="results"> |
36 | - <% for result in @results %> | 36 | + <% for result in @results.to_a %> |
37 | <% for klass in @searchable_types.keys %> | 37 | <% for klass in @searchable_types.keys %> |
38 | <% next if klass.to_s.include? "all" %> | 38 | <% next if klass.to_s.include? "all" %> |
39 | <% if result.is_a? klass.to_s.classify.constantize %> | 39 | <% if result.is_a? klass.to_s.classify.constantize %> |
@@ -44,6 +44,9 @@ | @@ -44,6 +44,9 @@ | ||
44 | <% end %> | 44 | <% end %> |
45 | <% end %> | 45 | <% end %> |
46 | <% end %> | 46 | <% end %> |
47 | + <div class="search_paginate"> | ||
48 | + <%= pagination_links @results %> | ||
49 | + </div> | ||
47 | </div> | 50 | </div> |
48 | </div> | 51 | </div> |
49 | </div> | 52 | </div> |