Commit 3ba2b5e89a21eb098b117e9bc842ead04b3df9bb
Exists in
elasticsearch_api
Merge branch 'elasticsearch' of softwarepublico.gov.br:noosferogov/noosfero into elasticsearch
Showing
19 changed files
with
153 additions
and
72 deletions
Show diff stats
plugins/elasticsearch/Gemfile
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
... | ... | @@ -10,24 +10,16 @@ class ElasticsearchPluginController < ApplicationController |
10 | 10 | @results = [] |
11 | 11 | @query = params[:q] |
12 | 12 | @checkbox = {} |
13 | - terms = get_terms(params) | |
14 | - puts "=" * 80, terms, "=" * 80 | |
13 | + | |
15 | 14 | if params[:model].present? |
16 | 15 | params[:model].keys.each do |model| |
17 | 16 | @checkbox[model.to_sym] = true |
18 | 17 | klass = model.classify.constantize |
19 | 18 | query = get_query params[:q], klass |
20 | - @results += klass.__elasticsearch__.search(query).records.to_a | |
19 | + @results |= klass.__elasticsearch__.search(query).records.to_a | |
21 | 20 | end |
22 | 21 | end |
23 | 22 | |
24 | - if params[:filter].present? | |
25 | - params[:filter].keys.each do |model| | |
26 | - params[:filter][model].keys.each do |filter| | |
27 | - @checkbox[filter.to_sym] = true | |
28 | - end | |
29 | - end | |
30 | - end | |
31 | 23 | end |
32 | 24 | |
33 | 25 | private | ... | ... |
... | ... | @@ -0,0 +1,31 @@ |
1 | +module INDEXED_MODEL | |
2 | + include Elasticsearch::Model | |
3 | + include Elasticsearch::Model::Callbacks | |
4 | + | |
5 | + def self.included base | |
6 | + base.extend ClassMethods | |
7 | + end | |
8 | + | |
9 | + module ClassMethods | |
10 | +# settings index: { number_of_shards: 1 } do | |
11 | +# mappings dynamic: 'false' do | |
12 | +# self::SEARCHABLE_FIELDS.each do |field, value| | |
13 | +# indexes field | |
14 | +# end | |
15 | +# end | |
16 | +# | |
17 | +# self.__elasticsearch__.client.indices.delete \ | |
18 | +# index: self.index_name rescue nil | |
19 | +# | |
20 | +# self.__elasticsearch__.client.indices.create \ | |
21 | +# index: self.index_name, | |
22 | +# body: { | |
23 | +# settings: self.settings.to_hash, | |
24 | +# mappings: self.mappings.to_hash | |
25 | +# } | |
26 | +# | |
27 | +# self.import | |
28 | +# end | |
29 | + end | |
30 | +end | |
31 | + | ... | ... |
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 | |
15 | - | |
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 | 11 | end | ... | ... |
... | ... | @@ -0,0 +1,15 @@ |
1 | +require_dependency 'article' | |
2 | +require_relative '../elasticsearch_helper' | |
3 | + | |
4 | +class Article | |
5 | + include INDEXED_MODEL | |
6 | + | |
7 | + def self.control_fields | |
8 | + %w(advertise published).map{ |e| e.to_sym } | |
9 | + end | |
10 | + | |
11 | + def self.indexable_fields | |
12 | + SEARCHABLE_FIELDS.keys + self.control_fields | |
13 | + end | |
14 | + | |
15 | +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. | ... | ... |