Commit 80baa449bf5c114322e4dbb8726f4683a1075acf

Authored by Luciano Prestes
Committed by Macartur Sousa
1 parent e9727422
Exists in elasticsearch_sort

Index models for elasticsearch

Signed-off-by: Luciano Prestes Cavalcanti <lucianopcbr@gmail.com>
Signed-off-by: Lucas Moura <lucas.moura128@gmail.com>
plugins/elasticsearch/lib/elasticsearch_plugin.rb
@@ -10,4 +10,36 @@ class ElasticsearchPlugin &lt; Noosfero::Plugin @@ -10,4 +10,36 @@ class ElasticsearchPlugin &lt; Noosfero::Plugin
10 _("A plugin that does this and that.") 10 _("A plugin that does this and that.")
11 end 11 end
12 12
  13 + Noosfero::Application.class_eval do
  14 + config.after_initialize do
  15 + models = ActiveRecord::Base.descendants.select do |model|
  16 + model.const_defined?("SEARCHABLE_FIELDS")
  17 + end
  18 +
  19 + models.each do |model|
  20 + model.class_eval do
  21 + include Elasticsearch::Model
  22 + include Elasticsearch::Model::Callbacks
  23 +
  24 + settings index: { number_of_shards: 1 } do
  25 + mappings dynamic: 'false' do
  26 + model::SEARCHABLE_FIELDS.each do |field, value|
  27 + indexes field
  28 + end
  29 + end
  30 + model.__elasticsearch__.client.indices.delete \
  31 + index: model.index_name rescue nil
  32 + model.__elasticsearch__.client.indices.create \
  33 + index: model.index_name,
  34 + body: {
  35 + settings: model.settings.to_hash,
  36 + mappings: model.mappings.to_hash
  37 + }
  38 +
  39 + model.import
  40 + end
  41 + end
  42 + end
  43 + end
  44 + end
13 end 45 end
plugins/elasticsearch/lib/ext/community.rb
@@ -1,46 +0,0 @@ @@ -1,46 +0,0 @@
1 -require_dependency 'community'  
2 -  
3 -require 'elasticsearch/model'  
4 -  
5 -Community.class_eval do  
6 - include Elasticsearch::Model  
7 - include Elasticsearch::Model::Callbacks  
8 -  
9 - def self.search(query)  
10 - __elasticsearch__.search(  
11 - {  
12 - query: {  
13 - multi_match: {  
14 - query: query,  
15 - fields: ['name', 'identifier']  
16 - }  
17 - },  
18 - highlight: {  
19 - pre_tags: ['<em>'],  
20 - post_tags: ['</em>'],  
21 - fields: {  
22 - name: {},  
23 - identifier: {}  
24 - }  
25 - }  
26 - }  
27 - )  
28 - end  
29 -  
30 - settings index: { number_of_shards: 1 } do  
31 - mappings dynamic: 'false' do  
32 - indexes :name, analyzer: 'english', index_options: 'offsets'  
33 - indexes :identifier, analyzer: 'english'  
34 - end  
35 - end  
36 -end  
37 -  
38 -Community.__elasticsearch__.client.indices.delete index: Community.index_name rescue nil  
39 -  
40 -# Create the new index with the new mapping  
41 -Community.__elasticsearch__.client.indices.create \  
42 - index: Community.index_name,  
43 - body: { settings: Community.settings.to_hash, mappings: Community.mappings.to_hash }  
44 -  
45 -# Index all Community records from the DB to Elasticsearch  
46 -Community.import  
plugins/elasticsearch/test/unit/index_models_test.rb 0 → 100644
@@ -0,0 +1,21 @@ @@ -0,0 +1,21 @@
  1 +require 'test_helper'
  2 +
  3 +class IndexModelsTest < ActiveSupport::TestCase
  4 +
  5 + should "check index models on elasticsearch" do
  6 + fields = []
  7 + mappings = []
  8 +
  9 + ActiveRecord::Base.descendants.each do |model|
  10 + if model.const_defined?("SEARCHABLE_FIELDS")
  11 + mappings << model.mappings.instance_values['mapping'].keys.sort
  12 + fields << model::SEARCHABLE_FIELDS.keys.sort
  13 + end
  14 + end
  15 +
  16 + mappings.count.times do |i|
  17 + assert_equal mappings[i], fields[i]
  18 + end
  19 + end
  20 +
  21 +end