module Chewy

The class is responsible for accumulating in redis [type, ids] that were requested to be reindexed during ‘latency` seconds. The reindex job is going to be scheduled after a `latency` seconds. that job is going to read accumulated [type, ids] from the redis and reindex all them at once.

Constants

VERSION

Attributes

adapters[RW]

Public Class Methods

client() click to toggle source

Main elasticsearch-ruby client instance

# File lib/chewy.rb, line 100
def client
  Chewy.current[:chewy_client] ||= Chewy::ElasticClient.new
end
config() click to toggle source
# File lib/chewy.rb, line 160
def config
  Chewy::Config.instance
end
create_indices() click to toggle source
# File lib/chewy.rb, line 170
def create_indices
  Chewy::Index.descendants.each(&:create)
end
create_indices!() click to toggle source
# File lib/chewy.rb, line 174
def create_indices!
  Chewy::Index.descendants.each(&:create!)
end
current() click to toggle source

A thread-local variables accessor @return [Hash]

# File lib/chewy.rb, line 69
def current
  unless Thread.current.thread_variable?(:chewy)
    Thread.current.thread_variable_set(:chewy, {})
  end

  Thread.current.thread_variable_get(:chewy)
end
delete_all()
Alias for: massacre
derive_name(index_name) click to toggle source

Derives an index for the passed string identifier if possible.

@example

Chewy.derive_name(UsersIndex) # => UsersIndex
Chewy.derive_name('namespace/users') # => Namespace::UsersIndex
Chewy.derive_name('missing') # => raises Chewy::UndefinedIndex

@param index_name [String, Chewy::Index] index identifier or class @raise [Chewy::UndefinedIndex] in cases when it is impossible to find index @return [Chewy::Index]

# File lib/chewy.rb, line 87
def derive_name(index_name)
  return index_name if index_name.is_a?(Class) && index_name < Chewy::Index

  class_name = "#{index_name.camelize.gsub(/Index\z/, '')}Index"
  index = class_name.safe_constantize

  return index if index && index < Chewy::Index

  raise Chewy::UndefinedIndex, "Can not find index named `#{class_name}`"
end
eager_load!() click to toggle source
# File lib/chewy.rb, line 178
def eager_load!
  return unless defined?(Chewy::Railtie)

  dirs = Chewy::Railtie.all_engines.map do |engine|
    engine.paths[Chewy.configuration[:indices_path]]
  end.compact.map(&:existent).flatten.uniq

  dirs.each do |dir|
    Dir.glob(File.join(dir, '**/*.rb')).each do |file|
      require_dependency file
    end
  end
end
massacre() click to toggle source

Deletes all corresponding indexes with current prefix from ElasticSearch. Be careful, if current prefix is blank, this will destroy all the indexes.

# File lib/chewy.rb, line 118
def massacre
  Chewy.client.indices.delete(index: [Chewy.configuration[:prefix], '*'].reject(&:blank?).join('_'))
  Chewy.wait_for_status
end
Also aliased as: delete_all
msearch(queries) click to toggle source
# File lib/chewy/multi_search.rb, line 59
def self.msearch(queries)
  Chewy::MultiSearch.new(queries)
end
repository() click to toggle source
# File lib/chewy.rb, line 165
def repository
  Chewy::Repository.instance
end
strategy(name = nil, &block) click to toggle source

Strategies are designed to allow nesting, so it is possible to redefine it for nested contexts.

Chewy.strategy(:atomic) do
  city1.do_update!
  Chewy.strategy(:urgent) do
    city2.do_update!
    city3.do_update!
    # there will be 2 update index requests for city2 and city3
  end
  city4..do_update!
  # city1 and city4 will be grouped in one index update request
end

It is possible to nest strategies without blocks:

Chewy.strategy(:urgent)
city1.do_update! # index updated
Chewy.strategy(:bypass)
city2.do_update! # update bypassed
Chewy.strategy.pop
city3.do_update! # index updated again
# File lib/chewy.rb, line 147
def strategy(name = nil, &block)
  Chewy.current[:chewy_strategy] ||= Chewy::Strategy.new
  if name
    if block
      Chewy.current[:chewy_strategy].wrap name, &block
    else
      Chewy.current[:chewy_strategy].push name
    end
  else
    Chewy.current[:chewy_strategy]
  end
end
wait_for_status() click to toggle source

Sends wait_for_status request to ElasticSearch with status defined in configuration.

Does nothing in case of config ‘wait_for_status` is undefined.

# File lib/chewy.rb, line 109
def wait_for_status
  if Chewy.configuration[:wait_for_status].present?
    client.cluster.health wait_for_status: Chewy.configuration[:wait_for_status]
  end
end