diff --git a/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb b/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb index 68d08cd..6098ec9 100644 --- a/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb +++ b/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb @@ -11,7 +11,7 @@ class ElasticsearchPluginController < ApplicationController def search define_searchable_types - define_search_fields_types + define_sort_types define_results end @@ -26,9 +26,9 @@ class ElasticsearchPluginController < ApplicationController @selected_type = (params[:selected_type]|| :all ).to_sym end - def define_search_fields_types - @filter_types = filters - @selected_filter = (params[:filter] || :relevance).to_sym + def define_sort_types + @sort_types = sort_types + @selected_sort = (params[:filter] || :relevance).to_sym end end diff --git a/plugins/elasticsearch/helpers/elasticsearch_helper.rb b/plugins/elasticsearch/helpers/elasticsearch_helper.rb index bc78f5f..6177d78 100644 --- a/plugins/elasticsearch/helpers/elasticsearch_helper.rb +++ b/plugins/elasticsearch/helpers/elasticsearch_helper.rb @@ -11,8 +11,8 @@ module ElasticsearchHelper } end - def filters - filters = { + def sort_types + sorts = { :relevance => { label: _("Relevance")}, :lexical => { label: _("Alphabetical")}, :more_recent => { label: _("More Recent")}, @@ -20,11 +20,11 @@ module ElasticsearchHelper selected_type = params[:selected_type] || nil - if selected_type + if selected_type and selected_type.to_sym != :all klass = selected_type.to_s.classify.constantize - filters.update klass.especific_filter if klass.respond_to? :especific_filter + sorts.update klass.especific_sort if klass.respond_to? :especific_sort end - filters + sorts end def process_results diff --git a/plugins/elasticsearch/helpers/searchable_model/elasticsearch_indexed_model.rb b/plugins/elasticsearch/helpers/searchable_model/elasticsearch_indexed_model.rb index 42f40df..52ef9a0 100644 --- a/plugins/elasticsearch/helpers/searchable_model/elasticsearch_indexed_model.rb +++ b/plugins/elasticsearch/helpers/searchable_model/elasticsearch_indexed_model.rb @@ -66,8 +66,9 @@ module ElasticsearchIndexedModel def indexed_fields fields = { - :environment => {type: :nested, hash: NestedEnvironment::environment_hash }, - :created_at => {type: :date } + :environment => {type: :nested, hash: NestedEnvironment::environment_hash }, + :category_ids => {type: :integer }, + :created_at => {type: :date } } fields.update(self::SEARCHABLE_FIELDS) fields.update(self.control_fields) diff --git a/plugins/elasticsearch/lib/elasticsearch_plugin/api.rb b/plugins/elasticsearch/lib/elasticsearch_plugin/api.rb index 160c10b..b18e9dc 100644 --- a/plugins/elasticsearch/lib/elasticsearch_plugin/api.rb +++ b/plugins/elasticsearch/lib/elasticsearch_plugin/api.rb @@ -18,8 +18,8 @@ class ElasticsearchPlugin::API < Grape::API present types, with: Grape::Presenters::Presenter end - get 'filters' do - present filters, with: Grape::Presenters::Presenter + get 'sort_types' do + present sort_types, with: Grape::Presenters::Presenter end end diff --git a/plugins/elasticsearch/lib/ext/community.rb b/plugins/elasticsearch/lib/ext/community.rb index 1771aab..7deebb3 100644 --- a/plugins/elasticsearch/lib/ext/community.rb +++ b/plugins/elasticsearch/lib/ext/community.rb @@ -23,7 +23,7 @@ class Community ] end - def self.especific_filter + def self.especific_sort { :more_active => { label: _("More Active") }, :more_popular => { label: _("More Popular") } diff --git a/plugins/elasticsearch/lib/ext/person.rb b/plugins/elasticsearch/lib/ext/person.rb index 138931f..4299b19 100644 --- a/plugins/elasticsearch/lib/ext/person.rb +++ b/plugins/elasticsearch/lib/ext/person.rb @@ -24,7 +24,7 @@ class Person ] end - def self.especific_filter + def self.especific_sort { :more_active => { label: _("More Active") }, :more_popular => { label: _("More Popular") } diff --git a/plugins/elasticsearch/lib/ext/text_article.rb b/plugins/elasticsearch/lib/ext/text_article.rb index d400048..c28ff24 100644 --- a/plugins/elasticsearch/lib/ext/text_article.rb +++ b/plugins/elasticsearch/lib/ext/text_article.rb @@ -10,9 +10,11 @@ class TextArticle def self.control_fields { - :advertise => { type: :boolean }, - :published => { type: 'boolean'}, - :profile => { type: :nested , hash: NestedProfile.hash } + :advertise => { type: :boolean }, + :published => { type: 'boolean'}, + :comments_count => { type: :integer }, + :hits => { type: :integer }, + :profile => { type: :nested , hash: NestedProfile.hash } } end @@ -32,5 +34,21 @@ class TextArticle ] end + def self.especific_sort + { + :more_popular => { label: _("More Viewed") }, + :more_comments => { label: _("More Commented") } + } + end + + def self.get_sort_by sort_by + case sort_by + when "more_popular" + { :hits => {order: :desc} } + when "more_comments" + { :comments_count => {order: :desc}} + end + end + include SearchableModelHelper end diff --git a/plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb b/plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb index ae62cd1..425be80 100644 --- a/plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb +++ b/plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb @@ -26,18 +26,18 @@