Commit 610a29db18334276b2edc859914dcafe061a9136
1 parent
dd29b575
Exists in
elasticsearch_sort
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
26 deletions
Show diff stats
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
| ... | ... | @@ -21,33 +21,34 @@ 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 | - text = text.downcase | |
| 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 | 52 | query = { |
| 52 | 53 | query: { |
| 53 | 54 | match_all: { |
| ... | ... | @@ -74,14 +75,23 @@ class ElasticsearchPluginController < ApplicationController |
| 74 | 75 | query |
| 75 | 76 | end |
| 76 | 77 | |
| 77 | - def results model | |
| 78 | + | |
| 79 | + def search_from_all_models | |
| 80 | + models = [] | |
| 81 | + query = get_query params[:q] | |
| 82 | + | |
| 83 | + SEARCHABLE_TYPES.keys.each {| model | models.append( model.to_s.classify.constantize) if model != :all } | |
| 84 | + Elasticsearch::Model.search(query, models, size: default_per_page).page(params[:page]).records | |
| 85 | + end | |
| 86 | + | |
| 87 | + def search_from_model model | |
| 78 | 88 | begin |
| 79 | 89 | klass = model.to_s.classify.constantize |
| 90 | + query = get_query params[:q], klass | |
| 91 | + klass.search(query, size: default_per_page).page(params[:page]).records | |
| 80 | 92 | rescue |
| 81 | - return | |
| 93 | + [] | |
| 82 | 94 | end |
| 83 | - query = get_query params[:q], klass | |
| 84 | - @results |= klass.__elasticsearch__.search(query).records.to_a | |
| 85 | 95 | end |
| 86 | 96 | |
| 87 | 97 | def define_searchable_types |
| ... | ... | @@ -94,4 +104,8 @@ class ElasticsearchPluginController < ApplicationController |
| 94 | 104 | @selected_filter_field = params[:selected_filter_field].nil? ? SEARCH_FILTERS.keys.first : params[:selected_filter_field].to_sym |
| 95 | 105 | end |
| 96 | 106 | |
| 107 | + def default_per_page | |
| 108 | + 10 | |
| 109 | + end | |
| 110 | + | |
| 97 | 111 | 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> | ... | ... |