Commit 2363d71019ffd14e77d5607b5ece34f052732e59
1 parent
7b7c6b1f
Exists in
elasticsearch_filter
and in
1 other branch
Elasticsearch: Adding dynamic sort
* Adding more_popular and more_active to Community and Person * Fixed Api to support dynamic filter Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
Showing
6 changed files
with
59 additions
and
8 deletions
Show diff stats
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
| ... | ... | @@ -22,12 +22,12 @@ class ElasticsearchPluginController < ApplicationController |
| 22 | 22 | end |
| 23 | 23 | |
| 24 | 24 | def define_searchable_types |
| 25 | - @searchable_types = ElasticsearchHelper::searchable_types | |
| 25 | + @searchable_types = searchable_types | |
| 26 | 26 | @selected_type = (params[:selected_type]|| :all ).to_sym |
| 27 | 27 | end |
| 28 | 28 | |
| 29 | 29 | def define_search_fields_types |
| 30 | - @filter_types = ElasticsearchHelper::filters | |
| 30 | + @filter_types = filters | |
| 31 | 31 | @selected_filter = (params[:filter] || :relevance).to_sym |
| 32 | 32 | end |
| 33 | 33 | ... | ... |
plugins/elasticsearch/helpers/elasticsearch_helper.rb
| 1 | 1 | module ElasticsearchHelper |
| 2 | 2 | |
| 3 | - def self.searchable_types | |
| 3 | + def searchable_types | |
| 4 | 4 | { |
| 5 | 5 | :all => { label: _("All Results")}, |
| 6 | 6 | :text_article => { label: _("Articles")}, |
| ... | ... | @@ -11,12 +11,20 @@ module ElasticsearchHelper |
| 11 | 11 | } |
| 12 | 12 | end |
| 13 | 13 | |
| 14 | - def self.filters | |
| 15 | - { | |
| 14 | + def filters | |
| 15 | + filters = { | |
| 16 | 16 | :relevance => { label: _("Relevance")}, |
| 17 | 17 | :lexical => { label: _("Alphabetical")}, |
| 18 | 18 | :more_recent => { label: _("More Recent")}, |
| 19 | 19 | } |
| 20 | + | |
| 21 | + selected_type = params[:selected_type] || nil | |
| 22 | + | |
| 23 | + if selected_type | |
| 24 | + klass = selected_type.to_s.classify.constantize | |
| 25 | + filters.update klass.especific_filter if klass.respond_to? :especific_filter | |
| 26 | + end | |
| 27 | + filters | |
| 20 | 28 | end |
| 21 | 29 | |
| 22 | 30 | def process_results |
| ... | ... | @@ -35,7 +43,7 @@ module ElasticsearchHelper |
| 35 | 43 | begin |
| 36 | 44 | klass = model.to_s.classify.constantize |
| 37 | 45 | |
| 38 | - query = get_query params[:query], klass: klass, sort_by: get_sort_by(params[:filter]) | |
| 46 | + query = get_query params[:query], klass: klass, sort_by: get_sort_by(params[:filter],klass) | |
| 39 | 47 | klass.search(query, size: default_per_page(params[:per_page])).page(params[:page]).records |
| 40 | 48 | rescue |
| 41 | 49 | [] |
| ... | ... | @@ -46,12 +54,14 @@ module ElasticsearchHelper |
| 46 | 54 | per_page ||= 10 |
| 47 | 55 | end |
| 48 | 56 | |
| 49 | - def get_sort_by sort_by | |
| 57 | + def get_sort_by sort_by, klass=nil | |
| 50 | 58 | case sort_by |
| 51 | 59 | when "lexical" |
| 52 | 60 | { "name.raw" => {"order" => "asc" }} |
| 53 | 61 | when "more_recent" |
| 54 | 62 | { "created_at" => {"order" => "desc"}} |
| 63 | + else | |
| 64 | + ( klass and klass.respond_to?(:get_sort_by) ) ? klass.get_sort_by(sort_by) : nil | |
| 55 | 65 | end |
| 56 | 66 | end |
| 57 | 67 | ... | ... |
plugins/elasticsearch/lib/elasticsearch_plugin/api.rb
| ... | ... | @@ -14,10 +14,14 @@ class ElasticsearchPlugin::API < Grape::API |
| 14 | 14 | end |
| 15 | 15 | |
| 16 | 16 | get 'types' do |
| 17 | - types = {types: ElasticsearchHelper::searchable_types.stringify_keys.keys} | |
| 17 | + types = {types: searchable_types.stringify_keys.keys } | |
| 18 | 18 | present types, with: Grape::Presenters::Presenter |
| 19 | 19 | end |
| 20 | 20 | |
| 21 | + get 'filters' do | |
| 22 | + present filters, with: Grape::Presenters::Presenter | |
| 23 | + end | |
| 24 | + | |
| 21 | 25 | end |
| 22 | 26 | |
| 23 | 27 | end | ... | ... |
plugins/elasticsearch/lib/ext/community.rb
| ... | ... | @@ -7,6 +7,8 @@ class Community |
| 7 | 7 | { |
| 8 | 8 | :secret => { type: :boolean }, |
| 9 | 9 | :visible => { type: :boolean }, |
| 10 | + :activities_count => { type: :integer }, | |
| 11 | + :members_count => { type: :integer } | |
| 10 | 12 | } |
| 11 | 13 | end |
| 12 | 14 | |
| ... | ... | @@ -21,5 +23,21 @@ class Community |
| 21 | 23 | ] |
| 22 | 24 | end |
| 23 | 25 | |
| 26 | + def self.especific_filter | |
| 27 | + { | |
| 28 | + :more_active => { label: _("More Active") }, | |
| 29 | + :more_popular => { label: _("More Popular") } | |
| 30 | + } | |
| 31 | + end | |
| 32 | + | |
| 33 | + def self.get_sort_by sort_by | |
| 34 | + case sort_by | |
| 35 | + when "more_active" | |
| 36 | + { :activities_count => {order: :desc}} | |
| 37 | + when "more_popular" | |
| 38 | + { :members_count => {order: :desc} } | |
| 39 | + end | |
| 40 | + end | |
| 41 | + | |
| 24 | 42 | include SearchableModelHelper |
| 25 | 43 | end | ... | ... |
plugins/elasticsearch/lib/ext/event.rb
plugins/elasticsearch/lib/ext/person.rb
| ... | ... | @@ -8,6 +8,8 @@ class Person |
| 8 | 8 | { |
| 9 | 9 | :visible => {type: 'boolean'}, |
| 10 | 10 | :secret => { type: :boolean }, |
| 11 | + :activities_count => { type: :integer }, | |
| 12 | + :friends_count => { type: :integer } | |
| 11 | 13 | } |
| 12 | 14 | end |
| 13 | 15 | |
| ... | ... | @@ -22,5 +24,21 @@ class Person |
| 22 | 24 | ] |
| 23 | 25 | end |
| 24 | 26 | |
| 27 | + def self.especific_filter | |
| 28 | + { | |
| 29 | + :more_active => { label: _("More Active") }, | |
| 30 | + :more_popular => { label: _("More Popular") } | |
| 31 | + } | |
| 32 | + end | |
| 33 | + | |
| 34 | + def self.get_sort_by sort_by | |
| 35 | + case sort_by | |
| 36 | + when "more_active" | |
| 37 | + { :activities_count => {order: :desc}} | |
| 38 | + when "more_popular" | |
| 39 | + { :friends_count => {order: :desc} } | |
| 40 | + end | |
| 41 | + end | |
| 42 | + | |
| 25 | 43 | include SearchableModelHelper |
| 26 | 44 | end | ... | ... |