Module: Chewy

Defined in:
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/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/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/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/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, Rspec, Runtime, Search, Stash Classes: Config, DocumentNotFound, Error, ImportFailed, Index, Journal, LogSubscriber, MultiSearch, Railtie, Repository, Strategy, UndefinedIndex, UndefinedUpdateStrategy

Constant Summary collapse

VERSION =
'7.2.2'.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



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

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



153
154
155
# File 'lib/chewy.rb', line 153

def config
  Chewy::Config.instance
end

.create_indicesObject



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

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

.create_indices!Object



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

def create_indices!
  Chewy::Index.descendants.each(&:create!)
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:



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

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



171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/chewy.rb', line 171

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.



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

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



158
159
160
# File 'lib/chewy.rb', line 158

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



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/chewy.rb', line 140

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.



102
103
104
105
106
# File 'lib/chewy.rb', line 102

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