Commit d88262317147ab21fafd3f34e5ba2d569b450b86
Committed by
Macartur Sousa
1 parent
e6b4ec6f
Exists in
elasticsearch_sort
Fixes tests and alphabetical ordering
Signed-off-by: DylanGuedes <djmgguedes@gmail.com> Signed-off-by: Arthur Jahn <stutrzbecher@gmail.com> Signed-off-by: Macartur Sousa <macartur.sc@gmail.com>
Showing
7 changed files
with
37 additions
and
50 deletions
Show diff stats
plugins/elasticsearch/Rakefile
plugins/elasticsearch/helpers/elasticsearch_helper.rb
| ... | ... | @@ -65,7 +65,7 @@ module ElasticsearchHelper |
| 65 | 65 | private |
| 66 | 66 | |
| 67 | 67 | def searchable_models |
| 68 | - SEARCHABLE_TYPES.except(:all).keys.map { | model | model.to_s.classify.constantize } | |
| 68 | + ElasticsearchHelper::searchable_types.except(:all).keys.map { | model | model.to_s.classify.constantize } | |
| 69 | 69 | end |
| 70 | 70 | |
| 71 | 71 | def query_method expression, fields |
| ... | ... | @@ -77,7 +77,7 @@ module ElasticsearchHelper |
| 77 | 77 | else |
| 78 | 78 | query_exp = { |
| 79 | 79 | multi_match: { |
| 80 | - query: expression, | |
| 80 | + query: expression.downcase, | |
| 81 | 81 | fields: fields, |
| 82 | 82 | tie_breaker: 0.4, |
| 83 | 83 | minimum_should_match: "40%" | ... | ... |
plugins/elasticsearch/lib/elasticsearch_indexed_model.rb
| ... | ... | @@ -7,20 +7,13 @@ module ElasticsearchIndexedModel |
| 7 | 7 | base.class_eval do |
| 8 | 8 | settings index: { number_of_shards: 1 } do |
| 9 | 9 | mappings dynamic: 'false' do |
| 10 | - puts "="*10 | |
| 11 | - puts base.inspect | |
| 12 | - puts base.indexable_fields | |
| 13 | - base.indexable_fields.each do |field, value| | |
| 10 | + base.indexed_fields.each do |field, value| | |
| 14 | 11 | value = {} if value.nil? |
| 15 | - if field.to_s == "name" | |
| 16 | - indexes "name", type: "string", fields: { | |
| 17 | - raw: { | |
| 18 | - type: "string", | |
| 19 | - index: "not_analyzed" | |
| 20 | - } | |
| 21 | - } | |
| 12 | + type = value[:type].presence | |
| 13 | + if type.nil? | |
| 14 | + indexes(field, fields: base.raw_field(field)) | |
| 22 | 15 | else |
| 23 | - indexes field, type: value[:type].presence | |
| 16 | + indexes field, type: type | |
| 24 | 17 | end |
| 25 | 18 | print '.' |
| 26 | 19 | end |
| ... | ... | @@ -40,9 +33,19 @@ module ElasticsearchIndexedModel |
| 40 | 33 | end |
| 41 | 34 | |
| 42 | 35 | module ClassMethods |
| 43 | - def indexable_fields | |
| 36 | + def raw_field name | |
| 37 | + { | |
| 38 | + raw: { | |
| 39 | + type: "string", | |
| 40 | + index: "not_analyzed" | |
| 41 | + } | |
| 42 | + } | |
| 43 | + end | |
| 44 | + | |
| 45 | + def indexed_fields | |
| 44 | 46 | self::SEARCHABLE_FIELDS.merge self.control_fields |
| 45 | 47 | end |
| 48 | + | |
| 46 | 49 | end |
| 47 | 50 | |
| 48 | 51 | end | ... | ... |
plugins/elasticsearch/lib/ext/community.rb
| ... | ... | @@ -3,12 +3,7 @@ require_relative '../elasticsearch_indexed_model' |
| 3 | 3 | |
| 4 | 4 | class Community |
| 5 | 5 | def self.control_fields |
| 6 | - {:name => {type: "string", analyzer: "english", fields: { | |
| 7 | - raw:{ | |
| 8 | - type: "string", | |
| 9 | - index: "not_analyzed" | |
| 10 | - } | |
| 11 | - }}} | |
| 6 | + {} | |
| 12 | 7 | end |
| 13 | 8 | include ElasticsearchIndexedModel |
| 14 | 9 | end | ... | ... |
plugins/elasticsearch/lib/ext/event.rb
plugins/elasticsearch/lib/ext/person.rb
| ... | ... | @@ -6,12 +6,7 @@ class Person |
| 6 | 6 | { |
| 7 | 7 | :visible => {type: 'boolean'}, |
| 8 | 8 | :public_profile => {type: 'boolean'}, |
| 9 | - :name => {type: "string", fields: { | |
| 10 | - raw:{ | |
| 11 | - type: "string", | |
| 12 | - index: "not_analyzed" | |
| 13 | - } | |
| 14 | - }}} | |
| 9 | + } | |
| 15 | 10 | end |
| 16 | 11 | include ElasticsearchIndexedModel |
| 17 | 12 | end | ... | ... |
plugins/elasticsearch/test/unit/controllers/elasticsearch_plugin_controller_test.rb
| ... | ... | @@ -15,13 +15,13 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase |
| 15 | 15 | |
| 16 | 16 | def create_people |
| 17 | 17 | 5.times do | index | |
| 18 | - create_user "person_#{index}" | |
| 18 | + create_user "person #{index}" | |
| 19 | 19 | end |
| 20 | 20 | end |
| 21 | 21 | |
| 22 | 22 | def create_communities |
| 23 | 23 | 6.times do | index | |
| 24 | - fast_create Community, name: "community_#{index}", created_at: Date.new | |
| 24 | + fast_create Community, name: "community #{index}", created_at: Date.new | |
| 25 | 25 | end |
| 26 | 26 | end |
| 27 | 27 | |
| ... | ... | @@ -54,31 +54,31 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase |
| 54 | 54 | end |
| 55 | 55 | |
| 56 | 56 | should 'return results filtered by query' do |
| 57 | - get :index, { 'query' => "person_"} | |
| 57 | + get :index, { 'query' => "person"} | |
| 58 | 58 | assert_response :success |
| 59 | 59 | assert_select ".search-item", 5 |
| 60 | 60 | assert_template partial: '_person_display' |
| 61 | 61 | end |
| 62 | 62 | |
| 63 | - should 'return results filtered by query with uppercase' do | |
| 64 | - get :index, {'query' => "PERSON_1"} | |
| 65 | - assert_response :success | |
| 66 | - assert_select ".search-item", 1 | |
| 67 | - assert_template partial: '_person_display' | |
| 68 | - end | |
| 63 | + should 'return results filtered by query with uppercase' do | |
| 64 | + get :index, {'query' => "PERSON 1"} | |
| 65 | + assert_response :success | |
| 66 | + assert_template partial: '_person_display' | |
| 67 | + assert_tag(tag: "div", attributes: { class: "person-item" } , descendant: { tag: "a", child: "person 1"} ) | |
| 68 | + end | |
| 69 | 69 | |
| 70 | - should 'return results filtered by query with downcase' do | |
| 71 | - get :index, {'query' => "person_1"} | |
| 72 | - assert_response :success | |
| 73 | - assert_select ".search-item", 1 | |
| 74 | - end | |
| 70 | + should 'return results filtered by query with downcase' do | |
| 71 | + get :index, {'query' => "person 1"} | |
| 72 | + assert_response :success | |
| 73 | + assert_tag(tag: "div", attributes: { class: "person-item" } , descendant: { tag: "a", child: "person 1"} ) | |
| 74 | + end | |
| 75 | 75 | |
| 76 | 76 | should 'return new person indexed' do |
| 77 | 77 | get :index, { "selected_type" => :community} |
| 78 | 78 | assert_response :success |
| 79 | 79 | assert_select ".search-item", 6 |
| 80 | 80 | |
| 81 | - fast_create Community, name: "community_#{7}", created_at: Date.new | |
| 81 | + fast_create Community, name: "community #{7}", created_at: Date.new | |
| 82 | 82 | Community.import |
| 83 | 83 | sleep 2 |
| 84 | 84 | |
| ... | ... | @@ -108,7 +108,7 @@ class ElasticsearchPluginControllerTest < ActionController::TestCase |
| 108 | 108 | end |
| 109 | 109 | |
| 110 | 110 | should 'pass params to elastic search controller' do |
| 111 | - get 'index', { query: 'community_' } | |
| 111 | + get 'index', { query: 'community' } | |
| 112 | 112 | assert_not_nil assigns(:results) |
| 113 | 113 | assert_template partial: '_community_display' |
| 114 | 114 | end | ... | ... |