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/rake_helper.rb,
lib/chewy/type/crutch.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/resque.rb,
lib/chewy/strategy/urgent.rb,
lib/chewy/type/witchcraft.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/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/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/strategy/active_job.rb,
lib/chewy/type/adapter/object.rb,
lib/chewy/type/adapter/sequel.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, RakeHelper, Runtime, Search Classes: Config, DocumentNotFound, Error, ImportFailed, Index, LogSubscriber, PluginMissing, Query, Railtie, RemovedFeature, Repository, Strategy, Type, UndefinedIndex, UndefinedType, UndefinedUpdateStrategy, UnderivableType
Constant Summary collapse
- VERSION =
'0.8.4'
Class Attribute Summary collapse
-
.adapters ⇒ Object
Returns the value of attribute adapters.
Class Method Summary collapse
-
.client ⇒ Object
Main elasticsearch-ruby client instance.
- .config ⇒ Object
-
.create_type(index, target, options = {}, &block) ⇒ Object
Creates Chewy::Type ancestor defining index and adapter methods.
-
.derive_type(name) ⇒ Object
Derives type from string ‘index#type` representation:.
-
.massacre ⇒ Object
(also: delete_all)
Deletes all corresponding indexes with current prefix from ElasticSearch.
- .repository ⇒ Object
-
.strategy(name = nil, &block) ⇒ Object
Strategies are designed to allow nesting, so it is possible to redefine it for nested contexts.
-
.wait_for_status ⇒ Object
Sends wait_for_status request to ElasticSearch with status defined in configuration.
Class Attribute Details
.adapters ⇒ Object
Returns the value of attribute adapters.
80 81 82 |
# File 'lib/chewy.rb', line 80 def adapters @adapters end |
Class Method Details
.client ⇒ Object
Main elasticsearch-ruby client instance
125 126 127 128 129 130 131 132 |
# File 'lib/chewy.rb', line 125 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 |
.create_type(index, target, options = {}, &block) ⇒ Object
Creates Chewy::Type ancestor defining index and adapter methods.
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/chewy.rb', line 110 def create_type index, target, = {}, &block type = Class.new(Chewy::Type) adapter = adapters.find { |adapter| adapter.accepts?(target) }.new(target, ) 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.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/chewy.rb', line 92 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 |
.massacre ⇒ Object 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.
146 147 148 149 |
# File 'lib/chewy.rb', line 146 def massacre Chewy.client.indices.delete(index: [Chewy.configuration[:prefix], '*'].delete_if(&:blank?).join(?_)) Chewy.wait_for_status end |
.repository ⇒ Object
193 194 195 |
# File 'lib/chewy.rb', line 193 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
175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/chewy.rb', line 175 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_status ⇒ Object
Sends wait_for_status request to ElasticSearch with status defined in configuration.
Does nothing in case of config ‘wait_for_status` is undefined.
139 140 141 |
# File 'lib/chewy.rb', line 139 def wait_for_status client.cluster.health wait_for_status: Chewy.configuration[:wait_for_status] if Chewy.configuration[:wait_for_status].present? end |