diff --git a/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb b/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb index 908981f..68d08cd 100644 --- a/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb +++ b/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb @@ -22,12 +22,12 @@ class ElasticsearchPluginController < ApplicationController end def define_searchable_types - @searchable_types = ElasticsearchHelper::searchable_types + @searchable_types = searchable_types @selected_type = (params[:selected_type]|| :all ).to_sym end def define_search_fields_types - @filter_types = ElasticsearchHelper::filters + @filter_types = filters @selected_filter = (params[:filter] || :relevance).to_sym end diff --git a/plugins/elasticsearch/helpers/elasticsearch_helper.rb b/plugins/elasticsearch/helpers/elasticsearch_helper.rb index 32ea7df..bc78f5f 100644 --- a/plugins/elasticsearch/helpers/elasticsearch_helper.rb +++ b/plugins/elasticsearch/helpers/elasticsearch_helper.rb @@ -1,6 +1,6 @@ module ElasticsearchHelper - def self.searchable_types + def searchable_types { :all => { label: _("All Results")}, :text_article => { label: _("Articles")}, @@ -11,12 +11,20 @@ module ElasticsearchHelper } end - def self.filters - { + def filters + filters = { :relevance => { label: _("Relevance")}, :lexical => { label: _("Alphabetical")}, :more_recent => { label: _("More Recent")}, } + + selected_type = params[:selected_type] || nil + + if selected_type + klass = selected_type.to_s.classify.constantize + filters.update klass.especific_filter if klass.respond_to? :especific_filter + end + filters end def process_results @@ -35,7 +43,7 @@ module ElasticsearchHelper begin klass = model.to_s.classify.constantize - query = get_query params[:query], klass: klass, sort_by: get_sort_by(params[:filter]) + query = get_query params[:query], klass: klass, sort_by: get_sort_by(params[:filter],klass) klass.search(query, size: default_per_page(params[:per_page])).page(params[:page]).records rescue [] @@ -46,12 +54,14 @@ module ElasticsearchHelper per_page ||= 10 end - def get_sort_by sort_by + def get_sort_by sort_by, klass=nil case sort_by when "lexical" { "name.raw" => {"order" => "asc" }} when "more_recent" { "created_at" => {"order" => "desc"}} + else + ( klass and klass.respond_to?(:get_sort_by) ) ? klass.get_sort_by(sort_by) : nil end end diff --git a/plugins/elasticsearch/lib/elasticsearch_plugin/api.rb b/plugins/elasticsearch/lib/elasticsearch_plugin/api.rb index 9326199..160c10b 100644 --- a/plugins/elasticsearch/lib/elasticsearch_plugin/api.rb +++ b/plugins/elasticsearch/lib/elasticsearch_plugin/api.rb @@ -14,10 +14,14 @@ class ElasticsearchPlugin::API < Grape::API end get 'types' do - types = {types: ElasticsearchHelper::searchable_types.stringify_keys.keys} + types = {types: searchable_types.stringify_keys.keys } present types, with: Grape::Presenters::Presenter end + get 'filters' do + present filters, with: Grape::Presenters::Presenter + end + end end diff --git a/plugins/elasticsearch/lib/ext/community.rb b/plugins/elasticsearch/lib/ext/community.rb index e4c9010..1771aab 100644 --- a/plugins/elasticsearch/lib/ext/community.rb +++ b/plugins/elasticsearch/lib/ext/community.rb @@ -7,6 +7,8 @@ class Community { :secret => { type: :boolean }, :visible => { type: :boolean }, + :activities_count => { type: :integer }, + :members_count => { type: :integer } } end @@ -21,5 +23,21 @@ class Community ] end + def self.especific_filter + { + :more_active => { label: _("More Active") }, + :more_popular => { label: _("More Popular") } + } + end + + def self.get_sort_by sort_by + case sort_by + when "more_active" + { :activities_count => {order: :desc}} + when "more_popular" + { :members_count => {order: :desc} } + end + end + include SearchableModelHelper end diff --git a/plugins/elasticsearch/lib/ext/event.rb b/plugins/elasticsearch/lib/ext/event.rb index 7e74fa4..c503da7 100644 --- a/plugins/elasticsearch/lib/ext/event.rb +++ b/plugins/elasticsearch/lib/ext/event.rb @@ -4,6 +4,7 @@ require_relative '../../helpers/searchable_model_helper' require_relative '../../helpers/nested_helper/profile' class Event + #TODO: o filtro é feito de forma diferente do artigo def self.control_fields { diff --git a/plugins/elasticsearch/lib/ext/person.rb b/plugins/elasticsearch/lib/ext/person.rb index 2691975..138931f 100644 --- a/plugins/elasticsearch/lib/ext/person.rb +++ b/plugins/elasticsearch/lib/ext/person.rb @@ -8,6 +8,8 @@ class Person { :visible => {type: 'boolean'}, :secret => { type: :boolean }, + :activities_count => { type: :integer }, + :friends_count => { type: :integer } } end @@ -22,5 +24,21 @@ class Person ] end + def self.especific_filter + { + :more_active => { label: _("More Active") }, + :more_popular => { label: _("More Popular") } + } + end + + def self.get_sort_by sort_by + case sort_by + when "more_active" + { :activities_count => {order: :desc}} + when "more_popular" + { :friends_count => {order: :desc} } + end + end + include SearchableModelHelper end -- libgit2 0.21.2