elasticsearch_plugin.rb
1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class ElasticsearchPlugin < Noosfero::Plugin
def self.plugin_name
# FIXME
"ElasticsearchPlugin"
end
def self.plugin_description
# FIXME
_("A plugin that does this and that.")
end
Noosfero::Application.class_eval do
config.after_initialize do
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