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/elastic_client.rb,
lib/chewy/index/compiled.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/search/parameters/knn.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/index/import/progressbar.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/runtime_mappings.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/strategy/delayed_sidekiq/redis_script.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, ElasticClient, Error, FeatureDisabled, ImportFailed, ImportScopeCleanupError, Index, InvalidJoinFieldType, Journal, LogSubscriber, MissingHitsInScrollError, MultiSearch, Railtie, Repository, Strategy, UndefinedIndex, UndefinedUpdateStrategy

Constant Summary collapse

VERSION =
'8.4.0'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.adaptersObject

Returns the value of attribute adapters.



66
67
68
# File 'lib/chewy.rb', line 66

def adapters
  @adapters
end

Class Method Details

.clientObject

Main elasticsearch-ruby client instance



101
102
103
# File 'lib/chewy.rb', line 101

def client
  Chewy.current[:chewy_client] ||= Chewy::ElasticClient.new
end

.close_clientObject

Closes the current thread’s client connections to Elasticsearch and drops the thread-local client, so the next ‘Chewy.client` call builds a fresh one.

Useful in long-lived multi-threaded processes (e.g. Sidekiq) where the per-thread client would otherwise keep its connections open until the dead thread is garbage collected, leaking file descriptors.



112
113
114
115
116
117
118
# File 'lib/chewy.rb', line 112

def close_client
  client = Chewy.current[:chewy_client]
  return unless client

  client.close
  Chewy.current[:chewy_client] = nil
end

.configObject



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

def config
  Chewy::Config.instance
end

.create_indicesObject



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

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

.create_indices!Object



194
195
196
# File 'lib/chewy.rb', line 194

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

.currentHash

A thread-local variables accessor

Returns:

  • (Hash)


70
71
72
73
74
75
76
# File 'lib/chewy.rb', line 70

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:



88
89
90
91
92
93
94
95
96
97
# File 'lib/chewy.rb', line 88

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



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

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.



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

def massacre
  unless Chewy.settings[:delete_all_enabled]
    raise FeatureDisabled, 'Feature disabled by default in ES 8. You can enable it in the cluster and set `delete_all_enabled` option in settings'
  end

  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



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

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


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

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.



125
126
127
128
129
# File 'lib/chewy.rb', line 125

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