diff --git a/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb b/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb index 77e4307..c46c1ce 100644 --- a/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb +++ b/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb @@ -1,7 +1,16 @@ class ElasticsearchPluginController < ApplicationController no_design_blocks - SEARCHABLE_MODELS = {communities: true, articles: true, people: true} + SEARCHABLE_TYPES = { :all => { label: _("All Results")}, + :community => { label: _("Communities")}, + :event => { label: _("Events")}, + :person => { label: _("People")} + } + + SEARCH_FILTERS = { :lexical => { label: _("Alphabetical Order")}, + :recent => { label: _("More Recent Order")}, + :access => { label: _("More accessed")} + } def index search() @@ -9,39 +18,37 @@ class ElasticsearchPluginController < ApplicationController end def search - @results = [] + define_searchable_types + define_search_fields_types + + process_results + end + + def process_results @query = params[:q] - @checkbox = {} - if params[:model].present? - params[:model].keys.each do |model| - @checkbox[model.to_sym] = true - results model - end + if @selected_type == :all + @results = search_from_all_models else - unless params[:q].blank? - SEARCHABLE_MODELS.keys.each do |model| - results model - end - end + @results = search_from_model @selected_type end - end private - def get_query text, klass + def fields_from_model + klass::SEARCHABLE_FIELDS.map do |key, value| + if value[:weight] + "#{key}^#{value[:weight]}" + else + "#{key}" + end + end + end + + def get_query text, klass=nil query = {} unless text.blank? - text = text.downcase - fields = klass::SEARCHABLE_FIELDS.map do |key, value| - if value[:weight] - "#{key}^#{value[:weight]}" - else - "#{key}" - end - end - query = { query: { match_all: { @@ -68,23 +75,37 @@ class ElasticsearchPluginController < ApplicationController query end - def get_terms params - terms = {} - return terms unless params[:filter].present? - params[:filter].keys.each do |model| - terms[model] = {} - params[:filter][model].keys.each do |filter| - @checkbox[filter.to_sym] = true - terms[model][params[:filter][model.to_sym][filter]] = filter - end + + def search_from_all_models + models = [] + query = get_query params[:q] + + SEARCHABLE_TYPES.keys.each {| model | models.append( model.to_s.classify.constantize) if model != :all } + Elasticsearch::Model.search(query, models, size: default_per_page).page(params[:page]).records + end + + def search_from_model model + begin + klass = model.to_s.classify.constantize + query = get_query params[:q], klass + klass.search(query, size: default_per_page).page(params[:page]).records + rescue + [] end - terms end - def results model - klass = model.to_s.classify.constantize - query = get_query params[:q], klass - @results |= klass.__elasticsearch__.search(query).records.to_a + def define_searchable_types + @searchable_types = SEARCHABLE_TYPES + @selected_type = params[:selected_type].nil? ? :all : params[:selected_type].to_sym + end + + def define_search_fields_types + @search_filter_types = SEARCH_FILTERS + @selected_filter_field = params[:selected_filter_field].nil? ? SEARCH_FILTERS.keys.first : params[:selected_filter_field].to_sym + end + + def default_per_page + 10 end end diff --git a/plugins/elasticsearch/lib/elasticsearch_plugin.rb b/plugins/elasticsearch/lib/elasticsearch_plugin.rb index f2485db..4c17a9e 100644 --- a/plugins/elasticsearch/lib/elasticsearch_plugin.rb +++ b/plugins/elasticsearch/lib/elasticsearch_plugin.rb @@ -8,4 +8,7 @@ class ElasticsearchPlugin < Noosfero::Plugin _("This plugin is used to communicate a elasticsearch to privide a search.") end + def stylesheet? + true + end end diff --git a/plugins/elasticsearch/public/style.css b/plugins/elasticsearch/public/style.css new file mode 100644 index 0000000..f312b95 --- /dev/null +++ b/plugins/elasticsearch/public/style.css @@ -0,0 +1,78 @@ +.controller-elasticsearch_plugin .wrapper { + display: flex; +} + +.controller-elasticsearch_plugin .sidebar { + width: 150px; +} + +.controller-elasticsearch_plugin .search_form { + flex: 1; +} + +.controller-elasticsearch_plugin .search_form .search_field { + margin-top: 5px; +} +.controller-elasticsearch_plugin .search_form .search_field #q { + width: 500px; +} + +.controller-elasticsearch_plugin .results-count { + margin-top: 7px; + margin-right: 15px; + text-align: right; +} + +.controller-elasticsearch_plugin ul { + list-style: none; + list-style-position: inside; + padding-left: 0px; +} + +.controller-elasticsearch_plugin .select-search-type { + padding: 10px; + margin: 10px; + font-weight: 900; + font-family: arial; + border: 1px solid #ddd; + margin-top: 0px; + margin-bottom: 0px; +} +.controller-elasticsearch_plugin .select-search-type.active { + padding: 10px; + background: #ccc; + margin-left: 10px; + margin-right: 10px; + font-weight: 900; + font-family: arial; +} + +.controller-elasticsearch_plugin .select-search-type a { + text-decoration: none; + color: black; +} + +.controller-elasticsearch_plugin .search-item .model-label { + background: #ddd; + color: black; + font-weight: 900; + padding: 3px; +} + +.controller-elasticsearch_plugin .search-item { + margin-bottom: 25px; + margin-top: 25px; +} + +.controller-elasticsearch_plugin .search-item .person-item { + display: flex; +} + +.controller-elasticsearch_plugin .right-side { + flex: 2; +} + +.controller-elasticsearch_plugin .left-side { + margin-left: 25px; + width: 100px; +} diff --git a/plugins/elasticsearch/views/elasticsearch_plugin/_article_display.html.erb b/plugins/elasticsearch/views/elasticsearch_plugin/_article_display.html.erb deleted file mode 100644 index fec9b85..0000000 --- a/plugins/elasticsearch/views/elasticsearch_plugin/_article_display.html.erb +++ /dev/null @@ -1,2 +0,0 @@ - -Article: <%= article.id %><%= article.name %> diff --git a/plugins/elasticsearch/views/elasticsearch_plugin/_community_display.html.erb b/plugins/elasticsearch/views/elasticsearch_plugin/_community_display.html.erb index f35c383..8f273b4 100644 --- a/plugins/elasticsearch/views/elasticsearch_plugin/_community_display.html.erb +++ b/plugins/elasticsearch/views/elasticsearch_plugin/_community_display.html.erb @@ -1 +1,7 @@ -Community: <%= community.name %> +
<%= community.description %>
+<%= event.body %>
+<%= person.description %>
+