From 9adc49471eb8568215ea950b94c0c3830d2633af Mon Sep 17 00:00:00 2001 From: DylanGuedes Date: Mon, 27 Jun 2016 18:59:40 +0000 Subject: [PATCH] Adds created at as a type of sort --- plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb | 8 +------- plugins/elasticsearch/helpers/elasticsearch_helper.rb | 27 +++++++++++++++++++++------ plugins/elasticsearch/lib/ext/community.rb | 4 +++- plugins/elasticsearch/lib/ext/event.rb | 1 + plugins/elasticsearch/lib/ext/person.rb | 1 + plugins/elasticsearch/lib/ext/text_article.rb | 3 ++- plugins/elasticsearch/lib/ext/uploaded_file.rb | 1 + plugins/elasticsearch/test/unit/controllers/elasticsearch_plugin_controller_test.rb | 1 - plugins/elasticsearch/views/elasticsearch_plugin/_person_display.html.erb | 1 + plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb | 8 +++++--- 10 files changed, 36 insertions(+), 19 deletions(-) diff --git a/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb b/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb index a7f1ef8..62659f4 100644 --- a/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb +++ b/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb @@ -27,12 +27,6 @@ class ElasticsearchPluginController < ApplicationController def define_search_fields_types @search_filter_types = ElasticsearchHelper::search_filters - @selected_filter_field = (params[:selected_filter_field] || ElasticsearchHelper::search_filters.keys.first).to_sym + @selected_filter_field = (params[:selected_filter_field]) end - - private - -# def permit_params -# params.require(:per_page, :query) -# end end diff --git a/plugins/elasticsearch/helpers/elasticsearch_helper.rb b/plugins/elasticsearch/helpers/elasticsearch_helper.rb index f9e351d..c2cad56 100644 --- a/plugins/elasticsearch/helpers/elasticsearch_helper.rb +++ b/plugins/elasticsearch/helpers/elasticsearch_helper.rb @@ -33,6 +33,15 @@ module ElasticsearchHelper fields.to_a end + def get_sort_by sort_by + case sort_by + when "lexical" + "name.raw" + when "recent" + "created_at" + end + end + def process_results selected_type = (params[:selected_type]|| :all).to_sym if selected_type == :all @@ -43,7 +52,7 @@ module ElasticsearchHelper end def search_from_all_models - query = get_query params[:query] + query = get_query params[:query], sort_by: get_sort_by(params[:selected_filter_field]) models = searchable_models Elasticsearch::Model.search(query, models, size: default_per_page(params[:per_page])).page(params[:page]).records end @@ -51,7 +60,8 @@ module ElasticsearchHelper def search_from_model model begin klass = model.to_s.classify.constantize - query = get_query params[:query], klass + + query = get_query params[:query], klass: klass, sort_by: get_sort_by(params[:selected_filter_field]) klass.search(query, size: default_per_page(params[:per_page])).page(params[:page]).records rescue [] @@ -63,7 +73,7 @@ module ElasticsearchHelper end private - + def searchable_models ElasticsearchHelper::searchable_types.except(:all).keys.map { | model | model.to_s.classify.constantize } end @@ -87,12 +97,17 @@ module ElasticsearchHelper query_exp end - def get_query text="", klass=nil + def get_query text="", options={} + klass = options[:klass] + sort_by = options[:sort_by] + fields = klass.nil? ? (fields_from_models searchable_models) : (fields_from_models [klass]) query = { - query: query_method(text, fields), - sort: "name.raw" + query: query_method(text, fields) } + if sort_by + query[:sort] = sort_by + end query end diff --git a/plugins/elasticsearch/lib/ext/community.rb b/plugins/elasticsearch/lib/ext/community.rb index 8dd9d86..b64d1bf 100644 --- a/plugins/elasticsearch/lib/ext/community.rb +++ b/plugins/elasticsearch/lib/ext/community.rb @@ -3,7 +3,9 @@ require_relative '../elasticsearch_indexed_model' class Community def self.control_fields - {} + { + :created_at => {type: 'date'} + } end include ElasticsearchIndexedModel end diff --git a/plugins/elasticsearch/lib/ext/event.rb b/plugins/elasticsearch/lib/ext/event.rb index d0a4b15..acd90b2 100644 --- a/plugins/elasticsearch/lib/ext/event.rb +++ b/plugins/elasticsearch/lib/ext/event.rb @@ -6,6 +6,7 @@ class Event { :advertise => {}, :published => {}, + :created_at => {type: 'date'} } end include ElasticsearchIndexedModel diff --git a/plugins/elasticsearch/lib/ext/person.rb b/plugins/elasticsearch/lib/ext/person.rb index c99e97e..3348442 100644 --- a/plugins/elasticsearch/lib/ext/person.rb +++ b/plugins/elasticsearch/lib/ext/person.rb @@ -6,6 +6,7 @@ class Person { :visible => {type: 'boolean'}, :public_profile => {type: 'boolean'}, + :created_at => {type: 'date'} } end include ElasticsearchIndexedModel diff --git a/plugins/elasticsearch/lib/ext/text_article.rb b/plugins/elasticsearch/lib/ext/text_article.rb index e43194b..c87c1d8 100644 --- a/plugins/elasticsearch/lib/ext/text_article.rb +++ b/plugins/elasticsearch/lib/ext/text_article.rb @@ -5,7 +5,8 @@ class TextArticle def self.control_fields { :advertise => {}, - :published => {} + :published => {}, + :created_at => {type: 'date'} } end include ElasticsearchIndexedModel diff --git a/plugins/elasticsearch/lib/ext/uploaded_file.rb b/plugins/elasticsearch/lib/ext/uploaded_file.rb index 187f8d1..17b974d 100644 --- a/plugins/elasticsearch/lib/ext/uploaded_file.rb +++ b/plugins/elasticsearch/lib/ext/uploaded_file.rb @@ -6,6 +6,7 @@ class UploadedFile { :advertise => {}, :published => {}, + :created_at => {type: 'date'} } end include ElasticsearchIndexedModel diff --git a/plugins/elasticsearch/test/unit/controllers/elasticsearch_plugin_controller_test.rb b/plugins/elasticsearch/test/unit/controllers/elasticsearch_plugin_controller_test.rb index 72b2a74..6ab5f5a 100644 --- a/plugins/elasticsearch/test/unit/controllers/elasticsearch_plugin_controller_test.rb +++ b/plugins/elasticsearch/test/unit/controllers/elasticsearch_plugin_controller_test.rb @@ -31,7 +31,6 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase assert_not_nil assigns(:searchable_types) assert_not_nil assigns(:selected_type) assert_not_nil assigns(:search_filter_types) - assert_not_nil assigns(:selected_filter_field) end should 'return 10 results if selected_type is nil and query is nil' do diff --git a/plugins/elasticsearch/views/elasticsearch_plugin/_person_display.html.erb b/plugins/elasticsearch/views/elasticsearch_plugin/_person_display.html.erb index 08ac688..01b1d19 100644 --- a/plugins/elasticsearch/views/elasticsearch_plugin/_person_display.html.erb +++ b/plugins/elasticsearch/views/elasticsearch_plugin/_person_display.html.erb @@ -7,6 +7,7 @@

<%= link_to person.name, person.url %>

<%= person.description %>

+ <%= person.created_at %>
diff --git a/plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb b/plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb index 391d43d..cce109c 100644 --- a/plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb +++ b/plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb @@ -13,8 +13,8 @@

<%= _("Sort by") %>

@@ -24,7 +24,9 @@
<%= form_tag '/plugin/elasticsearch/search', method: :get do %> <%= hidden_field_tag "selected_type", @selected_type %> - <%= hidden_field_tag "selected_filter_field", @selected_filter_field %> + <% if @selected_filter_field %> + <%= hidden_field_tag "selected_filter_field", @selected_filter_field %> + <% end %> <%= text_field_tag :query, @query %> <%= submit_tag _("Send") %> <% end %> -- libgit2 0.21.2