Commit 9adc49471eb8568215ea950b94c0c3830d2633af
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>
Showing
10 changed files
with
36 additions
and
19 deletions
Show diff stats
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
@@ -27,12 +27,6 @@ class ElasticsearchPluginController < ApplicationController | @@ -27,12 +27,6 @@ class ElasticsearchPluginController < ApplicationController | ||
27 | 27 | ||
28 | def define_search_fields_types | 28 | def define_search_fields_types |
29 | @search_filter_types = ElasticsearchHelper::search_filters | 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 | end | 31 | end |
32 | - | ||
33 | - private | ||
34 | - | ||
35 | -# def permit_params | ||
36 | -# params.require(:per_page, :query) | ||
37 | -# end | ||
38 | end | 32 | end |
plugins/elasticsearch/helpers/elasticsearch_helper.rb
@@ -33,6 +33,15 @@ module ElasticsearchHelper | @@ -33,6 +33,15 @@ module ElasticsearchHelper | ||
33 | fields.to_a | 33 | fields.to_a |
34 | end | 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 | def process_results | 45 | def process_results |
37 | selected_type = (params[:selected_type]|| :all).to_sym | 46 | selected_type = (params[:selected_type]|| :all).to_sym |
38 | if selected_type == :all | 47 | if selected_type == :all |
@@ -43,7 +52,7 @@ module ElasticsearchHelper | @@ -43,7 +52,7 @@ module ElasticsearchHelper | ||
43 | end | 52 | end |
44 | 53 | ||
45 | def search_from_all_models | 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 | models = searchable_models | 56 | models = searchable_models |
48 | Elasticsearch::Model.search(query, models, size: default_per_page(params[:per_page])).page(params[:page]).records | 57 | Elasticsearch::Model.search(query, models, size: default_per_page(params[:per_page])).page(params[:page]).records |
49 | end | 58 | end |
@@ -51,7 +60,8 @@ module ElasticsearchHelper | @@ -51,7 +60,8 @@ module ElasticsearchHelper | ||
51 | def search_from_model model | 60 | def search_from_model model |
52 | begin | 61 | begin |
53 | klass = model.to_s.classify.constantize | 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 | klass.search(query, size: default_per_page(params[:per_page])).page(params[:page]).records | 65 | klass.search(query, size: default_per_page(params[:per_page])).page(params[:page]).records |
56 | rescue | 66 | rescue |
57 | [] | 67 | [] |
@@ -63,7 +73,7 @@ module ElasticsearchHelper | @@ -63,7 +73,7 @@ module ElasticsearchHelper | ||
63 | end | 73 | end |
64 | 74 | ||
65 | private | 75 | private |
66 | - | 76 | + |
67 | def searchable_models | 77 | def searchable_models |
68 | ElasticsearchHelper::searchable_types.except(:all).keys.map { | model | model.to_s.classify.constantize } | 78 | ElasticsearchHelper::searchable_types.except(:all).keys.map { | model | model.to_s.classify.constantize } |
69 | end | 79 | end |
@@ -87,12 +97,17 @@ module ElasticsearchHelper | @@ -87,12 +97,17 @@ module ElasticsearchHelper | ||
87 | query_exp | 97 | query_exp |
88 | end | 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 | fields = klass.nil? ? (fields_from_models searchable_models) : (fields_from_models [klass]) | 104 | fields = klass.nil? ? (fields_from_models searchable_models) : (fields_from_models [klass]) |
92 | query = { | 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 | query | 111 | query |
97 | end | 112 | end |
98 | 113 |
plugins/elasticsearch/lib/ext/community.rb
@@ -3,7 +3,9 @@ require_relative '../elasticsearch_indexed_model' | @@ -3,7 +3,9 @@ require_relative '../elasticsearch_indexed_model' | ||
3 | 3 | ||
4 | class Community | 4 | class Community |
5 | def self.control_fields | 5 | def self.control_fields |
6 | - {} | 6 | + { |
7 | + :created_at => {type: 'date'} | ||
8 | + } | ||
7 | end | 9 | end |
8 | include ElasticsearchIndexedModel | 10 | include ElasticsearchIndexedModel |
9 | end | 11 | end |
plugins/elasticsearch/lib/ext/event.rb
plugins/elasticsearch/lib/ext/person.rb
@@ -6,6 +6,7 @@ class Person | @@ -6,6 +6,7 @@ class Person | ||
6 | { | 6 | { |
7 | :visible => {type: 'boolean'}, | 7 | :visible => {type: 'boolean'}, |
8 | :public_profile => {type: 'boolean'}, | 8 | :public_profile => {type: 'boolean'}, |
9 | + :created_at => {type: 'date'} | ||
9 | } | 10 | } |
10 | end | 11 | end |
11 | include ElasticsearchIndexedModel | 12 | include ElasticsearchIndexedModel |
plugins/elasticsearch/lib/ext/text_article.rb
@@ -5,7 +5,8 @@ class TextArticle | @@ -5,7 +5,8 @@ class TextArticle | ||
5 | def self.control_fields | 5 | def self.control_fields |
6 | { | 6 | { |
7 | :advertise => {}, | 7 | :advertise => {}, |
8 | - :published => {} | 8 | + :published => {}, |
9 | + :created_at => {type: 'date'} | ||
9 | } | 10 | } |
10 | end | 11 | end |
11 | include ElasticsearchIndexedModel | 12 | include ElasticsearchIndexedModel |
plugins/elasticsearch/lib/ext/uploaded_file.rb
plugins/elasticsearch/test/unit/controllers/elasticsearch_plugin_controller_test.rb
@@ -31,7 +31,6 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase | @@ -31,7 +31,6 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase | ||
31 | assert_not_nil assigns(:searchable_types) | 31 | assert_not_nil assigns(:searchable_types) |
32 | assert_not_nil assigns(:selected_type) | 32 | assert_not_nil assigns(:selected_type) |
33 | assert_not_nil assigns(:search_filter_types) | 33 | assert_not_nil assigns(:search_filter_types) |
34 | - assert_not_nil assigns(:selected_filter_field) | ||
35 | end | 34 | end |
36 | 35 | ||
37 | should 'return 10 results if selected_type is nil and query is nil' do | 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,6 +7,7 @@ | ||
7 | <div class="body"> | 7 | <div class="body"> |
8 | <h2><%= link_to person.name, person.url %></h2> | 8 | <h2><%= link_to person.name, person.url %></h2> |
9 | <p><%= person.description %></p> | 9 | <p><%= person.description %></p> |
10 | + <%= person.created_at %> | ||
10 | </div> | 11 | </div> |
11 | </div> | 12 | </div> |
12 | </div> | 13 | </div> |
plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb
@@ -13,8 +13,8 @@ | @@ -13,8 +13,8 @@ | ||
13 | <div class="search-filter"> | 13 | <div class="search-filter"> |
14 | <h3 class="box-title"><%= _("Sort by") %></h3> | 14 | <h3 class="box-title"><%= _("Sort by") %></h3> |
15 | <ul> | 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 | <% end %> | 18 | <% end %> |
19 | </ul> | 19 | </ul> |
20 | </div> | 20 | </div> |
@@ -24,7 +24,9 @@ | @@ -24,7 +24,9 @@ | ||
24 | <div class="search_field"> | 24 | <div class="search_field"> |
25 | <%= form_tag '/plugin/elasticsearch/search', method: :get do %> | 25 | <%= form_tag '/plugin/elasticsearch/search', method: :get do %> |
26 | <%= hidden_field_tag "selected_type", @selected_type %> | 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 | <%= text_field_tag :query, @query %> | 30 | <%= text_field_tag :query, @query %> |
29 | <%= submit_tag _("Send") %> | 31 | <%= submit_tag _("Send") %> |
30 | <% end %> | 32 | <% end %> |