Module: Chewy

Defined in:
lib/chewy.rb,
lib/chewy/type.rb,
lib/chewy/index.rb,
lib/chewy/query.rb,
lib/chewy/config.rb,
lib/chewy/errors.rb,
lib/chewy/railtie.rb,
lib/chewy/runtime.rb,
lib/chewy/version.rb,
lib/chewy/fields/base.rb,
lib/chewy/fields/root.rb,
lib/chewy/type/import.rb,
lib/chewy/index/search.rb,
lib/chewy/type/actions.rb,
lib/chewy/type/mapping.rb,
lib/chewy/type/observe.rb,
lib/chewy/type/wrapper.rb,
lib/chewy/index/actions.rb,
lib/chewy/index/aliases.rb,
lib/chewy/query/compose.rb,
lib/chewy/query/filters.rb,
lib/chewy/query/loading.rb,
lib/chewy/index/settings.rb,
lib/chewy/query/criteria.rb,
lib/chewy/query/nodes/or.rb,
lib/chewy/query/nodes/and.rb,
lib/chewy/query/nodes/not.rb,
lib/chewy/query/nodes/raw.rb,
lib/chewy/runtime/version.rb,
lib/chewy/query/nodes/base.rb,
lib/chewy/query/nodes/bool.rb,
lib/chewy/query/nodes/expr.rb,
lib/chewy/query/pagination.rb,
lib/chewy/query/nodes/equal.rb,
lib/chewy/query/nodes/field.rb,
lib/chewy/query/nodes/query.rb,
lib/chewy/query/nodes/range.rb,
lib/chewy/type/adapter/base.rb,
lib/chewy/query/nodes/exists.rb,
lib/chewy/query/nodes/prefix.rb,
lib/chewy/query/nodes/regexp.rb,
lib/chewy/query/nodes/script.rb,
lib/chewy/query/nodes/missing.rb,
lib/chewy/type/adapter/object.rb,
lib/chewy/query/nodes/has_child.rb,
lib/chewy/query/nodes/match_all.rb,
lib/chewy/query/nodes/has_parent.rb,
lib/chewy/query/nodes/has_relation.rb,
lib/chewy/query/pagination/kaminari.rb,
lib/chewy/type/adapter/active_record.rb,
lib/generators/chewy/install_generator.rb

Defined Under Namespace

Modules: Fields, Generators, Runtime Classes: Config, Error, ImportFailed, Index, Query, Railtie, Type, UndefinedIndex, UndefinedType, UnderivableType

Constant Summary collapse

VERSION =
'0.5.2'

Class Method Summary collapse

Class Method Details

.configObject



97
98
99
# File 'lib/chewy.rb', line 97

def config
  Chewy::Config.instance
end

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

Creates Chewy::Type ancestor defining index and adapter methods.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/chewy.rb', line 62

def create_type index, target, options = {}, &block
  type = Class.new(Chewy::Type)

  adapter = if (target.is_a?(Class) && target < ActiveRecord::Base) || target.is_a?(::ActiveRecord::Relation)
    Chewy::Type::Adapter::ActiveRecord.new(target, options)
  else
    Chewy::Type::Adapter::Object.new(target, options)
  end

  index.const_set(adapter.name, type)
  type.send(:define_singleton_method, :index) { index }
  type.send(:define_singleton_method, :adapter) { adapter }

  type.class_eval &block if block
  type
end

.derive_type(name) ⇒ Object

Derives type from string ‘index#type` representation:

Chewy.derive_type('users#user') # => UsersIndex::User

If index has only one type - it is possible to derive it without specification:

Chewy.derive_type('users') # => UsersIndex::User

If index has more then one type - it raises Chewy::UnderivableType.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/chewy.rb', line 44

def derive_type name
  return name if name.is_a?(Class) && name < Chewy::Type

  index_name, type_name = name.split('#', 2)
  class_name = "#{index_name.camelize}Index"
  index = class_name.safe_constantize
  raise Chewy::UnderivableType.new("Can not find index named `#{class_name}`") unless index && index < Chewy::Index
  type = if type_name.present?
    index.type_hash[type_name] or raise Chewy::UnderivableType.new("Index `#{class_name}` doesn`t have type named `#{type_name}`")
  elsif index.types.one?
    index.types.first
  else
    raise Chewy::UnderivableType.new("Index `#{class_name}` has more than one type, please specify type via `#{index_name}#type_name`")
  end
end

.massacreObject Also known as: delete_all

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



91
92
93
94
# File 'lib/chewy.rb', line 91

def massacre
  Chewy.client.indices.delete(index: [Chewy.configuration[:prefix], '*'].delete_if(&:blank?).join(?_))
  Chewy.wait_for_status
end

.wait_for_statusObject

Sends wait_for_status request to ElasticSearch with status defined in configuration.

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



84
85
86
# File 'lib/chewy.rb', line 84

def wait_for_status
  client.cluster.health wait_for_status: Chewy.configuration[:wait_for_status] if Chewy.configuration[:wait_for_status].present?
end