Commit b51e95f0135cbeb18f86acb8fe817f7473742dd7
1 parent
e4628511
Exists in
staging
and in
6 other branches
Elasticsearch: refacted sort by
* Changed name from helpers to display sort_types * Adding dynamic sort to text_article * indexed category_ids to be used after Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
Showing
8 changed files
with
44 additions
and
25 deletions
Show diff stats
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
... | ... | @@ -11,7 +11,7 @@ class ElasticsearchPluginController < ApplicationController |
11 | 11 | |
12 | 12 | def search |
13 | 13 | define_searchable_types |
14 | - define_search_fields_types | |
14 | + define_sort_types | |
15 | 15 | define_results |
16 | 16 | end |
17 | 17 | |
... | ... | @@ -26,9 +26,9 @@ class ElasticsearchPluginController < ApplicationController |
26 | 26 | @selected_type = (params[:selected_type]|| :all ).to_sym |
27 | 27 | end |
28 | 28 | |
29 | - def define_search_fields_types | |
30 | - @filter_types = filters | |
31 | - @selected_filter = (params[:filter] || :relevance).to_sym | |
29 | + def define_sort_types | |
30 | + @sort_types = sort_types | |
31 | + @selected_sort = (params[:filter] || :relevance).to_sym | |
32 | 32 | end |
33 | 33 | |
34 | 34 | end | ... | ... |
plugins/elasticsearch/helpers/elasticsearch_helper.rb
... | ... | @@ -11,8 +11,8 @@ module ElasticsearchHelper |
11 | 11 | } |
12 | 12 | end |
13 | 13 | |
14 | - def filters | |
15 | - filters = { | |
14 | + def sort_types | |
15 | + sorts = { | |
16 | 16 | :relevance => { label: _("Relevance")}, |
17 | 17 | :lexical => { label: _("Alphabetical")}, |
18 | 18 | :more_recent => { label: _("More Recent")}, |
... | ... | @@ -20,11 +20,11 @@ module ElasticsearchHelper |
20 | 20 | |
21 | 21 | selected_type = params[:selected_type] || nil |
22 | 22 | |
23 | - if selected_type | |
23 | + if selected_type and selected_type.to_sym != :all | |
24 | 24 | klass = selected_type.to_s.classify.constantize |
25 | - filters.update klass.especific_filter if klass.respond_to? :especific_filter | |
25 | + sorts.update klass.especific_sort if klass.respond_to? :especific_sort | |
26 | 26 | end |
27 | - filters | |
27 | + sorts | |
28 | 28 | end |
29 | 29 | |
30 | 30 | def process_results | ... | ... |
plugins/elasticsearch/helpers/searchable_model/elasticsearch_indexed_model.rb
... | ... | @@ -66,8 +66,9 @@ module ElasticsearchIndexedModel |
66 | 66 | |
67 | 67 | def indexed_fields |
68 | 68 | fields = { |
69 | - :environment => {type: :nested, hash: NestedEnvironment::environment_hash }, | |
70 | - :created_at => {type: :date } | |
69 | + :environment => {type: :nested, hash: NestedEnvironment::environment_hash }, | |
70 | + :category_ids => {type: :integer }, | |
71 | + :created_at => {type: :date } | |
71 | 72 | } |
72 | 73 | fields.update(self::SEARCHABLE_FIELDS) |
73 | 74 | fields.update(self.control_fields) | ... | ... |
plugins/elasticsearch/lib/elasticsearch_plugin/api.rb
... | ... | @@ -18,8 +18,8 @@ class ElasticsearchPlugin::API < Grape::API |
18 | 18 | present types, with: Grape::Presenters::Presenter |
19 | 19 | end |
20 | 20 | |
21 | - get 'filters' do | |
22 | - present filters, with: Grape::Presenters::Presenter | |
21 | + get 'sort_types' do | |
22 | + present sort_types, with: Grape::Presenters::Presenter | |
23 | 23 | end |
24 | 24 | |
25 | 25 | end | ... | ... |
plugins/elasticsearch/lib/ext/community.rb
plugins/elasticsearch/lib/ext/person.rb
plugins/elasticsearch/lib/ext/text_article.rb
... | ... | @@ -10,9 +10,11 @@ class TextArticle |
10 | 10 | |
11 | 11 | def self.control_fields |
12 | 12 | { |
13 | - :advertise => { type: :boolean }, | |
14 | - :published => { type: 'boolean'}, | |
15 | - :profile => { type: :nested , hash: NestedProfile.hash } | |
13 | + :advertise => { type: :boolean }, | |
14 | + :published => { type: 'boolean'}, | |
15 | + :comments_count => { type: :integer }, | |
16 | + :hits => { type: :integer }, | |
17 | + :profile => { type: :nested , hash: NestedProfile.hash } | |
16 | 18 | } |
17 | 19 | end |
18 | 20 | |
... | ... | @@ -32,5 +34,21 @@ class TextArticle |
32 | 34 | ] |
33 | 35 | end |
34 | 36 | |
37 | + def self.especific_sort | |
38 | + { | |
39 | + :more_popular => { label: _("More Viewed") }, | |
40 | + :more_comments => { label: _("More Commented") } | |
41 | + } | |
42 | + end | |
43 | + | |
44 | + def self.get_sort_by sort_by | |
45 | + case sort_by | |
46 | + when "more_popular" | |
47 | + { :hits => {order: :desc} } | |
48 | + when "more_comments" | |
49 | + { :comments_count => {order: :desc}} | |
50 | + end | |
51 | + end | |
52 | + | |
35 | 53 | include SearchableModelHelper |
36 | 54 | end | ... | ... |
plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb
... | ... | @@ -26,18 +26,18 @@ |
26 | 26 | <div class="sidebar"> |
27 | 27 | <ul class="search-options"> |
28 | 28 | <% for type,value in @searchable_types %> |
29 | - <li class="select-search-type <%= "active" if type == @selected_type %>"> | |
30 | - <%= link_to value[:label], "?selected_type=#{type}&query=#{@query}&filter=#{@selected_filter}"%> | |
31 | - </li> | |
29 | + <li class="select-search-type <%= "active" if type == @selected_type %>"> | |
30 | + <%= link_to value[:label], "?selected_type=#{type}&query=#{@query}&filter=#{@selected_sort}"%> | |
31 | + </li> | |
32 | 32 | <% end %> |
33 | 33 | </ul> |
34 | 34 | <div class="search-filter"> |
35 | 35 | <h3 class="box-title"><%= _("Sort by") %></h3> |
36 | 36 | <ul> |
37 | - <% for type, value in @filter_types %> | |
38 | - <li class="select-search-type <%= "active" if type == @selected_filter %>"> | |
39 | - <%= link_to value[:label], "?selected_type=#{@selected_type}&query=#{@query}&filter=#{type}" %> | |
40 | - </li> | |
37 | + <% for type, value in @sort_types %> | |
38 | + <li class="select-search-type <%= "active" if type == @selected_sort %>"> | |
39 | + <%= link_to value[:label], "?selected_type=#{@selected_type}&query=#{@query}&filter=#{type}" %> | |
40 | + </li> | |
41 | 41 | <% end %> |
42 | 42 | </ul> |
43 | 43 | </div> | ... | ... |