chewy 0.8.4 → 5.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +1 -0
- data/.rubocop.yml +56 -0
- data/.rubocop_todo.yml +44 -0
- data/.travis.yml +36 -67
- data/.yardopts +5 -0
- data/Appraisals +63 -58
- data/CHANGELOG.md +168 -11
- data/Gemfile +16 -9
- data/Guardfile +5 -5
- data/LEGACY_DSL.md +497 -0
- data/README.md +403 -470
- data/Rakefile +11 -1
- data/chewy.gemspec +12 -15
- data/gemfiles/rails.4.0.activerecord.gemfile +9 -9
- data/gemfiles/rails.4.1.activerecord.gemfile +9 -9
- data/gemfiles/rails.4.2.activerecord.gemfile +8 -8
- data/gemfiles/rails.4.2.mongoid.5.2.gemfile +16 -0
- data/gemfiles/rails.5.0.activerecord.gemfile +16 -0
- data/gemfiles/rails.5.0.mongoid.6.1.gemfile +16 -0
- data/gemfiles/rails.5.1.activerecord.gemfile +16 -0
- data/gemfiles/rails.5.1.mongoid.6.3.gemfile +16 -0
- data/gemfiles/rails.5.2.activerecord.gemfile +16 -0
- data/gemfiles/sequel.4.45.gemfile +11 -0
- data/lib/chewy/backports/deep_dup.rb +1 -1
- data/lib/chewy/backports/duplicable.rb +1 -0
- data/lib/chewy/config.rb +53 -21
- data/lib/chewy/errors.rb +6 -6
- data/lib/chewy/fields/base.rb +59 -29
- data/lib/chewy/fields/root.rb +49 -14
- data/lib/chewy/index/actions.rb +95 -36
- data/lib/chewy/index/aliases.rb +2 -1
- data/lib/chewy/index/settings.rb +10 -5
- data/lib/chewy/index/specification.rb +60 -0
- data/lib/chewy/index.rb +239 -138
- data/lib/chewy/journal.rb +55 -0
- data/lib/chewy/log_subscriber.rb +8 -8
- data/lib/chewy/minitest/helpers.rb +77 -0
- data/lib/chewy/minitest/search_index_receiver.rb +80 -0
- data/lib/chewy/minitest.rb +1 -0
- data/lib/chewy/query/compose.rb +18 -19
- data/lib/chewy/query/criteria.rb +34 -24
- data/lib/chewy/query/filters.rb +28 -11
- data/lib/chewy/query/loading.rb +3 -4
- data/lib/chewy/query/nodes/and.rb +1 -1
- data/lib/chewy/query/nodes/base.rb +1 -1
- data/lib/chewy/query/nodes/bool.rb +6 -4
- data/lib/chewy/query/nodes/equal.rb +4 -4
- data/lib/chewy/query/nodes/exists.rb +1 -1
- data/lib/chewy/query/nodes/expr.rb +2 -2
- data/lib/chewy/query/nodes/field.rb +35 -31
- data/lib/chewy/query/nodes/has_child.rb +1 -0
- data/lib/chewy/query/nodes/has_parent.rb +1 -0
- data/lib/chewy/query/nodes/has_relation.rb +10 -12
- data/lib/chewy/query/nodes/missing.rb +1 -1
- data/lib/chewy/query/nodes/not.rb +1 -1
- data/lib/chewy/query/nodes/or.rb +1 -1
- data/lib/chewy/query/nodes/prefix.rb +3 -2
- data/lib/chewy/query/nodes/query.rb +1 -1
- data/lib/chewy/query/nodes/range.rb +9 -9
- data/lib/chewy/query/nodes/raw.rb +1 -1
- data/lib/chewy/query/nodes/regexp.rb +13 -9
- data/lib/chewy/query/nodes/script.rb +4 -4
- data/lib/chewy/query/pagination.rb +10 -1
- data/lib/chewy/query.rb +286 -170
- data/lib/chewy/railtie.rb +7 -6
- data/lib/chewy/rake_helper.rb +275 -37
- data/lib/chewy/repository.rb +2 -2
- data/lib/chewy/rspec/update_index.rb +70 -65
- data/lib/chewy/rspec.rb +1 -1
- data/lib/chewy/runtime/version.rb +4 -4
- data/lib/chewy/search/loader.rb +83 -0
- data/lib/chewy/{query → search}/pagination/kaminari.rb +13 -5
- data/lib/chewy/search/pagination/will_paginate.rb +43 -0
- data/lib/chewy/search/parameters/aggs.rb +16 -0
- data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
- data/lib/chewy/search/parameters/concerns/bool_storage.rb +24 -0
- data/lib/chewy/search/parameters/concerns/hash_storage.rb +23 -0
- data/lib/chewy/search/parameters/concerns/integer_storage.rb +14 -0
- data/lib/chewy/search/parameters/concerns/query_storage.rb +238 -0
- data/lib/chewy/search/parameters/concerns/string_array_storage.rb +23 -0
- data/lib/chewy/search/parameters/concerns/string_storage.rb +14 -0
- data/lib/chewy/search/parameters/docvalue_fields.rb +12 -0
- data/lib/chewy/search/parameters/explain.rb +16 -0
- data/lib/chewy/search/parameters/filter.rb +47 -0
- data/lib/chewy/search/parameters/highlight.rb +16 -0
- data/lib/chewy/search/parameters/indices.rb +123 -0
- data/lib/chewy/search/parameters/indices_boost.rb +52 -0
- data/lib/chewy/search/parameters/limit.rb +17 -0
- data/lib/chewy/search/parameters/load.rb +32 -0
- data/lib/chewy/search/parameters/min_score.rb +16 -0
- data/lib/chewy/search/parameters/none.rb +27 -0
- data/lib/chewy/search/parameters/offset.rb +17 -0
- data/lib/chewy/search/parameters/order.rb +64 -0
- data/lib/chewy/search/parameters/post_filter.rb +19 -0
- data/lib/chewy/search/parameters/preference.rb +16 -0
- data/lib/chewy/search/parameters/profile.rb +16 -0
- data/lib/chewy/search/parameters/query.rb +19 -0
- data/lib/chewy/search/parameters/request_cache.rb +27 -0
- data/lib/chewy/search/parameters/rescore.rb +29 -0
- data/lib/chewy/search/parameters/script_fields.rb +16 -0
- data/lib/chewy/search/parameters/search_after.rb +20 -0
- data/lib/chewy/search/parameters/search_type.rb +16 -0
- data/lib/chewy/search/parameters/source.rb +73 -0
- data/lib/chewy/search/parameters/storage.rb +95 -0
- data/lib/chewy/search/parameters/stored_fields.rb +63 -0
- data/lib/chewy/search/parameters/suggest.rb +16 -0
- data/lib/chewy/search/parameters/terminate_after.rb +16 -0
- data/lib/chewy/search/parameters/timeout.rb +16 -0
- data/lib/chewy/search/parameters/track_scores.rb +16 -0
- data/lib/chewy/search/parameters/types.rb +20 -0
- data/lib/chewy/search/parameters/version.rb +16 -0
- data/lib/chewy/search/parameters.rb +167 -0
- data/lib/chewy/search/query_proxy.rb +257 -0
- data/lib/chewy/search/request.rb +1045 -0
- data/lib/chewy/search/response.rb +119 -0
- data/lib/chewy/search/scoping.rb +50 -0
- data/lib/chewy/search/scrolling.rb +134 -0
- data/lib/chewy/search.rb +81 -26
- data/lib/chewy/stash.rb +79 -0
- data/lib/chewy/strategy/active_job.rb +1 -0
- data/lib/chewy/strategy/atomic.rb +2 -4
- data/lib/chewy/strategy/base.rb +4 -4
- data/lib/chewy/strategy/bypass.rb +1 -2
- data/lib/chewy/strategy/resque.rb +1 -0
- data/lib/chewy/strategy/shoryuken.rb +40 -0
- data/lib/chewy/strategy/sidekiq.rb +13 -1
- data/lib/chewy/strategy/urgent.rb +1 -1
- data/lib/chewy/strategy.rb +19 -10
- data/lib/chewy/type/actions.rb +26 -2
- data/lib/chewy/type/adapter/active_record.rb +50 -24
- data/lib/chewy/type/adapter/base.rb +29 -9
- data/lib/chewy/type/adapter/mongoid.rb +19 -10
- data/lib/chewy/type/adapter/object.rb +195 -31
- data/lib/chewy/type/adapter/orm.rb +69 -33
- data/lib/chewy/type/adapter/sequel.rb +37 -19
- data/lib/chewy/type/crutch.rb +5 -4
- data/lib/chewy/type/import/bulk_builder.rb +122 -0
- data/lib/chewy/type/import/bulk_request.rb +78 -0
- data/lib/chewy/type/import/journal_builder.rb +45 -0
- data/lib/chewy/type/import/routine.rb +138 -0
- data/lib/chewy/type/import.rb +150 -176
- data/lib/chewy/type/mapping.rb +58 -42
- data/lib/chewy/type/observe.rb +21 -15
- data/lib/chewy/type/syncer.rb +222 -0
- data/lib/chewy/type/witchcraft.rb +89 -34
- data/lib/chewy/type/wrapper.rb +48 -16
- data/lib/chewy/type.rb +77 -49
- data/lib/chewy/version.rb +1 -1
- data/lib/chewy.rb +95 -52
- data/lib/generators/chewy/install_generator.rb +3 -3
- data/lib/sequel/plugins/chewy_observe.rb +4 -19
- data/lib/tasks/chewy.rake +91 -28
- data/spec/chewy/config_spec.rb +130 -12
- data/spec/chewy/fields/base_spec.rb +194 -172
- data/spec/chewy/fields/root_spec.rb +123 -17
- data/spec/chewy/fields/time_fields_spec.rb +10 -9
- data/spec/chewy/index/actions_spec.rb +228 -43
- data/spec/chewy/index/aliases_spec.rb +2 -2
- data/spec/chewy/index/settings_spec.rb +100 -49
- data/spec/chewy/index/specification_spec.rb +169 -0
- data/spec/chewy/index_spec.rb +159 -63
- data/spec/chewy/journal_spec.rb +268 -0
- data/spec/chewy/minitest/helpers_spec.rb +90 -0
- data/spec/chewy/minitest/search_index_receiver_spec.rb +120 -0
- data/spec/chewy/query/criteria_spec.rb +503 -236
- data/spec/chewy/query/filters_spec.rb +96 -68
- data/spec/chewy/query/loading_spec.rb +80 -42
- data/spec/chewy/query/nodes/and_spec.rb +3 -7
- data/spec/chewy/query/nodes/bool_spec.rb +5 -13
- data/spec/chewy/query/nodes/equal_spec.rb +20 -20
- data/spec/chewy/query/nodes/exists_spec.rb +7 -7
- data/spec/chewy/query/nodes/has_child_spec.rb +42 -23
- data/spec/chewy/query/nodes/has_parent_spec.rb +42 -23
- data/spec/chewy/query/nodes/match_all_spec.rb +2 -2
- data/spec/chewy/query/nodes/missing_spec.rb +6 -5
- data/spec/chewy/query/nodes/not_spec.rb +5 -7
- data/spec/chewy/query/nodes/or_spec.rb +3 -7
- data/spec/chewy/query/nodes/prefix_spec.rb +6 -6
- data/spec/chewy/query/nodes/query_spec.rb +3 -3
- data/spec/chewy/query/nodes/range_spec.rb +19 -19
- data/spec/chewy/query/nodes/raw_spec.rb +2 -2
- data/spec/chewy/query/nodes/regexp_spec.rb +31 -19
- data/spec/chewy/query/nodes/script_spec.rb +5 -5
- data/spec/chewy/query/pagination/kaminari_spec.rb +3 -55
- data/spec/chewy/query/pagination/will_paginate_spec.rb +5 -0
- data/spec/chewy/query/pagination_spec.rb +25 -22
- data/spec/chewy/query_spec.rb +510 -505
- data/spec/chewy/rake_helper_spec.rb +381 -0
- data/spec/chewy/repository_spec.rb +8 -8
- data/spec/chewy/rspec/update_index_spec.rb +215 -113
- data/spec/chewy/runtime_spec.rb +2 -2
- data/spec/chewy/search/loader_spec.rb +117 -0
- data/spec/chewy/search/pagination/kaminari_examples.rb +71 -0
- data/spec/chewy/search/pagination/kaminari_spec.rb +21 -0
- data/spec/chewy/search/pagination/will_paginate_examples.rb +63 -0
- data/spec/chewy/search/pagination/will_paginate_spec.rb +23 -0
- data/spec/chewy/search/parameters/aggs_spec.rb +5 -0
- data/spec/chewy/search/parameters/bool_storage_examples.rb +53 -0
- data/spec/chewy/search/parameters/docvalue_fields_spec.rb +5 -0
- data/spec/chewy/search/parameters/explain_spec.rb +5 -0
- data/spec/chewy/search/parameters/filter_spec.rb +5 -0
- data/spec/chewy/search/parameters/hash_storage_examples.rb +59 -0
- data/spec/chewy/search/parameters/highlight_spec.rb +5 -0
- data/spec/chewy/search/parameters/indices_spec.rb +191 -0
- data/spec/chewy/search/parameters/integer_storage_examples.rb +32 -0
- data/spec/chewy/search/parameters/limit_spec.rb +5 -0
- data/spec/chewy/search/parameters/load_spec.rb +60 -0
- data/spec/chewy/search/parameters/min_score_spec.rb +32 -0
- data/spec/chewy/search/parameters/none_spec.rb +5 -0
- data/spec/chewy/search/parameters/offset_spec.rb +5 -0
- data/spec/chewy/search/parameters/order_spec.rb +65 -0
- data/spec/chewy/search/parameters/post_filter_spec.rb +5 -0
- data/spec/chewy/search/parameters/preference_spec.rb +5 -0
- data/spec/chewy/search/parameters/profile_spec.rb +5 -0
- data/spec/chewy/search/parameters/query_spec.rb +5 -0
- data/spec/chewy/search/parameters/query_storage_examples.rb +388 -0
- data/spec/chewy/search/parameters/request_cache_spec.rb +67 -0
- data/spec/chewy/search/parameters/rescore_spec.rb +62 -0
- data/spec/chewy/search/parameters/script_fields_spec.rb +5 -0
- data/spec/chewy/search/parameters/search_after_spec.rb +32 -0
- data/spec/chewy/search/parameters/search_type_spec.rb +5 -0
- data/spec/chewy/search/parameters/source_spec.rb +156 -0
- data/spec/chewy/search/parameters/storage_spec.rb +60 -0
- data/spec/chewy/search/parameters/stored_fields_spec.rb +126 -0
- data/spec/chewy/search/parameters/string_array_storage_examples.rb +63 -0
- data/spec/chewy/search/parameters/string_storage_examples.rb +32 -0
- data/spec/chewy/search/parameters/suggest_spec.rb +5 -0
- data/spec/chewy/search/parameters/terminate_after_spec.rb +5 -0
- data/spec/chewy/search/parameters/timeout_spec.rb +5 -0
- data/spec/chewy/search/parameters/track_scores_spec.rb +5 -0
- data/spec/chewy/search/parameters/types_spec.rb +5 -0
- data/spec/chewy/search/parameters/version_spec.rb +5 -0
- data/spec/chewy/search/parameters_spec.rb +145 -0
- data/spec/chewy/search/query_proxy_spec.rb +68 -0
- data/spec/chewy/search/request_spec.rb +685 -0
- data/spec/chewy/search/response_spec.rb +192 -0
- data/spec/chewy/search/scrolling_spec.rb +169 -0
- data/spec/chewy/search_spec.rb +37 -20
- data/spec/chewy/stash_spec.rb +95 -0
- data/spec/chewy/strategy/active_job_spec.rb +8 -2
- data/spec/chewy/strategy/atomic_spec.rb +4 -1
- data/spec/chewy/strategy/resque_spec.rb +8 -2
- data/spec/chewy/strategy/shoryuken_spec.rb +66 -0
- data/spec/chewy/strategy/sidekiq_spec.rb +10 -2
- data/spec/chewy/strategy_spec.rb +6 -6
- data/spec/chewy/type/actions_spec.rb +29 -10
- data/spec/chewy/type/adapter/active_record_spec.rb +357 -139
- data/spec/chewy/type/adapter/mongoid_spec.rb +220 -101
- data/spec/chewy/type/adapter/object_spec.rb +129 -40
- data/spec/chewy/type/adapter/sequel_spec.rb +304 -152
- data/spec/chewy/type/import/bulk_builder_spec.rb +279 -0
- data/spec/chewy/type/import/bulk_request_spec.rb +102 -0
- data/spec/chewy/type/import/journal_builder_spec.rb +95 -0
- data/spec/chewy/type/import/routine_spec.rb +110 -0
- data/spec/chewy/type/import_spec.rb +360 -244
- data/spec/chewy/type/mapping_spec.rb +96 -29
- data/spec/chewy/type/observe_spec.rb +25 -15
- data/spec/chewy/type/syncer_spec.rb +123 -0
- data/spec/chewy/type/witchcraft_spec.rb +122 -44
- data/spec/chewy/type/wrapper_spec.rb +63 -23
- data/spec/chewy/type_spec.rb +32 -10
- data/spec/chewy_spec.rb +82 -12
- data/spec/spec_helper.rb +16 -2
- data/spec/support/active_record.rb +6 -2
- data/spec/support/class_helpers.rb +4 -19
- data/spec/support/mongoid.rb +17 -5
- data/spec/support/sequel.rb +6 -1
- metadata +250 -57
- data/gemfiles/rails.3.2.activerecord.gemfile +0 -15
- data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +0 -14
- data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.0.activerecord.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.0.activerecord.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.0.mongoid.4.0.0.gemfile +0 -15
- data/gemfiles/rails.4.0.mongoid.4.0.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.0.mongoid.4.0.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.0.mongoid.5.1.0.gemfile +0 -15
- data/gemfiles/rails.4.0.mongoid.5.1.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.0.mongoid.5.1.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.1.activerecord.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.1.activerecord.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.1.mongoid.4.0.0.gemfile +0 -15
- data/gemfiles/rails.4.1.mongoid.4.0.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.1.mongoid.4.0.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.1.mongoid.5.1.0.gemfile +0 -15
- data/gemfiles/rails.4.1.mongoid.5.1.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.1.mongoid.5.1.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +0 -15
- data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.4.0.0.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.4.0.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.2.mongoid.4.0.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.4.2.mongoid.5.1.0.gemfile +0 -15
- data/gemfiles/rails.4.2.mongoid.5.1.0.kaminari.gemfile +0 -14
- data/gemfiles/rails.4.2.mongoid.5.1.0.will_paginate.gemfile +0 -14
- data/gemfiles/rails.5.0.0.beta3.activerecord.gemfile +0 -16
- data/gemfiles/rails.5.0.0.beta3.activerecord.kaminari.gemfile +0 -16
- data/gemfiles/rails.5.0.0.beta3.activerecord.will_paginate.gemfile +0 -15
- data/gemfiles/sequel.4.31.gemfile +0 -13
- data/lib/chewy/query/pagination/will_paginate.rb +0 -27
- data/lib/chewy/query/scoping.rb +0 -20
- data/spec/chewy/query/pagination/will_paginage_spec.rb +0 -60
data/lib/chewy/type.rb
CHANGED
@@ -1,19 +1,20 @@
|
|
1
1
|
require 'chewy/search'
|
2
|
+
require 'chewy/type/adapter/object'
|
3
|
+
require 'chewy/type/adapter/active_record'
|
4
|
+
require 'chewy/type/adapter/mongoid'
|
5
|
+
require 'chewy/type/adapter/sequel'
|
2
6
|
require 'chewy/type/mapping'
|
3
7
|
require 'chewy/type/wrapper'
|
4
8
|
require 'chewy/type/observe'
|
5
9
|
require 'chewy/type/actions'
|
10
|
+
require 'chewy/type/syncer'
|
6
11
|
require 'chewy/type/crutch'
|
7
12
|
require 'chewy/type/import'
|
8
13
|
require 'chewy/type/witchcraft'
|
9
|
-
require 'chewy/type/adapter/object'
|
10
|
-
require 'chewy/type/adapter/active_record'
|
11
|
-
require 'chewy/type/adapter/mongoid'
|
12
|
-
require 'chewy/type/adapter/sequel'
|
13
14
|
|
14
15
|
module Chewy
|
15
16
|
class Type
|
16
|
-
IMPORT_OPTIONS_KEYS = [
|
17
|
+
IMPORT_OPTIONS_KEYS = %i[batch_size bulk_size refresh consistency replication raw_import journal pipeline].freeze
|
17
18
|
|
18
19
|
include Search
|
19
20
|
include Mapping
|
@@ -24,66 +25,93 @@ module Chewy
|
|
24
25
|
include Witchcraft
|
25
26
|
include Import
|
26
27
|
|
27
|
-
singleton_class.delegate :index_name, :client, to: :index
|
28
|
+
singleton_class.delegate :index_name, :derivable_index_name, :client, to: :index
|
28
29
|
|
29
30
|
class_attribute :_default_import_options
|
30
31
|
self._default_import_options = {}
|
31
32
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
33
|
+
class << self
|
34
|
+
# Chewy index current type belongs to. Defined inside `Chewy.create_type`
|
35
|
+
#
|
36
|
+
def index
|
37
|
+
raise NotImplementedError, 'Looks like this type was defined outside the index scope and `.index` method is undefined for it'
|
38
|
+
end
|
37
39
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
40
|
+
# Current type adapter. Defined inside `Chewy.create_type`, derived from
|
41
|
+
# `Chewy::Index.define_type` arguments.
|
42
|
+
#
|
43
|
+
def adapter
|
44
|
+
raise NotImplementedError
|
45
|
+
end
|
44
46
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
47
|
+
# Returns type name string
|
48
|
+
#
|
49
|
+
def type_name
|
50
|
+
adapter.type_name
|
51
|
+
end
|
50
52
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
53
|
+
# Appends type name to {Chewy::Index.derivable_name}
|
54
|
+
#
|
55
|
+
# @example
|
56
|
+
# class Namespace::UsersIndex < Chewy::Index
|
57
|
+
# define_type User
|
58
|
+
# end
|
59
|
+
# UsersIndex::User.derivable_name # => 'namespace/users#user'
|
60
|
+
#
|
61
|
+
# @see Chewy::Index.derivable_name
|
62
|
+
# @return [String, nil] derivable name or nil when it is impossible to calculate
|
63
|
+
def derivable_name
|
64
|
+
@derivable_name ||= [index.derivable_name, type_name].join('#') if index && index.derivable_name
|
65
|
+
end
|
56
66
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
67
|
+
# This method is an API shared with {Chewy::Index}, added for convenience.
|
68
|
+
#
|
69
|
+
# @return [Chewy::Type] array containing itself
|
70
|
+
def types
|
71
|
+
[self]
|
72
|
+
end
|
61
73
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
74
|
+
# Returns list of public class methods defined in current type
|
75
|
+
#
|
76
|
+
def scopes
|
77
|
+
public_methods - Chewy::Type.public_methods
|
78
|
+
end
|
79
|
+
|
80
|
+
def default_import_options(params)
|
81
|
+
params.assert_valid_keys(IMPORT_OPTIONS_KEYS)
|
82
|
+
self._default_import_options = _default_import_options.merge(params)
|
83
|
+
end
|
84
|
+
|
85
|
+
def method_missing(method, *args, &block)
|
86
|
+
if index.scopes.include?(method)
|
87
|
+
define_singleton_method method do |*method_args, &method_block|
|
88
|
+
all.scoping { index.public_send(method, *method_args, &method_block) }
|
89
|
+
end
|
90
|
+
send(method, *args, &block)
|
91
|
+
else
|
92
|
+
super
|
66
93
|
end
|
67
|
-
send(method, *args, &block)
|
68
|
-
else
|
69
|
-
super
|
70
94
|
end
|
71
|
-
end
|
72
95
|
|
73
|
-
|
74
|
-
|
75
|
-
|
96
|
+
def respond_to_missing?(method, _)
|
97
|
+
index.scopes.include?(method) || super
|
98
|
+
end
|
99
|
+
|
100
|
+
def const_missing(name)
|
101
|
+
to_resolve = "#{self}::#{name}"
|
102
|
+
to_resolve[index.to_s] = ''
|
76
103
|
|
77
|
-
|
104
|
+
@__resolved_constants ||= {}
|
78
105
|
|
79
|
-
|
106
|
+
if to_resolve.empty? || @__resolved_constants[to_resolve]
|
107
|
+
super
|
108
|
+
else
|
109
|
+
@__resolved_constants[to_resolve] = true
|
110
|
+
to_resolve.constantize
|
111
|
+
end
|
112
|
+
rescue NotImplementedError
|
80
113
|
super
|
81
|
-
else
|
82
|
-
@__resolved_constants[to_resolve] = true
|
83
|
-
to_resolve.constantize
|
84
114
|
end
|
85
|
-
rescue NotImplementedError
|
86
|
-
super
|
87
115
|
end
|
88
116
|
end
|
89
117
|
end
|
data/lib/chewy/version.rb
CHANGED
data/lib/chewy.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'active_support/version'
|
1
2
|
require 'active_support/concern'
|
2
3
|
require 'active_support/deprecation'
|
3
4
|
require 'active_support/json'
|
@@ -16,9 +17,32 @@ require 'active_support/core_ext/string/inflections'
|
|
16
17
|
require 'i18n/core_ext/hash'
|
17
18
|
require 'chewy/backports/deep_dup' unless Object.respond_to?(:deep_dup)
|
18
19
|
require 'singleton'
|
20
|
+
require 'base64'
|
19
21
|
|
20
22
|
require 'elasticsearch'
|
21
23
|
|
24
|
+
def try_require(path)
|
25
|
+
require path
|
26
|
+
rescue LoadError
|
27
|
+
nil
|
28
|
+
end
|
29
|
+
|
30
|
+
try_require 'kaminari'
|
31
|
+
try_require 'kaminari/core'
|
32
|
+
try_require 'will_paginate'
|
33
|
+
try_require 'will_paginate/collection'
|
34
|
+
try_require 'parallel'
|
35
|
+
|
36
|
+
ActiveSupport.on_load(:active_record) do
|
37
|
+
try_require 'will_paginate/active_record'
|
38
|
+
try_require 'kaminari/activerecord'
|
39
|
+
end
|
40
|
+
|
41
|
+
ActiveSupport.on_load(:mongoid) do
|
42
|
+
try_require 'will_paginate/mongoid'
|
43
|
+
try_require 'kaminari/mongoid'
|
44
|
+
end
|
45
|
+
|
22
46
|
require 'chewy/version'
|
23
47
|
require 'chewy/errors'
|
24
48
|
require 'chewy/config'
|
@@ -31,44 +55,24 @@ require 'chewy/index'
|
|
31
55
|
require 'chewy/type'
|
32
56
|
require 'chewy/fields/base'
|
33
57
|
require 'chewy/fields/root'
|
34
|
-
require 'chewy/
|
35
|
-
|
36
|
-
begin
|
37
|
-
require 'kaminari'
|
38
|
-
require 'chewy/query/pagination/kaminari'
|
39
|
-
rescue LoadError
|
40
|
-
end
|
41
|
-
|
42
|
-
begin
|
43
|
-
require 'will_paginate'
|
44
|
-
require 'will_paginate/collection'
|
45
|
-
require 'chewy/query/pagination/will_paginate'
|
46
|
-
rescue LoadError
|
47
|
-
end
|
58
|
+
require 'chewy/journal'
|
59
|
+
require 'chewy/railtie' if defined?(::Rails::Railtie)
|
48
60
|
|
49
61
|
ActiveSupport.on_load(:active_record) do
|
50
62
|
extend Chewy::Type::Observe::ActiveRecordMethods
|
51
|
-
|
52
|
-
begin
|
53
|
-
require 'will_paginate/active_record'
|
54
|
-
rescue LoadError
|
55
|
-
end
|
56
63
|
end
|
57
64
|
|
58
65
|
ActiveSupport.on_load(:mongoid) do
|
59
|
-
module Mongoid
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
require 'chewy/query/pagination/will_paginate'
|
66
|
-
rescue LoadError
|
66
|
+
module Mongoid
|
67
|
+
module Document
|
68
|
+
module ClassMethods
|
69
|
+
include Chewy::Type::Observe::MongoidMethods
|
70
|
+
end
|
71
|
+
end
|
67
72
|
end
|
68
73
|
end
|
69
74
|
|
70
75
|
module Chewy
|
71
|
-
|
72
76
|
@adapters = [
|
73
77
|
Chewy::Type::Adapter::ActiveRecord,
|
74
78
|
Chewy::Type::Adapter::Mongoid,
|
@@ -79,44 +83,62 @@ module Chewy
|
|
79
83
|
class << self
|
80
84
|
attr_accessor :adapters
|
81
85
|
|
82
|
-
# Derives type
|
83
|
-
#
|
84
|
-
# Chewy.derive_type('users#user') # => UsersIndex::User
|
85
|
-
#
|
86
|
-
# If index has only one type - it is possible to derive it without specification:
|
86
|
+
# Derives a single type for the passed string identifier if possible.
|
87
87
|
#
|
88
|
-
#
|
88
|
+
# @example
|
89
|
+
# Chewy.derive_types(UsersIndex::User) # => UsersIndex::User
|
90
|
+
# Chewy.derive_types('namespace/users') # => Namespace::UsersIndex::User
|
91
|
+
# Chewy.derive_types('places') # => raises Chewy::UnderivableType
|
92
|
+
# Chewy.derive_types('places#city') # => PlacesIndex::City
|
89
93
|
#
|
90
|
-
#
|
91
|
-
#
|
92
|
-
|
94
|
+
# @param name [String, Chewy::Type] string type identifier
|
95
|
+
# @raise [Chewy::UnderivableType] in cases when it is impossble to find index or type or more than one type found
|
96
|
+
# @return [Chewy::Type] an array of derived types
|
97
|
+
def derive_type(name)
|
93
98
|
return name if name.is_a?(Class) && name < Chewy::Type
|
94
99
|
|
95
|
-
|
96
|
-
|
100
|
+
types = derive_types(name)
|
101
|
+
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?
|
102
|
+
types.first
|
103
|
+
end
|
104
|
+
|
105
|
+
# Derives all the types for the passed string identifier if possible.
|
106
|
+
#
|
107
|
+
# @example
|
108
|
+
# Chewy.derive_types('namespace/users') # => [Namespace::UsersIndex::User]
|
109
|
+
# Chewy.derive_types('places') # => [PlacesIndex::City, PlacesIndex::Country]
|
110
|
+
# Chewy.derive_types('places#city') # => [PlacesIndex::City]
|
111
|
+
#
|
112
|
+
# @param from [String] string type identifier
|
113
|
+
# @raise [Chewy::UnderivableType] in cases when it is impossible to find index or type
|
114
|
+
# @return [Array<Chewy::Type>] an array of derived types
|
115
|
+
def derive_types(from)
|
116
|
+
return from.types if from.is_a?(Class) && (from < Chewy::Index || from < Chewy::Type)
|
117
|
+
|
118
|
+
index_name, type_name = from.split('#', 2)
|
119
|
+
class_name = "#{index_name.camelize.gsub(/Index\z/, '')}Index"
|
97
120
|
index = class_name.safe_constantize
|
98
|
-
raise Chewy::UnderivableType
|
99
|
-
|
100
|
-
index.type_hash[type_name] or raise Chewy::UnderivableType
|
101
|
-
|
102
|
-
index.types.first
|
121
|
+
raise Chewy::UnderivableType, "Can not find index named `#{class_name}`" unless index && index < Chewy::Index
|
122
|
+
if type_name.present?
|
123
|
+
type = index.type_hash[type_name] or raise Chewy::UnderivableType, "Index `#{class_name}` doesn`t have type named `#{type_name}`"
|
124
|
+
[type]
|
103
125
|
else
|
104
|
-
|
126
|
+
index.types
|
105
127
|
end
|
106
128
|
end
|
107
129
|
|
108
130
|
# Creates Chewy::Type ancestor defining index and adapter methods.
|
109
131
|
#
|
110
|
-
def create_type
|
132
|
+
def create_type(index, target, options = {}, &block)
|
111
133
|
type = Class.new(Chewy::Type)
|
112
134
|
|
113
|
-
adapter = adapters.find { |
|
135
|
+
adapter = adapters.find { |klass| klass.accepts?(target) }.new(target, options)
|
114
136
|
|
115
137
|
index.const_set(adapter.name, type)
|
116
138
|
type.send(:define_singleton_method, :index) { index }
|
117
139
|
type.send(:define_singleton_method, :adapter) { adapter }
|
118
140
|
|
119
|
-
type.class_eval
|
141
|
+
type.class_eval(&block) if block
|
120
142
|
type
|
121
143
|
end
|
122
144
|
|
@@ -144,7 +166,7 @@ module Chewy
|
|
144
166
|
# Be careful, if current prefix is blank, this will destroy all the indexes.
|
145
167
|
#
|
146
168
|
def massacre
|
147
|
-
Chewy.client.indices.delete(index: [Chewy.configuration[:prefix], '*'].
|
169
|
+
Chewy.client.indices.delete(index: [Chewy.configuration[:prefix], '*'].reject(&:blank?).join('_'))
|
148
170
|
Chewy.wait_for_status
|
149
171
|
end
|
150
172
|
alias_method :delete_all, :massacre
|
@@ -172,7 +194,7 @@ module Chewy
|
|
172
194
|
# Chewy.strategy.pop
|
173
195
|
# city3.do_update! # index updated again
|
174
196
|
#
|
175
|
-
def strategy
|
197
|
+
def strategy(name = nil, &block)
|
176
198
|
Thread.current[:chewy_strategy] ||= Chewy::Strategy.new
|
177
199
|
if name
|
178
200
|
if block
|
@@ -188,11 +210,32 @@ module Chewy
|
|
188
210
|
def config
|
189
211
|
Chewy::Config.instance
|
190
212
|
end
|
191
|
-
delegate
|
213
|
+
delegate(*Chewy::Config.delegated, to: :config)
|
192
214
|
|
193
215
|
def repository
|
194
216
|
Chewy::Repository.instance
|
195
217
|
end
|
196
|
-
delegate
|
218
|
+
delegate(*Chewy::Repository.delegated, to: :repository)
|
219
|
+
|
220
|
+
def create_indices
|
221
|
+
Chewy::Index.descendants.each(&:create)
|
222
|
+
end
|
223
|
+
|
224
|
+
def create_indices!
|
225
|
+
Chewy::Index.descendants.each(&:create!)
|
226
|
+
end
|
227
|
+
|
228
|
+
def eager_load!
|
229
|
+
return unless defined?(Chewy::Railtie)
|
230
|
+
dirs = Chewy::Railtie.all_engines.map { |engine| engine.paths[Chewy.configuration[:indices_path]] }.compact.map(&:existent).flatten.uniq
|
231
|
+
|
232
|
+
dirs.each do |dir|
|
233
|
+
Dir.glob(File.join(dir, '**/*.rb')).each do |file|
|
234
|
+
require_dependency file
|
235
|
+
end
|
236
|
+
end
|
237
|
+
end
|
197
238
|
end
|
198
239
|
end
|
240
|
+
|
241
|
+
require 'chewy/stash'
|
@@ -1,11 +1,11 @@
|
|
1
1
|
module Chewy
|
2
2
|
module Generators
|
3
3
|
class InstallGenerator < Rails::Generators::Base
|
4
|
-
source_root File.expand_path(
|
4
|
+
source_root File.expand_path('../../templates', __FILE__)
|
5
5
|
|
6
6
|
def copy_configuration
|
7
|
-
template
|
7
|
+
template 'chewy.yml', 'config/chewy.yml'
|
8
8
|
end
|
9
9
|
end
|
10
10
|
end
|
11
|
-
end
|
11
|
+
end
|
@@ -36,40 +36,25 @@ module Sequel
|
|
36
36
|
callback_options = ChewyObserve.extract_callback_options!(args)
|
37
37
|
update_proc = ChewyObserve.update_proc(type_name, *args, &block)
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
set_callback(:destroy_commit, callback_options, &update_proc)
|
42
|
-
else
|
43
|
-
set_callback(:save, callback_options, &update_proc)
|
44
|
-
set_callback(:destroy, callback_options, &update_proc)
|
45
|
-
end
|
39
|
+
set_callback(:save, callback_options, &update_proc)
|
40
|
+
set_callback(:destroy, callback_options, &update_proc)
|
46
41
|
end
|
47
42
|
end
|
48
43
|
|
49
44
|
# Instance level methods for Sequel::Model
|
50
45
|
#
|
51
46
|
module InstanceMethods
|
52
|
-
def after_commit
|
53
|
-
run_callbacks(:commit) do
|
54
|
-
super
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
def after_destroy_commit
|
59
|
-
run_callbacks(:destroy_commit) do
|
60
|
-
super
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
47
|
def after_save
|
65
48
|
run_callbacks(:save) do
|
66
49
|
super
|
50
|
+
db.after_commit {} if Chewy.use_after_commit_callbacks
|
67
51
|
end
|
68
52
|
end
|
69
53
|
|
70
54
|
def after_destroy
|
71
55
|
run_callbacks(:destroy) do
|
72
56
|
super
|
57
|
+
db.after_commit {} if Chewy.use_after_commit_callbacks
|
73
58
|
end
|
74
59
|
end
|
75
60
|
end
|
data/lib/tasks/chewy.rake
CHANGED
@@ -1,49 +1,112 @@
|
|
1
1
|
require 'chewy/rake_helper'
|
2
2
|
|
3
|
+
def parse_classes(args)
|
4
|
+
if args.present? && args.first.tr!('-', '')
|
5
|
+
{except: args}
|
6
|
+
else
|
7
|
+
{only: args}
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def parse_parallel_args(args)
|
12
|
+
options = {}
|
13
|
+
options[:parallel] = args.first =~ /\A\d+\z/ ? Integer(args.shift) : true
|
14
|
+
options.merge!(parse_classes(args))
|
15
|
+
end
|
16
|
+
|
17
|
+
def parse_journal_args(args)
|
18
|
+
options = {}
|
19
|
+
options[:time] = Time.parse(args.shift) if args.first =~ /\A\d+/
|
20
|
+
options.merge!(parse_classes(args))
|
21
|
+
end
|
22
|
+
|
3
23
|
namespace :chewy do
|
4
|
-
desc '
|
5
|
-
task
|
6
|
-
Chewy::RakeHelper.
|
24
|
+
desc 'Destroys, recreates and imports data for the specified indexes or all of them'
|
25
|
+
task reset: :environment do |_task, args|
|
26
|
+
Chewy::RakeHelper.reset(parse_classes(args.extras))
|
27
|
+
end
|
7
28
|
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
29
|
+
desc 'Resets data for the specified indexes or all of them only if the index specification is changed'
|
30
|
+
task upgrade: :environment do |_task, args|
|
31
|
+
Chewy::RakeHelper.upgrade(parse_classes(args.extras))
|
32
|
+
end
|
33
|
+
|
34
|
+
desc 'Updates data for the specified indexes/types or all of them'
|
35
|
+
task update: :environment do |_task, args|
|
36
|
+
Chewy::RakeHelper.update(parse_classes(args.extras))
|
37
|
+
end
|
38
|
+
|
39
|
+
desc 'Synchronizes data for the specified indexes/types or all of them'
|
40
|
+
task sync: :environment do |_task, args|
|
41
|
+
Chewy::RakeHelper.sync(parse_classes(args.extras))
|
42
|
+
end
|
43
|
+
|
44
|
+
desc 'Resets all the indexes with the specification changed and synchronizes the rest of them'
|
45
|
+
task deploy: :environment do
|
46
|
+
processed = Chewy::RakeHelper.upgrade
|
47
|
+
Chewy::RakeHelper.sync(except: processed)
|
48
|
+
end
|
49
|
+
|
50
|
+
namespace :parallel do
|
51
|
+
desc 'Parallel version of `rake chewy:reset`'
|
52
|
+
task reset: :environment do |_task, args|
|
53
|
+
Chewy::RakeHelper.reset(parse_parallel_args(args.extras))
|
54
|
+
end
|
55
|
+
|
56
|
+
desc 'Parallel version of `rake chewy:upgrade`'
|
57
|
+
task upgrade: :environment do |_task, args|
|
58
|
+
Chewy::RakeHelper.upgrade(parse_parallel_args(args.extras))
|
59
|
+
end
|
60
|
+
|
61
|
+
desc 'Parallel version of `rake chewy:update`'
|
62
|
+
task update: :environment do |_task, args|
|
63
|
+
Chewy::RakeHelper.update(parse_parallel_args(args.extras))
|
64
|
+
end
|
65
|
+
|
66
|
+
desc 'Parallel version of `rake chewy:sync`'
|
67
|
+
task sync: :environment do |_task, args|
|
68
|
+
Chewy::RakeHelper.sync(parse_parallel_args(args.extras))
|
69
|
+
end
|
70
|
+
|
71
|
+
desc 'Parallel version of `rake chewy:deploy`'
|
72
|
+
task deploy: :environment do |_task, args|
|
73
|
+
parallel = args.extras.first =~ /\A\d+\z/ ? Integer(args.extras.first) : true
|
74
|
+
processed = Chewy::RakeHelper.upgrade(parallel: parallel)
|
75
|
+
Chewy::RakeHelper.sync(except: processed, parallel: parallel)
|
13
76
|
end
|
14
77
|
end
|
15
78
|
|
16
|
-
namespace :
|
17
|
-
desc '
|
18
|
-
task
|
19
|
-
|
79
|
+
namespace :journal do
|
80
|
+
desc 'Applies changes that were done after the specified time for the specified indexes/types or all of them'
|
81
|
+
task apply: :environment do |_task, args|
|
82
|
+
Chewy::RakeHelper.journal_apply(parse_journal_args(args.extras))
|
83
|
+
end
|
20
84
|
|
21
|
-
|
22
|
-
|
23
|
-
|
85
|
+
desc 'Removes journal records created before the specified timestamp for the specified indexes/types or all of them'
|
86
|
+
task clean: :environment do |_task, args|
|
87
|
+
Chewy::RakeHelper.journal_clean(parse_journal_args(args.extras))
|
24
88
|
end
|
25
89
|
end
|
26
90
|
|
27
|
-
|
28
|
-
|
91
|
+
task apply_changes_from: :environment do |_task, args|
|
92
|
+
ActiveSupport::Deprecation.warn '`rake chewy:apply_changes_from` is deprecated and will be removed soon, use `rake chewy:journal:apply` instead'
|
93
|
+
|
29
94
|
Chewy::RakeHelper.subscribed_task_stats do
|
95
|
+
params = args.extras
|
30
96
|
|
31
|
-
if
|
32
|
-
|
97
|
+
if params.empty?
|
98
|
+
puts 'Please specify a timestamp like chewy:apply_changes_from[1469528705]'
|
33
99
|
else
|
34
|
-
|
100
|
+
timestamp, retries = params
|
101
|
+
time = Time.at(timestamp.to_i)
|
102
|
+
Chewy::Journal.new.apply(time, retries: (retries.to_i if retries))
|
35
103
|
end
|
36
104
|
end
|
37
105
|
end
|
38
106
|
|
39
|
-
|
40
|
-
|
41
|
-
task all: :environment do
|
42
|
-
ActiveSupport::Deprecation.warn('`rake chewy:update:all` is deprecated and will be removed soon. Use `rake chewy:update` instead')
|
107
|
+
task clean_journal: :environment do |_task, args|
|
108
|
+
ActiveSupport::Deprecation.warn '`rake chewy:clean_journal` is deprecated and will be removed soon, use `rake chewy:journal:clean` instead'
|
43
109
|
|
44
|
-
|
45
|
-
Chewy::RakeHelper.update_all
|
46
|
-
end
|
47
|
-
end
|
110
|
+
Chewy::Journal.new.clean(args.extras.first)
|
48
111
|
end
|
49
112
|
end
|