Commit b614d1559e26ed9f10404ef47483634e57a46352
Committed by
Macartur Sousa
1 parent
002cf525
Exists in
fix_sign_up_form
Elasticsearch: Refactored models
redirect index -> search action removing models extensions that are removed from core renaming elasticsearch helper to a better name (now, working as well) model extensions working with the new helper name check :weight exists refactoring models extensions
Showing
14 changed files
with
85 additions
and
87 deletions
Show diff stats
plugins/elasticsearch/controllers/elasticsearch_plugin_controller.rb
1 | class ElasticsearchPluginController < ApplicationController | 1 | class ElasticsearchPluginController < ApplicationController |
2 | no_design_blocks | 2 | no_design_blocks |
3 | 3 | ||
4 | + def index | ||
5 | + search() | ||
6 | + render :action => 'search' | ||
7 | + end | ||
8 | + | ||
4 | def search | 9 | def search |
5 | @results = [] | 10 | @results = [] |
6 | @query = params[:q] | 11 | @query = params[:q] |
@@ -23,7 +28,13 @@ class ElasticsearchPluginController < ApplicationController | @@ -23,7 +28,13 @@ class ElasticsearchPluginController < ApplicationController | ||
23 | query = {} | 28 | query = {} |
24 | unless text.blank? | 29 | unless text.blank? |
25 | 30 | ||
26 | - fields = klass::SEARCHABLE_FIELDS.map {|k, v| "#{k}^#{v[:weight]}"} | 31 | + fields = klass.indexable_fields.map do |key, value| |
32 | + if value[:weight] | ||
33 | + "#{k}^#{v[:weight]}" | ||
34 | + else | ||
35 | + "#{k}" | ||
36 | + end | ||
37 | + end | ||
27 | 38 | ||
28 | query = { | 39 | query = { |
29 | query: { | 40 | query: { |
plugins/elasticsearch/lib/elasticsearch_helper.rb
@@ -1,31 +0,0 @@ | @@ -1,31 +0,0 @@ | ||
1 | -module INDEXED_MODEL | ||
2 | - include Elasticsearch::Model | ||
3 | - include Elasticsearch::Model::Callbacks | ||
4 | - | ||
5 | - def self.included base | ||
6 | - base.extend ClassMethods | ||
7 | - end | ||
8 | - | ||
9 | - module ClassMethods | ||
10 | -# settings index: { number_of_shards: 1 } do | ||
11 | -# mappings dynamic: 'false' do | ||
12 | -# self::SEARCHABLE_FIELDS.each do |field, value| | ||
13 | -# indexes field | ||
14 | -# end | ||
15 | -# end | ||
16 | -# | ||
17 | -# self.__elasticsearch__.client.indices.delete \ | ||
18 | -# index: self.index_name rescue nil | ||
19 | -# | ||
20 | -# self.__elasticsearch__.client.indices.create \ | ||
21 | -# index: self.index_name, | ||
22 | -# body: { | ||
23 | -# settings: self.settings.to_hash, | ||
24 | -# mappings: self.mappings.to_hash | ||
25 | -# } | ||
26 | -# | ||
27 | -# self.import | ||
28 | -# end | ||
29 | - end | ||
30 | -end | ||
31 | - |
plugins/elasticsearch/lib/elasticsearch_indexed_model.rb
0 → 100644
@@ -0,0 +1,33 @@ | @@ -0,0 +1,33 @@ | ||
1 | +module ElasticsearchIndexedModel | ||
2 | + | ||
3 | + def self.included base | ||
4 | + base.send :include, Elasticsearch::Model | ||
5 | + base.class_eval do | ||
6 | + settings index: { number_of_shards: 1 } do | ||
7 | + mappings dynamic: 'false' do | ||
8 | + base::SEARCHABLE_FIELDS.each do |field, value| | ||
9 | + indexes field | ||
10 | + print '.' | ||
11 | + end | ||
12 | + end | ||
13 | + base.__elasticsearch__.client.indices.delete \ | ||
14 | + index: base.index_name rescue nil | ||
15 | + base.__elasticsearch__.client.indices.create \ | ||
16 | + index: base.index_name, | ||
17 | + body: { | ||
18 | + settings: base.settings.to_hash, | ||
19 | + mappings: base.mappings.to_hash | ||
20 | + } | ||
21 | + end | ||
22 | + end | ||
23 | + base.extend ClassMethods | ||
24 | + base.send :import | ||
25 | + end | ||
26 | + | ||
27 | + module ClassMethods | ||
28 | + def indexable_fields | ||
29 | + self::SEARCHABLE_FIELDS.keys + self.control_fields | ||
30 | + end | ||
31 | + end | ||
32 | + | ||
33 | +end |
plugins/elasticsearch/lib/ext/article.rb
1 | require_dependency 'article' | 1 | require_dependency 'article' |
2 | -require_relative '../elasticsearch_helper' | 2 | +require_relative '../elasticsearch_indexed_model' |
3 | 3 | ||
4 | class Article | 4 | class Article |
5 | - include INDEXED_MODEL | 5 | + include ElasticsearchIndexedModel |
6 | 6 | ||
7 | def self.control_fields | 7 | def self.control_fields |
8 | - %w(advertise published).map{ |e| e.to_sym } | 8 | + [ |
9 | + :advertise, | ||
10 | + :published, | ||
11 | + ] | ||
9 | end | 12 | end |
10 | - | ||
11 | - def self.indexable_fields | ||
12 | - SEARCHABLE_FIELDS.keys + self.control_fields | ||
13 | - end | ||
14 | - | ||
15 | end | 13 | end |
plugins/elasticsearch/lib/ext/category.rb
1 | require_dependency 'category' | 1 | require_dependency 'category' |
2 | +require_relative '../elasticsearch_indexed_model' | ||
2 | 3 | ||
3 | class Category | 4 | class Category |
5 | + include ElasticsearchIndexedModel | ||
6 | + | ||
4 | def self.control_fields | 7 | def self.control_fields |
5 | - %w() | 8 | + [] |
6 | end | 9 | end |
7 | 10 | ||
8 | - require_relative '../elasticsearch_helper' | ||
9 | end | 11 | end |
plugins/elasticsearch/lib/ext/certifier.rb
plugins/elasticsearch/lib/ext/comment.rb
1 | require_dependency 'comment' | 1 | require_dependency 'comment' |
2 | +require_relative '../elasticsearch_indexed_model' | ||
2 | 3 | ||
3 | class Comment | 4 | class Comment |
5 | + include ElasticsearchIndexedModel | ||
6 | + | ||
4 | def self.control_fields | 7 | def self.control_fields |
5 | - %w() | 8 | + [] |
6 | end | 9 | end |
7 | - | ||
8 | - require_relative '../elasticsearch_helper' | ||
9 | end | 10 | end |
plugins/elasticsearch/lib/ext/license.rb
1 | require_dependency 'license.rb' | 1 | require_dependency 'license.rb' |
2 | +require_relative '../elasticsearch_indexed_model' | ||
2 | 3 | ||
3 | class License | 4 | class License |
5 | + include ElasticsearchIndexedModel | ||
6 | + | ||
4 | def self.control_fields | 7 | def self.control_fields |
5 | - %w() | 8 | + [] |
6 | end | 9 | end |
7 | - | ||
8 | - require_relative '../elasticsearch_helper' | ||
9 | end | 10 | end |
plugins/elasticsearch/lib/ext/national_region.rb
1 | require_dependency 'national_region' | 1 | require_dependency 'national_region' |
2 | +require_relative '../elasticsearch_indexed_model' | ||
2 | 3 | ||
3 | class NationalRegion | 4 | class NationalRegion |
5 | + include ElasticsearchIndexedModel | ||
6 | + | ||
4 | def self.control_fields | 7 | def self.control_fields |
5 | - %w() | 8 | + [] |
6 | end | 9 | end |
7 | - | ||
8 | - require_relative '../elasticsearch_helper' | ||
9 | end | 10 | end |
plugins/elasticsearch/lib/ext/product.rb
plugins/elasticsearch/lib/ext/profile.rb
1 | require_dependency 'profile' | 1 | require_dependency 'profile' |
2 | +require_relative '../elasticsearch_indexed_model' | ||
2 | 3 | ||
3 | class Profile | 4 | class Profile |
5 | + include ElasticsearchIndexedModel | ||
6 | + | ||
4 | def self.control_fields | 7 | def self.control_fields |
5 | - %w( visible public_profile ) | 8 | + [ |
9 | + :visible, | ||
10 | + :public_profile, | ||
11 | + ] | ||
6 | end | 12 | end |
7 | - | ||
8 | - require_relative '../elasticsearch_helper' | ||
9 | end | 13 | end |
plugins/elasticsearch/lib/ext/qualifier.rb
plugins/elasticsearch/lib/ext/scrap.rb
1 | require_dependency 'scrap' | 1 | require_dependency 'scrap' |
2 | +require_relative '../elasticsearch_indexed_model' | ||
2 | 3 | ||
3 | class Scrap | 4 | class Scrap |
5 | + include ElasticsearchIndexedModel | ||
6 | + | ||
4 | def self.control_fields | 7 | def self.control_fields |
5 | - %w(advertise published) | 8 | + [ |
9 | + :advertise, | ||
10 | + :published, | ||
11 | + ] | ||
6 | end | 12 | end |
7 | - | ||
8 | - require_relative '../elasticsearch_helper' | ||
9 | end | 13 | end |
plugins/elasticsearch/lib/ext/user.rb
1 | require_dependency 'user' | 1 | require_dependency 'user' |
2 | +require_relative '../elasticsearch_indexed_model' | ||
2 | 3 | ||
3 | class User | 4 | class User |
5 | + include ElasticsearchIndexedModel | ||
6 | + | ||
4 | def self.control_fields | 7 | def self.control_fields |
5 | - %w() | 8 | + [] |
6 | end | 9 | end |
7 | - | ||
8 | - require_relative '../elasticsearch_helper' | ||
9 | end | 10 | end |