diff --git a/plugins/elasticsearch/Rakefile b/plugins/elasticsearch/Rakefile index c455af5..229a657 100644 --- a/plugins/elasticsearch/Rakefile +++ b/plugins/elasticsearch/Rakefile @@ -30,7 +30,7 @@ task :start do if elasticsearch_development sh 'sudo systemctl start elasticsearch >> /dev/null 2>&1' sh 'sudo systemctl enable elasticsearch >> /dev/null 2>&1' - sleep 2 + sleep 10 end end diff --git a/plugins/elasticsearch/helpers/elasticsearch_helper.rb b/plugins/elasticsearch/helpers/elasticsearch_helper.rb index e4ab04f..f9e351d 100644 --- a/plugins/elasticsearch/helpers/elasticsearch_helper.rb +++ b/plugins/elasticsearch/helpers/elasticsearch_helper.rb @@ -65,7 +65,7 @@ module ElasticsearchHelper private def searchable_models - SEARCHABLE_TYPES.except(:all).keys.map { | model | model.to_s.classify.constantize } + ElasticsearchHelper::searchable_types.except(:all).keys.map { | model | model.to_s.classify.constantize } end def query_method expression, fields @@ -77,7 +77,7 @@ module ElasticsearchHelper else query_exp = { multi_match: { - query: expression, + query: expression.downcase, fields: fields, tie_breaker: 0.4, minimum_should_match: "40%" diff --git a/plugins/elasticsearch/lib/elasticsearch_indexed_model.rb b/plugins/elasticsearch/lib/elasticsearch_indexed_model.rb index 41b7964..d3c77db 100644 --- a/plugins/elasticsearch/lib/elasticsearch_indexed_model.rb +++ b/plugins/elasticsearch/lib/elasticsearch_indexed_model.rb @@ -7,20 +7,13 @@ module ElasticsearchIndexedModel base.class_eval do settings index: { number_of_shards: 1 } do mappings dynamic: 'false' do - puts "="*10 - puts base.inspect - puts base.indexable_fields - base.indexable_fields.each do |field, value| + base.indexed_fields.each do |field, value| value = {} if value.nil? - if field.to_s == "name" - indexes "name", type: "string", fields: { - raw: { - type: "string", - index: "not_analyzed" - } - } + type = value[:type].presence + if type.nil? + indexes(field, fields: base.raw_field(field)) else - indexes field, type: value[:type].presence + indexes field, type: type end print '.' end @@ -40,9 +33,19 @@ module ElasticsearchIndexedModel end module ClassMethods - def indexable_fields + def raw_field name + { + raw: { + type: "string", + index: "not_analyzed" + } + } + end + + def indexed_fields self::SEARCHABLE_FIELDS.merge self.control_fields end + end end diff --git a/plugins/elasticsearch/lib/ext/community.rb b/plugins/elasticsearch/lib/ext/community.rb index cd15948..8dd9d86 100644 --- a/plugins/elasticsearch/lib/ext/community.rb +++ b/plugins/elasticsearch/lib/ext/community.rb @@ -3,12 +3,7 @@ require_relative '../elasticsearch_indexed_model' class Community def self.control_fields - {:name => {type: "string", analyzer: "english", fields: { - raw:{ - type: "string", - index: "not_analyzed" - } - }}} + {} end include ElasticsearchIndexedModel end diff --git a/plugins/elasticsearch/lib/ext/event.rb b/plugins/elasticsearch/lib/ext/event.rb index 362b765..d0a4b15 100644 --- a/plugins/elasticsearch/lib/ext/event.rb +++ b/plugins/elasticsearch/lib/ext/event.rb @@ -6,12 +6,6 @@ class Event { :advertise => {}, :published => {}, - :name => {type: "string", analyzer: "english", fields: { - raw:{ - type: "string", - index: "not_analyzed" - } - }} } end include ElasticsearchIndexedModel diff --git a/plugins/elasticsearch/lib/ext/person.rb b/plugins/elasticsearch/lib/ext/person.rb index 75ef4e5..c99e97e 100644 --- a/plugins/elasticsearch/lib/ext/person.rb +++ b/plugins/elasticsearch/lib/ext/person.rb @@ -6,12 +6,7 @@ class Person { :visible => {type: 'boolean'}, :public_profile => {type: 'boolean'}, - :name => {type: "string", fields: { - raw:{ - type: "string", - index: "not_analyzed" - } - }}} + } end include ElasticsearchIndexedModel end diff --git a/plugins/elasticsearch/test/unit/controllers/elasticsearch_plugin_controller_test.rb b/plugins/elasticsearch/test/unit/controllers/elasticsearch_plugin_controller_test.rb index 0266d66..72b2a74 100644 --- a/plugins/elasticsearch/test/unit/controllers/elasticsearch_plugin_controller_test.rb +++ b/plugins/elasticsearch/test/unit/controllers/elasticsearch_plugin_controller_test.rb @@ -15,13 +15,13 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase def create_people 5.times do | index | - create_user "person_#{index}" + create_user "person #{index}" end end def create_communities 6.times do | index | - fast_create Community, name: "community_#{index}", created_at: Date.new + fast_create Community, name: "community #{index}", created_at: Date.new end end @@ -54,31 +54,31 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase end should 'return results filtered by query' do - get :index, { 'query' => "person_"} + get :index, { 'query' => "person"} assert_response :success assert_select ".search-item", 5 assert_template partial: '_person_display' end - should 'return results filtered by query with uppercase' do - get :index, {'query' => "PERSON_1"} - assert_response :success - assert_select ".search-item", 1 - assert_template partial: '_person_display' - end + should 'return results filtered by query with uppercase' do + get :index, {'query' => "PERSON 1"} + assert_response :success + assert_template partial: '_person_display' + assert_tag(tag: "div", attributes: { class: "person-item" } , descendant: { tag: "a", child: "person 1"} ) + end - should 'return results filtered by query with downcase' do - get :index, {'query' => "person_1"} - assert_response :success - assert_select ".search-item", 1 - end + should 'return results filtered by query with downcase' do + get :index, {'query' => "person 1"} + assert_response :success + assert_tag(tag: "div", attributes: { class: "person-item" } , descendant: { tag: "a", child: "person 1"} ) + end should 'return new person indexed' do get :index, { "selected_type" => :community} assert_response :success assert_select ".search-item", 6 - fast_create Community, name: "community_#{7}", created_at: Date.new + fast_create Community, name: "community #{7}", created_at: Date.new Community.import sleep 2 @@ -108,7 +108,7 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase end should 'pass params to elastic search controller' do - get 'index', { query: 'community_' } + get 'index', { query: 'community' } assert_not_nil assigns(:results) assert_template partial: '_community_display' end -- libgit2 0.21.2