Commit 123c7c03e5534757e5f01ebcd309e472ffc40c15

Authored by Macartur Sousa
1 parent 0ab82a23
Exists in elasticsearch_sort

Adding tests and refactored control_fields indexed

Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
plugins/elasticsearch/Rakefile
... ... @@ -24,6 +24,7 @@ task :start do
24 24 end
25 25 sh 'sudo systemctl start elasticsearch'
26 26 sh 'sudo systemctl enable elasticsearch'
  27 + sleep 100 # this sleep gives time to the service to be ready.
27 28 end
28 29  
29 30 desc "stop elasticsearch"
... ...
plugins/elasticsearch/lib/elasticsearch_indexed_model.rb
... ... @@ -7,7 +7,8 @@ module ElasticsearchIndexedModel
7 7 settings index: { number_of_shards: 1 } do
8 8 mappings dynamic: 'false' do
9 9 base.indexable_fields.each do |field, value|
10   - indexes field
  10 + value = {} if value.nil?
  11 + indexes field, type: value[:type].presence
11 12 print '.'
12 13 end
13 14 end
... ... @@ -26,7 +27,7 @@ module ElasticsearchIndexedModel
26 27  
27 28 module ClassMethods
28 29 def indexable_fields
29   - self::SEARCHABLE_FIELDS.keys + self.control_fields
  30 + self::SEARCHABLE_FIELDS.update self.control_fields
30 31 end
31 32 end
32 33  
... ...
plugins/elasticsearch/lib/ext/community.rb
... ... @@ -3,7 +3,7 @@ require_relative &#39;../elasticsearch_indexed_model&#39;
3 3  
4 4 class Community
5 5 def self.control_fields
6   - []
  6 + {}
7 7 end
8 8 include ElasticsearchIndexedModel
9 9 end
... ...
plugins/elasticsearch/lib/ext/event.rb
... ... @@ -3,10 +3,10 @@ require_relative &#39;../elasticsearch_indexed_model&#39;
3 3  
4 4 class Event
5 5 def self.control_fields
6   - [
7   - :advertise,
8   - :published,
9   - ]
  6 + {
  7 + :advertise => {},
  8 + :published => {},
  9 + }
10 10 end
11 11 include ElasticsearchIndexedModel
12 12 end
... ...
plugins/elasticsearch/lib/ext/person.rb
... ... @@ -3,10 +3,10 @@ require_relative &#39;../elasticsearch_indexed_model&#39;
3 3  
4 4 class Person
5 5 def self.control_fields
6   - [
7   - :visible,
8   - :public_profile,
9   - ]
  6 + {
  7 + :visible => {type: 'boolean'},
  8 + :public_profile => {type: 'boolean'},
  9 + }
10 10 end
11 11 include ElasticsearchIndexedModel
12 12 end
... ...
plugins/elasticsearch/lib/ext/text_article.rb
... ... @@ -3,10 +3,10 @@ require_relative &#39;../elasticsearch_indexed_model&#39;
3 3  
4 4 class TextArticle
5 5 def self.control_fields
6   - [
7   - :advertise,
8   - :published,
9   - ]
  6 + {
  7 + :advertise => nil,
  8 + :published => nil,
  9 + }
10 10 end
11 11 include ElasticsearchIndexedModel
12 12 end
... ...
plugins/elasticsearch/lib/ext/uploaded_file.rb
... ... @@ -3,10 +3,10 @@ require_relative &#39;../elasticsearch_indexed_model&#39;
3 3  
4 4 class UploadedFile
5 5 def self.control_fields
6   - [
7   - :advertise,
8   - :published,
9   - ]
  6 + {
  7 + :advertise => nil,
  8 + :published => nil,
  9 + }
10 10 end
11 11 include ElasticsearchIndexedModel
12 12 end
... ...
plugins/elasticsearch/test/test_helper.rb
... ... @@ -30,4 +30,8 @@ class ElasticsearchTestHelper &lt; ActionController::TestCase
30 30 []
31 31 end
32 32  
  33 + def indexed_fields model
  34 + model.mappings.to_hash[model.name.downcase.to_sym][:properties]
  35 + end
  36 +
33 37 end
... ...
plugins/elasticsearch/test/unit/models/community_test.rb
... ... @@ -3,19 +3,24 @@ require &quot;#{File.dirname(__FILE__)}/../../test_helper&quot;
3 3 class CommunityTest < ElasticsearchTestHelper
4 4  
5 5 def indexed_models
6   - [Community, Person]
  6 + [Community]
7 7 end
8 8  
9 9 def setup
10   - @profile = create_user('testing').person
11 10 super
12 11 end
13 12  
14   - should 'index custom fields for Event model' do
15   - community_cluster = Community.__elasticsearch__.client.cluster
  13 + should 'index searchable fields for Community model' do
  14 + Community::SEARCHABLE_FIELDS.each do |key, value|
  15 + assert_includes indexed_fields(Community), key
  16 + end
  17 + end
16 18  
17   - assert_not_nil Community.mappings.to_hash[:community][:properties][:name]
18   - assert_not_nil Community.mappings.to_hash[:community][:properties][:identifier]
19   - assert_not_nil Community.mappings.to_hash[:community][:properties][:nickname]
  19 + should 'index control fields for Community model' do
  20 + Community::control_fields.each do |key, value|
  21 + assert_includes indexed_fields(Community), key
  22 + assert_includes indexed_fields(Community)[key][:type], value[:type] || 'string'
  23 + end
20 24 end
  25 +
21 26 end
... ...
plugins/elasticsearch/test/unit/models/event_test.rb
... ... @@ -7,14 +7,20 @@ class EventTest &lt; ElasticsearchTestHelper
7 7 end
8 8  
9 9 def setup
10   - @profile = create_user('testing').person
11 10 super
12 11 end
13 12  
14   - should 'index custom fields for Event model' do
15   - event_cluster = Event.__elasticsearch__.client.cluster
  13 + should 'index searchable fields for Event model' do
  14 + Event::SEARCHABLE_FIELDS.each do |key, value|
  15 + assert_includes indexed_fields(Event), key
  16 + end
  17 + end
16 18  
17   - assert_not_nil Event.mappings.to_hash[:event][:properties][:advertise]
18   - assert_not_nil Event.mappings.to_hash[:event][:properties][:published]
  19 + should 'index control fields for Event model' do
  20 + Event::control_fields.each do |key, value|
  21 + assert_includes indexed_fields(Event), key
  22 + assert_includes indexed_fields(Event)[key][:type], value[:type] || 'string'
  23 + end
19 24 end
  25 +
20 26 end
... ...
plugins/elasticsearch/test/unit/models/person_test.rb
... ... @@ -3,21 +3,24 @@ require &quot;#{File.dirname(__FILE__)}/../../test_helper&quot;
3 3 class PersonTest < ElasticsearchTestHelper
4 4  
5 5 def indexed_models
6   - [Event]
  6 + [Person]
7 7 end
8 8  
9 9 def setup
10   - @profile = create_user('testing').person
11 10 super
12 11 end
13 12  
14   - should 'index custom fields for Event model' do
15   - person_cluster = Person.__elasticsearch__.client.cluster
  13 + should 'index searchable fields for Person model' do
  14 + Person::SEARCHABLE_FIELDS.each do |key, value|
  15 + assert_includes indexed_fields(Person), key
  16 + end
  17 + end
16 18  
17   - assert_not_nil Person.mappings.to_hash[:person][:properties][:name]
18   - assert_not_nil Person.mappings.to_hash[:person][:properties][:identifier]
19   - assert_not_nil Person.mappings.to_hash[:person][:properties][:nickname]
20   - assert_not_nil Person.mappings.to_hash[:person][:properties][:visible]
  19 + should 'index control fields for Person model' do
  20 + Person::control_fields.each do |key, value|
  21 + assert_includes indexed_fields(Person), key
  22 + assert_includes indexed_fields(Person)[key][:type], value[:type] || 'string'
  23 + end
21 24 end
22 25  
23 26 end
... ...