Module: Chewy

Defined in:
lib/chewy.rb,
lib/chewy/type.rb,
lib/chewy/index.rb,
lib/chewy/query.rb,
lib/chewy/stash.rb,
lib/chewy/config.rb,
lib/chewy/errors.rb,
lib/chewy/search.rb,
lib/chewy/journal.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/rake_helper.rb,
lib/chewy/type/crutch.rb,
lib/chewy/type/import.rb,
lib/chewy/type/syncer.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/search/loader.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/search/request.rb,
lib/chewy/search/scoping.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/search/response.rb,
lib/chewy/strategy/atomic.rb,
lib/chewy/strategy/bypass.rb,
lib/chewy/strategy/resque.rb,
lib/chewy/strategy/urgent.rb,
lib/chewy/type/witchcraft.rb,
lib/chewy/minitest/helpers.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/search/scrolling.rb,
lib/chewy/strategy/sidekiq.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/search/parameters.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/search/query_proxy.rb,
lib/chewy/strategy/shoryuken.rb,
lib/chewy/index/specification.rb,
lib/chewy/query/nodes/missing.rb,
lib/chewy/strategy/active_job.rb,
lib/chewy/type/adapter/object.rb,
lib/chewy/type/adapter/sequel.rb,
lib/chewy/type/import/routine.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/search/parameters/aggs.rb,
lib/chewy/search/parameters/load.rb,
lib/chewy/search/parameters/none.rb,
lib/chewy/search/parameters/limit.rb,
lib/chewy/search/parameters/order.rb,
lib/chewy/search/parameters/query.rb,
lib/chewy/search/parameters/types.rb,
lib/chewy/query/nodes/has_relation.rb,
lib/chewy/search/parameters/filter.rb,
lib/chewy/search/parameters/offset.rb,
lib/chewy/search/parameters/source.rb,
lib/chewy/type/import/bulk_builder.rb,
lib/chewy/type/import/bulk_request.rb,
lib/chewy/search/parameters/explain.rb,
lib/chewy/search/parameters/indices.rb,
lib/chewy/search/parameters/profile.rb,
lib/chewy/search/parameters/rescore.rb,
lib/chewy/search/parameters/storage.rb,
lib/chewy/search/parameters/suggest.rb,
lib/chewy/search/parameters/timeout.rb,
lib/chewy/search/parameters/version.rb,
lib/chewy/search/pagination/kaminari.rb,
lib/chewy/type/adapter/active_record.rb,
lib/chewy/search/parameters/highlight.rb,
lib/chewy/search/parameters/min_score.rb,
lib/chewy/type/import/journal_builder.rb,
lib/chewy/search/parameters/preference.rb,
lib/generators/chewy/install_generator.rb,
lib/chewy/search/parameters/post_filter.rb,
lib/chewy/search/parameters/search_type.rb,
lib/chewy/search/parameters/search_after.rb,
lib/chewy/search/parameters/track_scores.rb,
lib/chewy/search/pagination/will_paginate.rb,
lib/chewy/search/parameters/indices_boost.rb,
lib/chewy/search/parameters/request_cache.rb,
lib/chewy/search/parameters/script_fields.rb,
lib/chewy/search/parameters/stored_fields.rb,
lib/chewy/search/parameters/docvalue_fields.rb,
lib/chewy/search/parameters/terminate_after.rb,
lib/chewy/search/parameters/concerns/bool_storage.rb,
lib/chewy/search/parameters/concerns/hash_storage.rb,
lib/chewy/search/parameters/concerns/query_storage.rb,
lib/chewy/search/parameters/concerns/string_storage.rb,
lib/chewy/search/parameters/concerns/integer_storage.rb,
lib/chewy/search/parameters/allow_partial_search_results.rb,
lib/chewy/search/parameters/concerns/string_array_storage.rb

Defined Under Namespace

Modules: Fields, Generators, Minitest, RakeHelper, Runtime, Search, Stash Classes: Config, DocumentNotFound, Error, ImportFailed, Index, Journal, LogSubscriber, PluginMissing, Query, Railtie, RemovedFeature, Repository, Strategy, Type, UndefinedIndex, UndefinedType, UndefinedUpdateStrategy, UnderivableType

Constant Summary collapse

VERSION =
'5.2.0'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.adaptersObject

Returns the value of attribute adapters.



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

def adapters
  @adapters
end

Class Method Details

.clientObject

Main elasticsearch-ruby client instance



147
148
149
150
151
152
153
154
# File 'lib/chewy.rb', line 147

def client
  Thread.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

.configObject



210
211
212
# File 'lib/chewy.rb', line 210

def config
  Chewy::Config.instance
end

.create_indicesObject



220
221
222
# File 'lib/chewy.rb', line 220

def create_indices
  Chewy::Index.descendants.each(&:create)
end

.create_indices!Object



224
225
226
# File 'lib/chewy.rb', line 224

def create_indices!
  Chewy::Index.descendants.each(&:create!)
end

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

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



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/chewy.rb', line 132

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

  adapter = adapters.find { |klass| klass.accepts?(target) }.new(target, **options)

  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) ⇒ Chewy::Type

Derives a single type for the passed string identifier if possible.

Examples:

Chewy.derive_types(UsersIndex::User) # => UsersIndex::User
Chewy.derive_types('namespace/users') # => Namespace::UsersIndex::User
Chewy.derive_types('places') # => raises Chewy::UnderivableType
Chewy.derive_types('places#city') # => PlacesIndex::City

Parameters:

  • name (String, Chewy::Type)

    string type identifier

Returns:

Raises:

  • (Chewy::UnderivableType)

    in cases when it is impossble to find index or type or more than one type found



97
98
99
100
101
102
103
# File 'lib/chewy.rb', line 97

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

  types = derive_types(name)
  raise Chewy::UnderivableType, "Index `#{types.first.index}` has more than one type, please specify type via `#{types.first.index.derivable_name}#type_name`" unless types.one?
  types.first
end

.derive_types(from) ⇒ Array<Chewy::Type>

Derives all the types for the passed string identifier if possible.

Examples:

Chewy.derive_types('namespace/users') # => [Namespace::UsersIndex::User]
Chewy.derive_types('places') # => [PlacesIndex::City, PlacesIndex::Country]
Chewy.derive_types('places#city') # => [PlacesIndex::City]

Parameters:

  • from (String)

    string type identifier

Returns:

Raises:



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/chewy.rb', line 115

def derive_types(from)
  return from.types if from.is_a?(Class) && (from < Chewy::Index || from < Chewy::Type)

  index_name, type_name = from.split('#', 2)
  class_name = "#{index_name.camelize.gsub(/Index\z/, '')}Index"
  index = class_name.safe_constantize
  raise Chewy::UnderivableType, "Can not find index named `#{class_name}`" unless index && index < Chewy::Index
  if type_name.present?
    type = index.type_hash[type_name] or raise Chewy::UnderivableType, "Index `#{class_name}` doesn`t have type named `#{type_name}`"
    [type]
  else
    index.types
  end
end

.eager_load!Object



228
229
230
231
232
233
234
235
236
237
# File 'lib/chewy.rb', line 228

def eager_load!
  return unless defined?(Chewy::Railtie)
  dirs = Chewy::Railtie.all_engines.map { |engine| engine.paths[Chewy.configuration[:indices_path]] }.compact.map(&:existent).flatten.uniq

  dirs.each do |dir|
    Dir.glob(File.join(dir, '**/*.rb')).each do |file|
      require_dependency file
    end
  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.



168
169
170
171
# File 'lib/chewy.rb', line 168

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

.repositoryObject



215
216
217
# File 'lib/chewy.rb', line 215

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



197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/chewy.rb', line 197

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

.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.



161
162
163
# File 'lib/chewy.rb', line 161

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