Commit 9adc49471eb8568215ea950b94c0c3830d2633af

Authored by Dylan Guedes
Committed by Macartur Sousa
1 parent d8826231
Exists in elasticsearch_sort

Adds created at as a type of sort

Signed-off-by: DylanGuedes <djmgguedes@gmail.com>
Signed-off-by: Arthur Jahn <stutrzbecher@gmail.com>
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
... ... @@ -27,12 +27,6 @@ class ElasticsearchPluginController &lt; ApplicationController
27 27  
28 28 def define_search_fields_types
29 29 @search_filter_types = ElasticsearchHelper::search_filters
30   - @selected_filter_field = (params[:selected_filter_field] || ElasticsearchHelper::search_filters.keys.first).to_sym
  30 + @selected_filter_field = (params[:selected_filter_field])
31 31 end
32   -
33   - private
34   -
35   -# def permit_params
36   -# params.require(:per_page, :query)
37   -# end
38 32 end
... ...
plugins/elasticsearch/helpers/elasticsearch_helper.rb
... ... @@ -33,6 +33,15 @@ module ElasticsearchHelper
33 33 fields.to_a
34 34 end
35 35  
  36 + def get_sort_by sort_by
  37 + case sort_by
  38 + when "lexical"
  39 + "name.raw"
  40 + when "recent"
  41 + "created_at"
  42 + end
  43 + end
  44 +
36 45 def process_results
37 46 selected_type = (params[:selected_type]|| :all).to_sym
38 47 if selected_type == :all
... ... @@ -43,7 +52,7 @@ module ElasticsearchHelper
43 52 end
44 53  
45 54 def search_from_all_models
46   - query = get_query params[:query]
  55 + query = get_query params[:query], sort_by: get_sort_by(params[:selected_filter_field])
47 56 models = searchable_models
48 57 Elasticsearch::Model.search(query, models, size: default_per_page(params[:per_page])).page(params[:page]).records
49 58 end
... ... @@ -51,7 +60,8 @@ module ElasticsearchHelper
51 60 def search_from_model model
52 61 begin
53 62 klass = model.to_s.classify.constantize
54   - query = get_query params[:query], klass
  63 +
  64 + query = get_query params[:query], klass: klass, sort_by: get_sort_by(params[:selected_filter_field])
55 65 klass.search(query, size: default_per_page(params[:per_page])).page(params[:page]).records
56 66 rescue
57 67 []
... ... @@ -63,7 +73,7 @@ module ElasticsearchHelper
63 73 end
64 74  
65 75 private
66   -
  76 +
67 77 def searchable_models
68 78 ElasticsearchHelper::searchable_types.except(:all).keys.map { | model | model.to_s.classify.constantize }
69 79 end
... ... @@ -87,12 +97,17 @@ module ElasticsearchHelper
87 97 query_exp
88 98 end
89 99  
90   - def get_query text="", klass=nil
  100 + def get_query text="", options={}
  101 + klass = options[:klass]
  102 + sort_by = options[:sort_by]
  103 +
91 104 fields = klass.nil? ? (fields_from_models searchable_models) : (fields_from_models [klass])
92 105 query = {
93   - query: query_method(text, fields),
94   - sort: "name.raw"
  106 + query: query_method(text, fields)
95 107 }
  108 + if sort_by
  109 + query[:sort] = sort_by
  110 + end
96 111 query
97 112 end
98 113  
... ...
plugins/elasticsearch/lib/ext/community.rb
... ... @@ -3,7 +3,9 @@ require_relative &#39;../elasticsearch_indexed_model&#39;
3 3  
4 4 class Community
5 5 def self.control_fields
6   - {}
  6 + {
  7 + :created_at => {type: 'date'}
  8 + }
7 9 end
8 10 include ElasticsearchIndexedModel
9 11 end
... ...
plugins/elasticsearch/lib/ext/event.rb
... ... @@ -6,6 +6,7 @@ class Event
6 6 {
7 7 :advertise => {},
8 8 :published => {},
  9 + :created_at => {type: 'date'}
9 10 }
10 11 end
11 12 include ElasticsearchIndexedModel
... ...
plugins/elasticsearch/lib/ext/person.rb
... ... @@ -6,6 +6,7 @@ class Person
6 6 {
7 7 :visible => {type: 'boolean'},
8 8 :public_profile => {type: 'boolean'},
  9 + :created_at => {type: 'date'}
9 10 }
10 11 end
11 12 include ElasticsearchIndexedModel
... ...
plugins/elasticsearch/lib/ext/text_article.rb
... ... @@ -5,7 +5,8 @@ class TextArticle
5 5 def self.control_fields
6 6 {
7 7 :advertise => {},
8   - :published => {}
  8 + :published => {},
  9 + :created_at => {type: 'date'}
9 10 }
10 11 end
11 12 include ElasticsearchIndexedModel
... ...
plugins/elasticsearch/lib/ext/uploaded_file.rb
... ... @@ -6,6 +6,7 @@ class UploadedFile
6 6 {
7 7 :advertise => {},
8 8 :published => {},
  9 + :created_at => {type: 'date'}
9 10 }
10 11 end
11 12 include ElasticsearchIndexedModel
... ...
plugins/elasticsearch/test/unit/controllers/elasticsearch_plugin_controller_test.rb
... ... @@ -31,7 +31,6 @@ class ElasticsearchPluginControllerTest &lt; ActionController::TestCase
31 31 assert_not_nil assigns(:searchable_types)
32 32 assert_not_nil assigns(:selected_type)
33 33 assert_not_nil assigns(:search_filter_types)
34   - assert_not_nil assigns(:selected_filter_field)
35 34 end
36 35  
37 36 should 'return 10 results if selected_type is nil and query is nil' do
... ...
plugins/elasticsearch/views/elasticsearch_plugin/_person_display.html.erb
... ... @@ -7,6 +7,7 @@
7 7 <div class="body">
8 8 <h2><%= link_to person.name, person.url %></h2>
9 9 <p><%= person.description %></p>
  10 + <%= person.created_at %>
10 11 </div>
11 12 </div>
12 13 </div>
... ...
plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb
... ... @@ -13,8 +13,8 @@
13 13 <div class="search-filter">
14 14 <h3 class="box-title"><%= _("Sort by") %></h3>
15 15 <ul>
16   - <% for type in @search_filter_types.values %>
17   - <li><a href="#"><%= type[:label] %></a></li>
  16 + <% for type, value in @search_filter_types %>
  17 + <li><%= link_to value[:label], "?selected_type=#{@selected_type}&query=#{@query}&selected_filter_field=#{type}" %></li>
18 18 <% end %>
19 19 </ul>
20 20 </div>
... ... @@ -24,7 +24,9 @@
24 24 <div class="search_field">
25 25 <%= form_tag '/plugin/elasticsearch/search', method: :get do %>
26 26 <%= hidden_field_tag "selected_type", @selected_type %>
27   - <%= hidden_field_tag "selected_filter_field", @selected_filter_field %>
  27 + <% if @selected_filter_field %>
  28 + <%= hidden_field_tag "selected_filter_field", @selected_filter_field %>
  29 + <% end %>
28 30 <%= text_field_tag :query, @query %>
29 31 <%= submit_tag _("Send") %>
30 32 <% end %>
... ...