From af8e20d4725f971b7d613fd9bad48a3edf39e67c Mon Sep 17 00:00:00 2001 From: Macartur Sousa Date: Sun, 15 May 2016 16:09:52 -0300 Subject: [PATCH] Elasticsearch: Changed models and adding tests --- plugins/elasticsearch/Gemfile | 3 ++- plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb | 12 ++---------- plugins/elasticsearch/lib/elasticsearch_plugin.rb | 40 +++------------------------------------- plugins/elasticsearch/lib/load_models.rb | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/elasticsearch/views/elasticsearch_plugin/_article_display.html.erb | 2 +- plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb | 2 +- spec/models/community_spec.rb | 27 ++++++--------------------- spec/spec_helper.rb | 4 ++++ 8 files changed, 78 insertions(+), 71 deletions(-) create mode 100644 plugins/elasticsearch/lib/load_models.rb diff --git a/plugins/elasticsearch/Gemfile b/plugins/elasticsearch/Gemfile index 6b6aa79..8a9e481 100644 --- a/plugins/elasticsearch/Gemfile +++ b/plugins/elasticsearch/Gemfile @@ -1,4 +1,5 @@ source 'https://rubygems.org' gem 'elasticsearch-model' -gem 'elasticsearch-rails' +gem 'elasticsearch-rails' +gem 'elasticsearch-extensions' diff --git a/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb b/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb index 270a899..08735d5 100644 --- a/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb +++ b/plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb @@ -5,24 +5,16 @@ class ElasticsearchPluginController < ApplicationController @results = [] @query = params[:q] @checkbox = {} - terms = get_terms(params) - puts "=" * 80, terms, "=" * 80 + if params[:model].present? params[:model].keys.each do |model| @checkbox[model.to_sym] = true klass = model.classify.constantize query = get_query params[:q], klass - @results += klass.__elasticsearch__.search(query).records.to_a + @results |= klass.__elasticsearch__.search(query).records.to_a end end - if params[:filter].present? - params[:filter].keys.each do |model| - params[:filter][model].keys.each do |filter| - @checkbox[filter.to_sym] = true - end - end - end end private diff --git a/plugins/elasticsearch/lib/elasticsearch_plugin.rb b/plugins/elasticsearch/lib/elasticsearch_plugin.rb index ffb0c4d..ab1785d 100644 --- a/plugins/elasticsearch/lib/elasticsearch_plugin.rb +++ b/plugins/elasticsearch/lib/elasticsearch_plugin.rb @@ -1,48 +1,14 @@ class ElasticsearchPlugin < Noosfero::Plugin def self.plugin_name - # FIXME "ElasticsearchPlugin" end def self.plugin_description - # FIXME - _("A plugin that does this and that.") + _("This plugin is used to communicate a elasticsearch to privide a search.") end - Noosfero::Application.class_eval do - config.after_initialize do + # load all models to provide searchable fields + require_relative "load_models" - Rails.application.eager_load! #TODO: REMOVE THIS LINE - - models = ActiveRecord::Base.descendants.select do |model| - model.const_defined?("SEARCHABLE_FIELDS") - end - - models.each do |model| - model.class_eval do - include Elasticsearch::Model - include Elasticsearch::Model::Callbacks - - settings index: { number_of_shards: 1 } do - mappings dynamic: 'false' do - model::SEARCHABLE_FIELDS.each do |field, value| - indexes field - end - end - model.__elasticsearch__.client.indices.delete \ - index: model.index_name rescue nil - model.__elasticsearch__.client.indices.create \ - index: model.index_name, - body: { - settings: model.settings.to_hash, - mappings: model.mappings.to_hash - } - - model.import - end - end - end - end - end end diff --git a/plugins/elasticsearch/lib/load_models.rb b/plugins/elasticsearch/lib/load_models.rb new file mode 100644 index 0000000..4954048 --- /dev/null +++ b/plugins/elasticsearch/lib/load_models.rb @@ -0,0 +1,59 @@ +Noosfero::Application.class_eval do + + config.after_initialize do + Rails.application.eager_load! #TODO: REMOVE THIS LINE + indeces_models searchables_models + end + + def searchables_models + ActiveRecord::Base.descendants.select do |model| + model.const_defined?("SEARCHABLE_FIELDS") + end + end + + def indeces_models models + indexed_models = Array.new + models.each do |model| + next if indexed_models.include? model + + create_searchable_model model + indexed_models.push model + + if model.descendants.count > 0 + model.descendants.each { | descendant_model| + indexed_models.push descendant_model + create_searchable_model descendant_model + } + end + + end + + end + + def create_searchable_model model + model.class_eval do + include Elasticsearch::Model + include Elasticsearch::Model::Callbacks + settings index: { number_of_shards: 1 } do + mappings dynamic: 'false' do + model::SEARCHABLE_FIELDS.each do |field, value| + indexes field + end + end + + model.__elasticsearch__.client.indices.delete \ + index: model.index_name rescue nil + + model.__elasticsearch__.client.indices.create \ + index: model.index_name, + body: { + settings: model.settings.to_hash, + mappings: model.mappings.to_hash + } + + model.import + end + end + end + +end diff --git a/plugins/elasticsearch/views/elasticsearch_plugin/_article_display.html.erb b/plugins/elasticsearch/views/elasticsearch_plugin/_article_display.html.erb index cd586ab..fec9b85 100644 --- a/plugins/elasticsearch/views/elasticsearch_plugin/_article_display.html.erb +++ b/plugins/elasticsearch/views/elasticsearch_plugin/_article_display.html.erb @@ -1,2 +1,2 @@ -Article: <%= article.name %> +Article: <%= article.id %><%= article.name %> diff --git a/plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb b/plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb index b8f2f96..b0bcc67 100644 --- a/plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb +++ b/plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb @@ -15,7 +15,7 @@ <%= check_box_tag 'model[articles]', 1, @checkbox[:articles] %> <%= label_tag('articles', _("articles")) %> - <%= check_box_tag 'filter[articles][gallery]', :type, @checkbox[:gallery] %> + <%= check_box_tag 'model[gallery]', :type, @checkbox[:gallery] %> <%= label_tag('gallery', _("gallery")) %> <% end %> diff --git a/spec/models/community_spec.rb b/spec/models/community_spec.rb index da15e86..9d90fa4 100644 --- a/spec/models/community_spec.rb +++ b/spec/models/community_spec.rb @@ -2,37 +2,22 @@ require 'rails_helper' require 'rake' require 'elasticsearch/extensions/test/cluster/tasks' -RSpec.configure do |config| - config.before :each, elasticsearch: true do - puts '='*10, 'before', '='*10 - Elasticsearch::Extensions::Test::Cluster.start() unless Elasticsearch::Extensions::Test::Cluster.running? - end - - config.after :suite do - puts '='*10, 'after', '='*10 - Elasticsearch::Extensions::Test::Cluster.stop() if Elasticsearch::Extensions::Test::Cluster.running? - end -end - -RSpec.describe Community, type: :model, elasticsearch: true do +describe Community, type: :model, elasticsearch: true do before do - Environment.create!(:name => 'Noosfero', :contact_email => 'noosfero@localhost.localdomain', :is_default => true) - @environment = Environment.default @environment.enabled_plugins = ['ElasticsearchPlugin'] @environment.save! @community = Community.new(name: "Debian") @community.save! - - sleep 2 + _start = Time.new + Article.import + sleep 4 + p Article.__elasticsearch__.client.cluster.health end it "assert true" do - communities = Community.__elasticsearch__.search({}).records.to_a - - p communities - + Article.__elasticsearch__.search({}).records.to_a expect(true).to be true end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 913e28a..ff43c1b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -16,7 +16,11 @@ # users commonly want. # # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +require 'rake' +require 'elasticsearch/extensions/test/cluster/tasks' + RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate # assertion/expectation library such as wrong or the stdlib/minitest # assertions if you prefer. -- libgit2 0.21.2