Class: Chewy::Index
- Inherits:
- 
      Object
      
        - Object
- Chewy::Index
 
- Defined in:
- lib/chewy/index.rb,
 lib/chewy/index/crutch.rb,
 lib/chewy/index/import.rb,
 lib/chewy/index/syncer.rb,
 lib/chewy/index/actions.rb,
 lib/chewy/index/aliases.rb,
 lib/chewy/index/mapping.rb,
 lib/chewy/index/observe.rb,
 lib/chewy/index/wrapper.rb,
 lib/chewy/index/settings.rb,
 lib/chewy/index/witchcraft.rb,
 lib/chewy/index/adapter/orm.rb,
 lib/chewy/index/adapter/base.rb,
 lib/chewy/index/specification.rb,
 lib/chewy/index/adapter/object.rb,
 lib/chewy/index/import/routine.rb,
 lib/chewy/index/observe/callback.rb,
 lib/chewy/index/import/bulk_builder.rb,
 lib/chewy/index/import/bulk_request.rb,
 lib/chewy/index/adapter/active_record.rb,
 lib/chewy/index/import/journal_builder.rb,
 lib/chewy/index/observe/active_record_methods.rb
Direct Known Subclasses
Defined Under Namespace
Modules: Actions, Adapter, Aliases, Crutch, Import, Mapping, Observe, Witchcraft, Wrapper Classes: Settings, Specification, Syncer
Constant Summary collapse
- IMPORT_OPTIONS_KEYS =
- %i[ batch_size bulk_size consistency direct_import journal pipeline raw_import refresh replication ].freeze 
- STRATEGY_OPTIONS =
- { delayed_sidekiq: %i[latency margin ttl reindex_wrapper] }.freeze 
Constants included from Import
Import::IMPORT_WORKER, Import::LEFTOVERS_WORKER
Class Method Summary collapse
- 
  
    
      .base_name  ⇒ String 
    
    
  
  
  
  
  
  
  
  
  
    Base name for the index. 
- .default_import_options(params) ⇒ Object
- 
  
    
      .derivable_name  ⇒ String? 
    
    
  
  
  
  
  
  
  
  
  
    Similar to the Index.base_name but respects the class namespace, also, can't be redefined. 
- .index_name(suggest = nil, prefix: nil, suffix: nil) ⇒ Object
- 
  
    
      .index_scope(target, options = {})  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Defines scope and options for the index. 
- .mappings_hash ⇒ Object
- 
  
    
      .prefix  ⇒ String 
    
    
  
  
  
  
  
  
  
  
  
    Used as a default value for Index.index_name. 
- 
  
    
      .scopes  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns list of public class methods defined in current index. 
- 
  
    
      .settings(params = {}, &block)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Used as a part of index definition DSL. 
- .settings_hash ⇒ Object
- 
  
    
      .specification  ⇒ Chewy::Index::Specification 
    
    
  
  
  
  
  
  
  
  
  
    A specification object instance for this particular index. 
- 
  
    
      .specification_hash  ⇒ Hash 
    
    
  
  
  
  
  
  
  
  
  
    Returns a hash containing the index settings and mappings Used for the ES index creation as body. 
- .strategy_config(params = {}) ⇒ Object
Methods included from Wrapper
#==, #initialize, #method_missing, #respond_to_missing?
Methods included from Observe::Helpers
#extract_callback_options!, #update_proc
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Chewy::Index::Wrapper
Class Method Details
.base_name ⇒ String
Base name for the index. Uses the default value inferred from the class name unless redefined.
| 107 108 109 110 111 112 | # File 'lib/chewy/index.rb', line 107 def base_name @base_name ||= name.sub(/Index\z/, '').demodulize.underscore if name raise UndefinedIndex if @base_name.blank? @base_name end | 
.default_import_options(params) ⇒ Object
| 224 225 226 227 | # File 'lib/chewy/index.rb', line 224 def (params) params.assert_valid_keys(IMPORT_OPTIONS_KEYS) self. = .merge(params) end | 
.derivable_name ⇒ String?
Similar to the base_name but respects the class namespace, also, can't be redefined. Used to reference index with the string identifier
| 125 126 127 | # File 'lib/chewy/index.rb', line 125 def derivable_name @derivable_name ||= name.sub(/Index\z/, '').underscore if name end | 
.index_name(suggest) ⇒ String .index_name(prefix: nil, suffix: nil) ⇒ String
| 83 84 85 86 87 88 89 90 91 92 93 | # File 'lib/chewy/index.rb', line 83 def index_name(suggest = nil, prefix: nil, suffix: nil) if suggest @base_name = suggest.to_s.presence else [ prefix || self.prefix, base_name, suffix ].reject(&:blank?).join('_') end end | 
.index_scope(target, options = {}) ⇒ Object
Defines scope and options for the index. Arguments depends on adapter used. For ActiveRecord you can pass model or scope and options
class CarsIndex < Chewy::Index index_scope Car ... end
For plain objects you can completely omit this directive, unless you need to specify some options:
class PlanesIndex < Chewy::Index ... end
The main difference between using plain objects or ActiveRecord models for indexing
is import. If you will call CarsIndex.import - it will import all the cars
automatically, while PlanesIndex.import(my_planes) requires import data to be
passed.
| 164 165 166 167 168 169 | # File 'lib/chewy/index.rb', line 164 def index_scope(target, = {}) raise 'Index scope is already defined' if index_scope_defined? self.adapter = Chewy.adapters.find { |klass| klass.accepts?(target) }.new(target, **) self.index_scope_defined = true end | 
.mappings_hash ⇒ Object
| 204 205 206 207 | # File 'lib/chewy/index.rb', line 204 def mappings_hash mappings = root.mappings_hash mappings.present? ? {mappings: mappings} : {} end | 
.prefix ⇒ String
Used as a default value for index_name. Return prefix from the configuration but can be redefined per-index to be more dynamic.
| 141 142 143 | # File 'lib/chewy/index.rb', line 141 def prefix Chewy.configuration[:prefix] end | 
.scopes ⇒ Object
Returns list of public class methods defined in current index
| 196 197 198 | # File 'lib/chewy/index.rb', line 196 def scopes public_methods - Chewy::Index.public_methods end | 
.settings(params = {}, &block) ⇒ Object
Used as a part of index definition DSL. Defines settings:
class UsersIndex < Chewy::Index settings analysis: { analyzer: { name: { tokenizer: 'standard', filter: ['lowercase', 'icu_folding', 'names_nysiis'] } } } end
See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-update-settings.html for more details
It is possible to store analyzers settings in Chewy repositories
and link them form index class. See Chewy::Index::Settings for details.
| 190 191 192 | # File 'lib/chewy/index.rb', line 190 def settings(params = {}, &block) self._settings = Chewy::Index::Settings.new(params, &block) end | 
.settings_hash ⇒ Object
| 200 201 202 | # File 'lib/chewy/index.rb', line 200 def settings_hash _settings.to_hash end | 
.specification ⇒ Chewy::Index::Specification
Returns a specification object instance for this particular index.
| 220 221 222 | # File 'lib/chewy/index.rb', line 220 def specification @specification ||= Specification.new(self) end | 
.specification_hash ⇒ Hash
Returns a hash containing the index settings and mappings Used for the ES index creation as body.
| 214 215 216 | # File 'lib/chewy/index.rb', line 214 def specification_hash [settings_hash, mappings_hash].inject(:merge) end | 
.strategy_config(params = {}) ⇒ Object
| 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | # File 'lib/chewy/index.rb', line 229 def strategy_config(params = {}) @strategy_config ||= begin config_struct = Struct.new(*STRATEGY_OPTIONS.keys).new STRATEGY_OPTIONS.each_with_object(config_struct) do |(strategy, ), res| res[strategy] = case strategy when :delayed_sidekiq Struct.new(*STRATEGY_OPTIONS[strategy]).new.tap do |config| .each do |option| config[option] = params.dig(strategy, option) || Chewy.configuration.dig(:strategy_config, strategy, option) end config[:reindex_wrapper] ||= ->(&reindex) { reindex.call } # default wrapper end else raise NotImplementedError, "Unsupported strategy: '#{strategy}'" end end end end |