Commit 610a29db18334276b2edc859914dcafe061a9136

Authored by Macartur Sousa
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>
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
@@ -21,33 +21,34 @@ class ElasticsearchPluginController &lt; ApplicationController @@ -21,33 +21,34 @@ class ElasticsearchPluginController &lt; 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 - 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 query = { 52 query = {
52 query: { 53 query: {
53 match_all: { 54 match_all: {
@@ -74,14 +75,23 @@ class ElasticsearchPluginController &lt; ApplicationController @@ -74,14 +75,23 @@ class ElasticsearchPluginController &lt; ApplicationController
74 query 75 query
75 end 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 begin 88 begin
79 klass = model.to_s.classify.constantize 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 rescue 92 rescue
81 - return 93 + []
82 end 94 end
83 - query = get_query params[:q], klass  
84 - @results |= klass.__elasticsearch__.search(query).records.to_a  
85 end 95 end
86 96
87 def define_searchable_types 97 def define_searchable_types
@@ -94,4 +104,8 @@ class ElasticsearchPluginController &lt; ApplicationController @@ -94,4 +104,8 @@ class ElasticsearchPluginController &lt; ApplicationController
94 @selected_filter_field = params[:selected_filter_field].nil? ? SEARCH_FILTERS.keys.first : params[:selected_filter_field].to_sym 104 @selected_filter_field = params[:selected_filter_field].nil? ? SEARCH_FILTERS.keys.first : params[:selected_filter_field].to_sym
95 end 105 end
96 106
  107 + def default_per_page
  108 + 10
  109 + end
  110 +
97 end 111 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>