Class: Chewy::Index

Inherits:
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/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

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

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:


103
104
105
106
107
108
# File 'lib/chewy/index.rb', line 103

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


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

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


121
122
123
# File 'lib/chewy/index.rb', line 121

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:


79
80
81
82
83
84
85
86
87
88
89
# File 'lib/chewy/index.rb', line 79

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.


160
161
162
163
164
165
# File 'lib/chewy/index.rb', line 160

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


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

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


137
138
139
# File 'lib/chewy/index.rb', line 137

def prefix
  Chewy.configuration[:prefix]
end

.scopesObject

Returns list of public class methods defined in current index


192
193
194
# File 'lib/chewy/index.rb', line 192

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.


186
187
188
# File 'lib/chewy/index.rb', line 186

def settings(params = {}, &block)
  self._settings = Chewy::Index::Settings.new(params, &block)
end

.settings_hashObject


196
197
198
# File 'lib/chewy/index.rb', line 196

def settings_hash
  _settings.to_hash
end

.specificationChewy::Index::Specification

Returns a specification object instance for this particular index.

Returns:

See Also:


216
217
218
# File 'lib/chewy/index.rb', line 216

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:


210
211
212
# File 'lib/chewy/index.rb', line 210

def specification_hash
  [settings_hash, mappings_hash].inject(:merge)
end