Class: Chewy::Index

Inherits:
Object
  • Object
show all
Includes:
Actions, Aliases, Crutch, Import, Mapping, Observe, Witchcraft, Wrapper, Search
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

Stash::Journal, Stash::Specification

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

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_nameString

Base name for the index. Uses the default value inferred from the class name unless redefined.

Examples:

class Namespace::UsersIndex < Chewy::Index
end
UsersIndex.index_name # => 'users'

Class.new(Chewy::Index).base_name # => raises UndefinedIndex

Returns:

  • (String)

    current base name

Raises:



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 default_import_options(params)
  params.assert_valid_keys(IMPORT_OPTIONS_KEYS)
  self._default_import_options = _default_import_options.merge(params)
end

.derivable_nameString?

Similar to the base_name but respects the class namespace, also, can't be redefined. Used to reference index with the string identifier

Examples:

class Namespace::UsersIndex < Chewy::Index
end
UsersIndex.derivable_name # => 'namespace/users'

Class.new(Chewy::Index).derivable_name # => nil

Returns:

  • (String, nil)

    derivable name or nil when it is impossible to calculate



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

Overloads:

  • .index_name(suggest) ⇒ String

    If suggested name is passed, it is set up as the new base name for the index. Used for the index base name redefinition.

    Examples:

    class UsersIndex < Chewy::Index
      index_name :legacy_users
    end
    UsersIndex.index_name # => 'legacy_users'

    Parameters:

    • suggest (String, Symbol)

      suggested base name

    Returns:

    • (String)

      new base name

  • .index_name(prefix: nil, suffix: nil) ⇒ String

    If suggested name is not passed, returns the base name accompanied with the prefix (if any) and suffix (if passed).

    Examples:

    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'

    Parameters:

    • prefix (String) (defaults to: nil)

      index name prefix, uses prefix method by default

    • suffix (String) (defaults to: nil)

      index name suffix, used for creating several indexes for the same alias during the zero-downtime reset

    Returns:

    • (String)

      result index name

    Raises:



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, 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

.mappings_hashObject



204
205
206
207
# File 'lib/chewy/index.rb', line 204

def mappings_hash
  mappings = root.mappings_hash
  mappings.present? ? {mappings: mappings} : {}
end

.prefixString

Used as a default value for index_name. Return prefix from the configuration but can be redefined per-index to be more dynamic.

Examples:

class UsersIndex < Chewy::Index
  def self.prefix
    'foobar'
  end
end
UsersIndex.index_name # => 'foobar_users'

Returns:

  • (String)

    prefix



141
142
143
# File 'lib/chewy/index.rb', line 141

def prefix
  Chewy.configuration[:prefix]
end

.scopesObject

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_hashObject



200
201
202
# File 'lib/chewy/index.rb', line 200

def settings_hash
  _settings.to_hash
end

.specificationChewy::Index::Specification

Returns a specification object instance for this particular index.

Returns:

See Also:



220
221
222
# File 'lib/chewy/index.rb', line 220

def specification
  @specification ||= Specification.new(self)
end

.specification_hashHash

Returns a hash containing the index settings and mappings Used for the ES index creation as body.

Returns:

  • (Hash)

    specification as a hash

See Also:



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, 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