module Chewy

Constants

VERSION

Attributes

adapters[RW]

Public Class Methods

client() click to toggle source

Main elasticsearch-ruby client instance

# File lib/chewy.rb, line 98
def client
  Chewy.current[:chewy_client] ||= begin
    client_configuration = configuration.deep_dup
    client_configuration.delete(:prefix) # used by Chewy, not relevant to Elasticsearch::Client
    block = client_configuration[:transport_options].try(:delete, :proc)
    ::Elasticsearch::Client.new(client_configuration, &block)
  end
end
config() click to toggle source
# File lib/chewy.rb, line 163
def config
  Chewy::Config.instance
end
create_indices() click to toggle source
# File lib/chewy.rb, line 173
def create_indices
  Chewy::Index.descendants.each(&:create)
end
create_indices!() click to toggle source
# File lib/chewy.rb, line 177
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 67
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 85
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 181
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 121
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 168
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 150
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 112
def wait_for_status
  if Chewy.configuration[:wait_for_status].present?
    client.cluster.health wait_for_status: Chewy.configuration[:wait_for_status]
  end
end