Commit 324b48bd9462125eb84e1a6ff8ec1c787fa4ccd7
1 parent
123b7823
Exists in
elasticsearch_api
Adding tests and refactored control_fields indexed
Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
Showing
11 changed files
with
59 additions
and
39 deletions
Show diff stats
plugins/elasticsearch/Rakefile
@@ -24,6 +24,7 @@ task :start do | @@ -24,6 +24,7 @@ task :start do | ||
24 | end | 24 | end |
25 | sh 'sudo systemctl start elasticsearch' | 25 | sh 'sudo systemctl start elasticsearch' |
26 | sh 'sudo systemctl enable elasticsearch' | 26 | sh 'sudo systemctl enable elasticsearch' |
27 | + sleep 100 # this sleep gives time to the service to be ready. | ||
27 | end | 28 | end |
28 | 29 | ||
29 | desc "stop elasticsearch" | 30 | desc "stop elasticsearch" |
plugins/elasticsearch/lib/elasticsearch_indexed_model.rb
@@ -7,7 +7,8 @@ module ElasticsearchIndexedModel | @@ -7,7 +7,8 @@ module ElasticsearchIndexedModel | ||
7 | settings index: { number_of_shards: 1 } do | 7 | settings index: { number_of_shards: 1 } do |
8 | mappings dynamic: 'false' do | 8 | mappings dynamic: 'false' do |
9 | base.indexable_fields.each do |field, value| | 9 | base.indexable_fields.each do |field, value| |
10 | - indexes field | 10 | + value = {} if value.nil? |
11 | + indexes field, type: value[:type].presence | ||
11 | print '.' | 12 | print '.' |
12 | end | 13 | end |
13 | end | 14 | end |
@@ -26,7 +27,7 @@ module ElasticsearchIndexedModel | @@ -26,7 +27,7 @@ module ElasticsearchIndexedModel | ||
26 | 27 | ||
27 | module ClassMethods | 28 | module ClassMethods |
28 | def indexable_fields | 29 | def indexable_fields |
29 | - self::SEARCHABLE_FIELDS.keys + self.control_fields | 30 | + self::SEARCHABLE_FIELDS.update self.control_fields |
30 | end | 31 | end |
31 | end | 32 | end |
32 | 33 |
plugins/elasticsearch/lib/ext/community.rb
@@ -3,7 +3,7 @@ require_relative '../elasticsearch_indexed_model' | @@ -3,7 +3,7 @@ require_relative '../elasticsearch_indexed_model' | ||
3 | 3 | ||
4 | class Community | 4 | class Community |
5 | def self.control_fields | 5 | def self.control_fields |
6 | - [] | 6 | + {} |
7 | end | 7 | end |
8 | include ElasticsearchIndexedModel | 8 | include ElasticsearchIndexedModel |
9 | end | 9 | end |
plugins/elasticsearch/lib/ext/event.rb
@@ -3,10 +3,10 @@ require_relative '../elasticsearch_indexed_model' | @@ -3,10 +3,10 @@ require_relative '../elasticsearch_indexed_model' | ||
3 | 3 | ||
4 | class Event | 4 | class Event |
5 | def self.control_fields | 5 | def self.control_fields |
6 | - [ | ||
7 | - :advertise, | ||
8 | - :published, | ||
9 | - ] | 6 | + { |
7 | + :advertise => {}, | ||
8 | + :published => {}, | ||
9 | + } | ||
10 | end | 10 | end |
11 | include ElasticsearchIndexedModel | 11 | include ElasticsearchIndexedModel |
12 | end | 12 | end |
plugins/elasticsearch/lib/ext/person.rb
@@ -3,10 +3,10 @@ require_relative '../elasticsearch_indexed_model' | @@ -3,10 +3,10 @@ require_relative '../elasticsearch_indexed_model' | ||
3 | 3 | ||
4 | class Person | 4 | class Person |
5 | def self.control_fields | 5 | def self.control_fields |
6 | - [ | ||
7 | - :visible, | ||
8 | - :public_profile, | ||
9 | - ] | 6 | + { |
7 | + :visible => {type: 'boolean'}, | ||
8 | + :public_profile => {type: 'boolean'}, | ||
9 | + } | ||
10 | end | 10 | end |
11 | include ElasticsearchIndexedModel | 11 | include ElasticsearchIndexedModel |
12 | end | 12 | end |
plugins/elasticsearch/lib/ext/text_article.rb
@@ -3,10 +3,10 @@ require_relative '../elasticsearch_indexed_model' | @@ -3,10 +3,10 @@ require_relative '../elasticsearch_indexed_model' | ||
3 | 3 | ||
4 | class TextArticle | 4 | class TextArticle |
5 | def self.control_fields | 5 | def self.control_fields |
6 | - [ | ||
7 | - :advertise, | ||
8 | - :published, | ||
9 | - ] | 6 | + { |
7 | + :advertise => nil, | ||
8 | + :published => nil, | ||
9 | + } | ||
10 | end | 10 | end |
11 | include ElasticsearchIndexedModel | 11 | include ElasticsearchIndexedModel |
12 | end | 12 | end |
plugins/elasticsearch/lib/ext/uploaded_file.rb
@@ -3,10 +3,10 @@ require_relative '../elasticsearch_indexed_model' | @@ -3,10 +3,10 @@ require_relative '../elasticsearch_indexed_model' | ||
3 | 3 | ||
4 | class UploadedFile | 4 | class UploadedFile |
5 | def self.control_fields | 5 | def self.control_fields |
6 | - [ | ||
7 | - :advertise, | ||
8 | - :published, | ||
9 | - ] | 6 | + { |
7 | + :advertise => nil, | ||
8 | + :published => nil, | ||
9 | + } | ||
10 | end | 10 | end |
11 | include ElasticsearchIndexedModel | 11 | include ElasticsearchIndexedModel |
12 | end | 12 | end |
plugins/elasticsearch/test/test_helper.rb
@@ -30,4 +30,8 @@ class ElasticsearchTestHelper < ActionController::TestCase | @@ -30,4 +30,8 @@ class ElasticsearchTestHelper < ActionController::TestCase | ||
30 | [] | 30 | [] |
31 | end | 31 | end |
32 | 32 | ||
33 | + def indexed_fields model | ||
34 | + model.mappings.to_hash[model.name.downcase.to_sym][:properties] | ||
35 | + end | ||
36 | + | ||
33 | end | 37 | end |
plugins/elasticsearch/test/unit/models/community_test.rb
@@ -3,19 +3,24 @@ require "#{File.dirname(__FILE__)}/../../test_helper" | @@ -3,19 +3,24 @@ require "#{File.dirname(__FILE__)}/../../test_helper" | ||
3 | class CommunityTest < ElasticsearchTestHelper | 3 | class CommunityTest < ElasticsearchTestHelper |
4 | 4 | ||
5 | def indexed_models | 5 | def indexed_models |
6 | - [Community, Person] | 6 | + [Community] |
7 | end | 7 | end |
8 | 8 | ||
9 | def setup | 9 | def setup |
10 | - @profile = create_user('testing').person | ||
11 | super | 10 | super |
12 | end | 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 | end | 24 | end |
25 | + | ||
21 | end | 26 | end |
plugins/elasticsearch/test/unit/models/event_test.rb
@@ -7,14 +7,20 @@ class EventTest < ElasticsearchTestHelper | @@ -7,14 +7,20 @@ class EventTest < ElasticsearchTestHelper | ||
7 | end | 7 | end |
8 | 8 | ||
9 | def setup | 9 | def setup |
10 | - @profile = create_user('testing').person | ||
11 | super | 10 | super |
12 | end | 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 | end | 24 | end |
25 | + | ||
20 | end | 26 | end |
plugins/elasticsearch/test/unit/models/person_test.rb
@@ -3,21 +3,24 @@ require "#{File.dirname(__FILE__)}/../../test_helper" | @@ -3,21 +3,24 @@ require "#{File.dirname(__FILE__)}/../../test_helper" | ||
3 | class PersonTest < ElasticsearchTestHelper | 3 | class PersonTest < ElasticsearchTestHelper |
4 | 4 | ||
5 | def indexed_models | 5 | def indexed_models |
6 | - [Event] | 6 | + [Person] |
7 | end | 7 | end |
8 | 8 | ||
9 | def setup | 9 | def setup |
10 | - @profile = create_user('testing').person | ||
11 | super | 10 | super |
12 | end | 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 | end | 24 | end |
22 | 25 | ||
23 | end | 26 | end |