Class: Chewy::Index

Inherits:
Object show all
Includes:
Actions, Aliases, Search
Defined in:
lib/chewy/index.rb,
lib/chewy/index/actions.rb,
lib/chewy/index/aliases.rb,
lib/chewy/index/settings.rb,
lib/chewy/index/specification.rb

Direct Known Subclasses

Stash::Journal, Stash::Specification

Defined Under Namespace

Modules: Actions, Aliases Classes: Settings, Specification

Class Method Summary collapse

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:



77
78
79
80
81
# File 'lib/chewy/index.rb', line 77

def base_name
  @base_name ||= name.sub(/Index\z/, '').demodulize.underscore if name
  raise UndefinedIndex if @base_name.blank?
  @base_name
end

.build_index_name(*args) ⇒ Object



270
271
272
273
# File 'lib/chewy/index.rb', line 270

def build_index_name(*args)
  ActiveSupport::Deprecation.warn '`Chewy::Index.build_index_name` is deprecated and will be removed soon, use `Chewy::Index.index_name` instead'
  index_name(args.extract_options!)
end

.define_type(target, options = {}, &block) ⇒ Object

Defines type for the index. Arguments depends on adapter used. For ActiveRecord you can pass model or scope and options

class CarsIndex < Chewy::Index define_type Car do ... end # defines VehiclesIndex::Car type end

Type name might be passed in complicated cases:

class VehiclesIndex < Chewy::Index define_type Vehicle.cars.includes(:manufacturer), name: 'cars' do ... end # defines VehiclesIndex::Cars type

define_type Vehicle.motocycles.includes(:manufacturer), name: 'motocycles' do
   ...
end # defines VehiclesIndex::Motocycles type

end

For plain objects:

class PlanesIndex < Chewy::Index define_type :plane do ... end # defines PlanesIndex::Plane type end

The main difference between using plain objects or ActiveRecord models for indexing is import. If you will call CarsIndex::Car.import - it will import all the cars automatically, while PlanesIndex::Plane.import(my_planes) requires import data to be passed.



148
149
150
151
# File 'lib/chewy/index.rb', line 148

def define_type(target, options = {}, &block)
  type_class = Chewy.create_type(self, target, options, &block)
  self.type_hash = type_hash.merge(type_class.type_name => type_class)
end

.derivable_index_nameObject



246
247
248
249
# File 'lib/chewy/index.rb', line 246

def derivable_index_name
  ActiveSupport::Deprecation.warn '`Chewy::Index.derivable_index_name` is deprecated and will be removed soon, use `Chewy::Index.derivable_name` instead'
  derivable_name
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



94
95
96
# File 'lib/chewy/index.rb', line 94

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:



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/chewy/index.rb', line 53

def index_name(suggest = nil, prefix: nil, suffix: nil)
  if suggest
    @base_name = suggest.to_s.presence
  else
    [
      prefix || prefix_with_deprecation,
      base_name,
      suffix
    ].reject(&:blank?).join('_')
  end
end

.index_paramsObject



235
236
237
238
# File 'lib/chewy/index.rb', line 235

def index_params
  ActiveSupport::Deprecation.warn '`Chewy::Index.index_params` is deprecated and will be removed soon, use `Chewy::Index.specification_hash`'
  specification_hash
end

.mappings_hashObject



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

def mappings_hash
  mappings = types.map(&:mappings_hash).inject(:merge)
  mappings.present? ? {mappings: mappings} : {}
end

.method_missing(name, *args, &block) ⇒ Object

Handling old default_prefix if it is not defined.



252
253
254
255
256
257
258
259
# File 'lib/chewy/index.rb', line 252

def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissing
  if name == :default_prefix
    ActiveSupport::Deprecation.warn '`Chewy::Index.default_prefix` is deprecated and will be removed soon, use `Chewy::Index.prefix` instead'
    prefix
  else
    super
  end
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



110
111
112
# File 'lib/chewy/index.rb', line 110

def prefix
  Chewy.configuration[:prefix]
end

.prefix_with_deprecationObject



261
262
263
264
265
266
267
268
# File 'lib/chewy/index.rb', line 261

def prefix_with_deprecation
  if respond_to?(:default_prefix)
    ActiveSupport::Deprecation.warn '`Chewy::Index.default_prefix` is deprecated and will be removed soon, define `Chewy::Index.prefix` method instead'
    default_prefix
  else
    prefix
  end
end

.scopesObject

Returns list of public class methods defined in current index



213
214
215
# File 'lib/chewy/index.rb', line 213

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.



207
208
209
# File 'lib/chewy/index.rb', line 207

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

.settings_hashObject



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

def settings_hash
  _settings.to_hash
end

.specificationChewy::Index::Specification

Returns a specification object instance for this particular index.

Returns:

See Also:



242
243
244
# File 'lib/chewy/index.rb', line 242

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:



231
232
233
# File 'lib/chewy/index.rb', line 231

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

.type(type_name) ⇒ Object

Returns named type:

UserIndex.type('admin') # => UsersIndex::Admin



184
185
186
# File 'lib/chewy/index.rb', line 184

def type(type_name)
  type_hash.fetch(type_name) { raise UndefinedType, "Unknown type in #{name}: #{type_name}" }
end

.type_namesObject

Returns defined types names:

UsersIndex.type_names # => ['admin', 'manager', 'user']



176
177
178
# File 'lib/chewy/index.rb', line 176

def type_names
  type_hash.keys
end

.types(*args) ⇒ Object

Types method has double usage. If no arguments are passed - it returns array of defined types:

UsersIndex.types # => [UsersIndex::Admin, UsersIndex::Manager, UsersIndex::User]

If arguments are passed it treats like a part of chainable query DSL and adds types array for index to select.

UsersIndex.filters { name =~ 'ro' }.types(:admin, :manager) UsersIndex.types(:admin, :manager).filters { name =~ 'ro' } # the same as the first example



164
165
166
167
168
169
170
# File 'lib/chewy/index.rb', line 164

def types(*args)
  if args.present?
    all.types(*args)
  else
    type_hash.values
  end
end