Module: Chewy

Defined in:
lib/chewy/strategy/delayed_sidekiq/scheduler.rb,
lib/chewy.rb,
lib/chewy/index.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/index/crutch.rb,
lib/chewy/index/import.rb,
lib/chewy/index/syncer.rb,
lib/chewy/multi_search.rb,
lib/chewy/index/actions.rb,
lib/chewy/index/aliases.rb,
lib/chewy/index/mapping.rb,
lib/chewy/index/observe.rb,
lib/chewy/index/wrapper.rb,
lib/chewy/rspec/helpers.rb,
lib/chewy/search/loader.rb,
lib/chewy/strategy/base.rb,
lib/chewy/index/settings.rb,
lib/chewy/log_subscriber.rb,
lib/chewy/search/request.rb,
lib/chewy/search/scoping.rb,
lib/chewy/runtime/version.rb,
lib/chewy/search/response.rb,
lib/chewy/strategy/atomic.rb,
lib/chewy/strategy/bypass.rb,
lib/chewy/strategy/urgent.rb,
lib/chewy/index/witchcraft.rb,
lib/chewy/minitest/helpers.rb,
lib/chewy/search/scrolling.rb,
lib/chewy/strategy/sidekiq.rb,
lib/chewy/index/adapter/orm.rb,
lib/chewy/search/parameters.rb,
lib/chewy/index/adapter/base.rb,
lib/chewy/search/query_proxy.rb,
lib/chewy/index/specification.rb,
lib/chewy/strategy/active_job.rb,
lib/chewy/index/adapter/object.rb,
lib/chewy/index/import/routine.rb,
lib/chewy/strategy/lazy_sidekiq.rb,
lib/chewy/index/observe/callback.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/filter.rb,
lib/chewy/search/parameters/offset.rb,
lib/chewy/search/parameters/source.rb,
lib/chewy/strategy/delayed_sidekiq.rb,
lib/chewy/index/import/bulk_builder.rb,
lib/chewy/index/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/search/parameters/collapse.rb,
lib/chewy/strategy/atomic_no_refresh.rb,
lib/chewy/index/adapter/active_record.rb,
lib/chewy/search/parameters/highlight.rb,
lib/chewy/search/parameters/min_score.rb,
lib/chewy/index/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/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/strategy/delayed_sidekiq/worker.rb,
lib/chewy/search/parameters/docvalue_fields.rb,
lib/chewy/search/parameters/terminate_after.rb,
lib/chewy/search/parameters/track_total_hits.rb,
lib/chewy/index/observe/active_record_methods.rb,
lib/chewy/search/parameters/ignore_unavailable.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

Overview

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.

Defined Under Namespace

Modules: Fields, Generators, Minitest, RakeHelper, Rspec, Runtime, Search, Stash Classes: Config, DocumentNotFound, Error, ImportFailed, Index, InvalidJoinFieldType, Journal, LogSubscriber, MultiSearch, Railtie, Repository, Strategy, UndefinedIndex, UndefinedUpdateStrategy

Constant Summary collapse

VERSION =
'7.3.4'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.adaptersObject

Returns the value of attribute adapters.



63
64
65
# File 'lib/chewy.rb', line 63

def adapters
  @adapters
end

Class Method Details

.clientObject

Main elasticsearch-ruby client instance



98
99
100
101
102
103
104
105
# 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

.configObject



163
164
165
# File 'lib/chewy.rb', line 163

def config
  Chewy::Config.instance
end

.create_indicesObject



173
174
175
# File 'lib/chewy.rb', line 173

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

.create_indices!Object



177
178
179
# File 'lib/chewy.rb', line 177

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

.currentHash

A thread-local variables accessor

Returns:

  • (Hash)


67
68
69
70
71
72
73
# 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

.derive_name(index_name) ⇒ Chewy::Index

Derives an index for the passed string identifier if possible.

Examples:

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

Parameters:

  • index_name (String, Chewy::Index)

    index identifier or class

Returns:

Raises:



85
86
87
88
89
90
91
92
93
94
# 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!Object



181
182
183
184
185
186
187
188
189
190
191
192
193
# 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

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



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

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

.msearch(queries) ⇒ Object



59
60
61
# File 'lib/chewy/multi_search.rb', line 59

def self.msearch(queries)
  Chewy::MultiSearch.new(queries)
end

.repositoryObject



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

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



150
151
152
153
154
155
156
157
158
159
160
161
# 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_statusObject

Sends wait_for_status request to ElasticSearch with status defined in configuration.

Does nothing in case of config wait_for_status is undefined.



112
113
114
115
116
# 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