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/search.rb,
lib/chewy/railtie.rb,
lib/chewy/runtime.rb,
lib/chewy/version.rb,
lib/chewy/strategy.rb,
lib/chewy/repository.rb,
lib/chewy/fields/base.rb,
lib/chewy/fields/root.rb,
lib/chewy/type/import.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/query/scoping.rb,
lib/chewy/strategy/base.rb,
lib/chewy/index/settings.rb,
lib/chewy/log_subscriber.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/strategy/atomic.rb,
lib/chewy/strategy/bypass.rb,
lib/chewy/strategy/urgent.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/type/adapter/orm.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/type/adapter/mongoid.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,
lib/chewy/query/pagination/will_paginate.rb

Defined Under Namespace

Modules: Fields, Generators, Runtime, Search Classes: Config, DocumentNotFound, Error, ImportFailed, Index, LogSubscriber, Query, Railtie, Repository, Strategy, Type, UndefinedIndex, UndefinedType, UndefinedUpdateStrategy, UnderivableType

Constant Summary collapse

VERSION =
'0.7.0'

Class Method Summary collapse

Class Method Details

.atomic(&block) ⇒ Object



179
180
181
182
# File 'lib/chewy.rb', line 179

def atomic &block
  ActiveSupport::Deprecation.warn('`Chewy.atomic` block is deprecated and will be removed soon, use `Chewy.strategy(:atomic)` block instead')
  strategy(:atomic, &block)
end

.clientObject

Main elasticsearch-ruby client instance



112
113
114
# File 'lib/chewy.rb', line 112

def client
  Thread.current[:chewy_client] ||= ::Elasticsearch::Client.new configuration
end

.configObject



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

def config
  Chewy::Config.instance
end

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

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



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/chewy.rb', line 91

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

  adapter = if defined?(::ActiveRecord::Base) && ((target.is_a?(Class) && target < ::ActiveRecord::Base) || target.is_a?(::ActiveRecord::Relation))
    Chewy::Type::Adapter::ActiveRecord.new(target, options)
  elsif defined?(::Mongoid::Document) && ((target.is_a?(Class) && target.ancestors.include?(::Mongoid::Document)) || target.is_a?(::Mongoid::Criteria))
    Chewy::Type::Adapter::Mongoid.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.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/chewy.rb', line 73

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.



128
129
130
131
# File 'lib/chewy.rb', line 128

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

.repositoryObject



189
190
191
# File 'lib/chewy.rb', line 189

def repository
  Chewy::Repository.instance
end

.strategy(name = nil, &block) ⇒ Object

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


157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/chewy.rb', line 157

def strategy name = nil, &block
  Thread.current[:chewy_strategy] ||= Chewy::Strategy.new
  if name
    if block
      Thread.current[:chewy_strategy].wrap name, &block
    else
      Thread.current[:chewy_strategy].push name
    end
  else
    Thread.current[:chewy_strategy]
  end
end

.urgent_update=(value) ⇒ Object



170
171
172
173
174
175
176
177
# File 'lib/chewy.rb', line 170

def urgent_update= value
  ActiveSupport::Deprecation.warn('`Chewy.urgent_update = value` is deprecated and will be removed soon, use `Chewy.strategy(:urgent)` block instead')
  if value
    strategy(:urgent)
  else
    strategy.pop
  end
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.



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

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