class Chewy::Index
Constants
- IMPORT_OPTIONS_KEYS
- STRATEGY_OPTIONS
Public Class Methods
Base name for the index. Uses the default value inferred from the class name unless redefined.
@example
class Namespace::UsersIndex < Chewy::Index end UsersIndex.index_name # => 'users' Class.new(Chewy::Index).base_name # => raises UndefinedIndex
@raise [UndefinedIndex] when the base name is blank @return [String] current base name
# 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
# File lib/chewy/index.rb, line 224 def default_import_options(params) params.assert_valid_keys(IMPORT_OPTIONS_KEYS) self._default_import_options = _default_import_options.merge(params) end
Similar to the {.base_name} but respects the class namespace, also, can’t be redefined. Used to reference index with the string identifier
@example
class Namespace::UsersIndex < Chewy::Index end UsersIndex.derivable_name # => 'namespace/users' Class.new(Chewy::Index).derivable_name # => nil
@return [String, nil] derivable name or nil when it is impossible to calculate
# File lib/chewy/index.rb, line 125 def derivable_name @derivable_name ||= name.sub(/Index\z/, '').underscore if name end
@overload index_name
(suggest)
If suggested name is passed, it is set up as the new base name for the index. Used for the index base name redefinition. @example class UsersIndex < Chewy::Index index_name :legacy_users end UsersIndex.index_name # => 'legacy_users' @param suggest [String, Symbol] suggested base name @return [String] new base name
@overload index_name
(prefix: nil, suffix: nil)
If suggested name is not passed, returns the base name accompanied with the prefix (if any) and suffix (if passed). @example class UsersIndex < Chewy::Index end Chewy.settings = {prefix: 'test'} UsersIndex.index_name # => 'test_users' UsersIndex.index_name(prefix: 'foobar') # => 'foobar_users' UsersIndex.index_name(suffix: '2017') # => 'test_users_2017' UsersIndex.index_name(prefix: '', suffix: '2017') # => 'users_2017' @param prefix [String] index name prefix, uses {.prefix} method by default @param suffix [String] index name suffix, used for creating several indexes for the same alias during the zero-downtime reset @raise [UndefinedIndex] if the base name is blank @return [String] result index name
# 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
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.
# File lib/chewy/index.rb, line 164 def index_scope(target, options = {}) raise 'Index scope is already defined' if index_scope_defined? self.adapter = Chewy.adapters.find { |klass| klass.accepts?(target) }.new(target, **options) self.index_scope_defined = true end
# File lib/chewy/index.rb, line 204 def mappings_hash mappings = root.mappings_hash mappings.present? ? {mappings: mappings} : {} end
Used as a default value for {.index_name}. Return prefix from the configuration but can be redefined per-index to be more dynamic.
@example
class UsersIndex < Chewy::Index def self.prefix 'foobar' end end UsersIndex.index_name # => 'foobar_users'
@return [String] prefix
# File lib/chewy/index.rb, line 141 def prefix Chewy.configuration[:prefix] end
Returns list of public class methods defined in current index
# File lib/chewy/index.rb, line 196 def scopes public_methods - Chewy::Index.public_methods end
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 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.
# File lib/chewy/index.rb, line 190 def settings(params = {}, &block) self._settings = Chewy::Index::Settings.new(params, &block) end
# File lib/chewy/index.rb, line 200 def settings_hash _settings.to_hash end
@see Chewy::Index::Specification
@return [Chewy::Index::Specification] a specification object instance for this particular index
# File lib/chewy/index.rb, line 220 def specification @specification ||= Specification.new(self) end
Returns a hash containing the index settings and mappings Used for the ES index creation as body.
@see Chewy::Index::Specification
@return [Hash] specification as a hash
# File lib/chewy/index.rb, line 214 def specification_hash [settings_hash, mappings_hash].inject(:merge) end
# 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, options), res| res[strategy] = case strategy when :delayed_sidekiq Struct.new(*STRATEGY_OPTIONS[strategy]).new.tap do |config| options.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