Commit af8e20d4725f971b7d613fd9bad48a3edf39e67c
1 parent
b2ee20a1
Exists in
fix_sign_up_form
Elasticsearch: Changed models and adding tests
Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
Showing
8 changed files
with
78 additions
and
71 deletions
Show diff stats
plugins/elasticsearch/Gemfile
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
| ... | ... | @@ -5,24 +5,16 @@ class ElasticsearchPluginController < ApplicationController |
| 5 | 5 | @results = [] |
| 6 | 6 | @query = params[:q] |
| 7 | 7 | @checkbox = {} |
| 8 | - terms = get_terms(params) | |
| 9 | - puts "=" * 80, terms, "=" * 80 | |
| 8 | + | |
| 10 | 9 | if params[:model].present? |
| 11 | 10 | params[:model].keys.each do |model| |
| 12 | 11 | @checkbox[model.to_sym] = true |
| 13 | 12 | klass = model.classify.constantize |
| 14 | 13 | query = get_query params[:q], klass |
| 15 | - @results += klass.__elasticsearch__.search(query).records.to_a | |
| 14 | + @results |= klass.__elasticsearch__.search(query).records.to_a | |
| 16 | 15 | end |
| 17 | 16 | end |
| 18 | 17 | |
| 19 | - if params[:filter].present? | |
| 20 | - params[:filter].keys.each do |model| | |
| 21 | - params[:filter][model].keys.each do |filter| | |
| 22 | - @checkbox[filter.to_sym] = true | |
| 23 | - end | |
| 24 | - end | |
| 25 | - end | |
| 26 | 18 | end |
| 27 | 19 | |
| 28 | 20 | private | ... | ... |
plugins/elasticsearch/lib/elasticsearch_plugin.rb
| 1 | 1 | class ElasticsearchPlugin < Noosfero::Plugin |
| 2 | 2 | |
| 3 | 3 | def self.plugin_name |
| 4 | - # FIXME | |
| 5 | 4 | "ElasticsearchPlugin" |
| 6 | 5 | end |
| 7 | 6 | |
| 8 | 7 | def self.plugin_description |
| 9 | - # FIXME | |
| 10 | - _("A plugin that does this and that.") | |
| 8 | + _("This plugin is used to communicate a elasticsearch to privide a search.") | |
| 11 | 9 | end |
| 12 | 10 | |
| 13 | - Noosfero::Application.class_eval do | |
| 14 | - config.after_initialize do | |
| 11 | + # load all models to provide searchable fields | |
| 12 | + require_relative "load_models" | |
| 15 | 13 | |
| 16 | - Rails.application.eager_load! #TODO: REMOVE THIS LINE | |
| 17 | - | |
| 18 | - models = ActiveRecord::Base.descendants.select do |model| | |
| 19 | - model.const_defined?("SEARCHABLE_FIELDS") | |
| 20 | - end | |
| 21 | - | |
| 22 | - models.each do |model| | |
| 23 | - model.class_eval do | |
| 24 | - include Elasticsearch::Model | |
| 25 | - include Elasticsearch::Model::Callbacks | |
| 26 | - | |
| 27 | - settings index: { number_of_shards: 1 } do | |
| 28 | - mappings dynamic: 'false' do | |
| 29 | - model::SEARCHABLE_FIELDS.each do |field, value| | |
| 30 | - indexes field | |
| 31 | - end | |
| 32 | - end | |
| 33 | - model.__elasticsearch__.client.indices.delete \ | |
| 34 | - index: model.index_name rescue nil | |
| 35 | - model.__elasticsearch__.client.indices.create \ | |
| 36 | - index: model.index_name, | |
| 37 | - body: { | |
| 38 | - settings: model.settings.to_hash, | |
| 39 | - mappings: model.mappings.to_hash | |
| 40 | - } | |
| 41 | - | |
| 42 | - model.import | |
| 43 | - end | |
| 44 | - end | |
| 45 | - end | |
| 46 | - end | |
| 47 | - end | |
| 48 | 14 | end | ... | ... |
| ... | ... | @@ -0,0 +1,59 @@ |
| 1 | +Noosfero::Application.class_eval do | |
| 2 | + | |
| 3 | + config.after_initialize do | |
| 4 | + Rails.application.eager_load! #TODO: REMOVE THIS LINE | |
| 5 | + indeces_models searchables_models | |
| 6 | + end | |
| 7 | + | |
| 8 | + def searchables_models | |
| 9 | + ActiveRecord::Base.descendants.select do |model| | |
| 10 | + model.const_defined?("SEARCHABLE_FIELDS") | |
| 11 | + end | |
| 12 | + end | |
| 13 | + | |
| 14 | + def indeces_models models | |
| 15 | + indexed_models = Array.new | |
| 16 | + models.each do |model| | |
| 17 | + next if indexed_models.include? model | |
| 18 | + | |
| 19 | + create_searchable_model model | |
| 20 | + indexed_models.push model | |
| 21 | + | |
| 22 | + if model.descendants.count > 0 | |
| 23 | + model.descendants.each { | descendant_model| | |
| 24 | + indexed_models.push descendant_model | |
| 25 | + create_searchable_model descendant_model | |
| 26 | + } | |
| 27 | + end | |
| 28 | + | |
| 29 | + end | |
| 30 | + | |
| 31 | + end | |
| 32 | + | |
| 33 | + def create_searchable_model model | |
| 34 | + model.class_eval do | |
| 35 | + include Elasticsearch::Model | |
| 36 | + include Elasticsearch::Model::Callbacks | |
| 37 | + settings index: { number_of_shards: 1 } do | |
| 38 | + mappings dynamic: 'false' do | |
| 39 | + model::SEARCHABLE_FIELDS.each do |field, value| | |
| 40 | + indexes field | |
| 41 | + end | |
| 42 | + end | |
| 43 | + | |
| 44 | + model.__elasticsearch__.client.indices.delete \ | |
| 45 | + index: model.index_name rescue nil | |
| 46 | + | |
| 47 | + model.__elasticsearch__.client.indices.create \ | |
| 48 | + index: model.index_name, | |
| 49 | + body: { | |
| 50 | + settings: model.settings.to_hash, | |
| 51 | + mappings: model.mappings.to_hash | |
| 52 | + } | |
| 53 | + | |
| 54 | + model.import | |
| 55 | + end | |
| 56 | + end | |
| 57 | + end | |
| 58 | + | |
| 59 | +end | ... | ... |
plugins/elasticsearch/views/elasticsearch_plugin/_article_display.html.erb
plugins/elasticsearch/views/elasticsearch_plugin/search.html.erb
| ... | ... | @@ -15,7 +15,7 @@ |
| 15 | 15 | <%= check_box_tag 'model[articles]', 1, @checkbox[:articles] %> |
| 16 | 16 | <%= label_tag('articles', _("articles")) %> |
| 17 | 17 | |
| 18 | - <%= check_box_tag 'filter[articles][gallery]', :type, @checkbox[:gallery] %> | |
| 18 | + <%= check_box_tag 'model[gallery]', :type, @checkbox[:gallery] %> | |
| 19 | 19 | <%= label_tag('gallery', _("gallery")) %> |
| 20 | 20 | <% end %> |
| 21 | 21 | ... | ... |
spec/models/community_spec.rb
| ... | ... | @@ -2,37 +2,22 @@ require 'rails_helper' |
| 2 | 2 | require 'rake' |
| 3 | 3 | require 'elasticsearch/extensions/test/cluster/tasks' |
| 4 | 4 | |
| 5 | -RSpec.configure do |config| | |
| 6 | - config.before :each, elasticsearch: true do | |
| 7 | - puts '='*10, 'before', '='*10 | |
| 8 | - Elasticsearch::Extensions::Test::Cluster.start() unless Elasticsearch::Extensions::Test::Cluster.running? | |
| 9 | - end | |
| 10 | - | |
| 11 | - config.after :suite do | |
| 12 | - puts '='*10, 'after', '='*10 | |
| 13 | - Elasticsearch::Extensions::Test::Cluster.stop() if Elasticsearch::Extensions::Test::Cluster.running? | |
| 14 | - end | |
| 15 | -end | |
| 16 | - | |
| 17 | -RSpec.describe Community, type: :model, elasticsearch: true do | |
| 5 | +describe Community, type: :model, elasticsearch: true do | |
| 18 | 6 | before do |
| 19 | - Environment.create!(:name => 'Noosfero', :contact_email => 'noosfero@localhost.localdomain', :is_default => true) | |
| 20 | - | |
| 21 | 7 | @environment = Environment.default |
| 22 | 8 | @environment.enabled_plugins = ['ElasticsearchPlugin'] |
| 23 | 9 | @environment.save! |
| 24 | 10 | |
| 25 | 11 | @community = Community.new(name: "Debian") |
| 26 | 12 | @community.save! |
| 27 | - | |
| 28 | - sleep 2 | |
| 13 | + _start = Time.new | |
| 14 | + Article.import | |
| 15 | + sleep 4 | |
| 16 | + p Article.__elasticsearch__.client.cluster.health | |
| 29 | 17 | end |
| 30 | 18 | |
| 31 | 19 | it "assert true" do |
| 32 | - communities = Community.__elasticsearch__.search({}).records.to_a | |
| 33 | - | |
| 34 | - p communities | |
| 35 | - | |
| 20 | + Article.__elasticsearch__.search({}).records.to_a | |
| 36 | 21 | expect(true).to be true |
| 37 | 22 | end |
| 38 | 23 | end | ... | ... |
spec/spec_helper.rb
| ... | ... | @@ -16,7 +16,11 @@ |
| 16 | 16 | # users commonly want. |
| 17 | 17 | # |
| 18 | 18 | # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration |
| 19 | +require 'rake' | |
| 20 | +require 'elasticsearch/extensions/test/cluster/tasks' | |
| 21 | + | |
| 19 | 22 | RSpec.configure do |config| |
| 23 | + | |
| 20 | 24 | # rspec-expectations config goes here. You can use an alternate |
| 21 | 25 | # assertion/expectation library such as wrong or the stdlib/minitest |
| 22 | 26 | # assertions if you prefer. | ... | ... |