Class: SearchIndexManager
- Inherits:
-
Object
- Object
- SearchIndexManager
- Defined in:
- app/classes/search_index_manager.rb
Overview
Does not support Elasticsearch version > 5
Constant Summary collapse
- INDEX_NAME =
A single combined index name is used for environments like Heroku where multiple shards are expensive.
"#{Rails.app_class.parent_name.underscore}_#{Rails.env}".freeze
Class Method Summary collapse
- .create_index(options = {}) ⇒ Object
- .delete_index! ⇒ Object
- .import(options = {}) ⇒ Object
-
.indexed_models ⇒ Object
Override this in your host app with an array of all the models you want to search.
- .rebuild ⇒ Object
- .refresh_index ⇒ Object
Class Method Details
.create_index(options = {}) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'app/classes/search_index_manager.rb', line 20 def self.create_index( = {}) settings = indexed_models.map(&:settings).reduce({}, &:merge) mappings = indexed_models.map(&:mappings).reduce({}, &:merge) if [:force] logger.debug { "[Forest] Deleting index #{INDEX_NAME}" } delete_index! end logger.debug { "[Forest] Creating index #{INDEX_NAME} with configuration:" } logger.debug { "[Forest] -- Settings #{settings.inspect}" } logger.debug { "[Forest] -- Mappings #{mappings.inspect}" } create index: INDEX_NAME, body: { settings: settings, mappings: mappings } end |
.delete_index! ⇒ Object
35 36 37 38 39 |
# File 'app/classes/search_index_manager.rb', line 35 def self.delete_index! if exists? index: INDEX_NAME delete index: INDEX_NAME end end |
.import(options = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'app/classes/search_index_manager.rb', line 53 def self.import( = {}) models_to_index = [:models] || indexed_models if (models_to_index - indexed_models).present? raise TypeError, 'The :models option cannot contain non-indexed models' end models_to_index.each do |model| logger.debug { "[Forest] importing model #{model.name}#{' (using `' + model_scope(model).to_s + '` scope)' if model_scope(model).present?}" } model.import batch_size: 500, scope: model_scope(model) end end |
.indexed_models ⇒ Object
Override this in your host app with an array of all the models you want to search. For example:
SearchIndexManager.class_eval do
def self.indexed_models
[Page, Spree::Product]
end
end
16 17 18 |
# File 'app/classes/search_index_manager.rb', line 16 def self.indexed_models [] end |
.rebuild ⇒ Object
47 48 49 50 51 |
# File 'app/classes/search_index_manager.rb', line 47 def self.rebuild = { force: true } create_index() import end |
.refresh_index ⇒ Object
41 42 43 44 45 |
# File 'app/classes/search_index_manager.rb', line 41 def self.refresh_index if exists? index: INDEX_NAME refresh index: INDEX_NAME end end |