Commit 2bc8d51688c01c5a2b7f3ad62a82fb637ff37a4d
1 parent
fb805200
Exists in
elasticsearch_sort
Extract load_models to ext
Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
Showing
14 changed files
with
136 additions
and
62 deletions
Show diff stats
@@ -0,0 +1,31 @@ | @@ -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
@@ -8,7 +8,4 @@ class ElasticsearchPlugin < Noosfero::Plugin | @@ -8,7 +8,4 @@ class ElasticsearchPlugin < Noosfero::Plugin | ||
8 | _("This plugin is used to communicate a elasticsearch to privide a search.") | 8 | _("This plugin is used to communicate a elasticsearch to privide a search.") |
9 | end | 9 | end |
10 | 10 | ||
11 | - # load all models to provide searchable fields | ||
12 | - require_relative "load_models" | ||
13 | - | ||
14 | end | 11 | end |
@@ -0,0 +1,15 @@ | @@ -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/lib/load_models.rb
@@ -1,59 +0,0 @@ | @@ -1,59 +0,0 @@ | ||
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 |