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/index.rb
CHANGED
@@ -2,6 +2,7 @@ require 'chewy/search'
|
|
2
2
|
require 'chewy/index/actions'
|
3
3
|
require 'chewy/index/aliases'
|
4
4
|
require 'chewy/index/settings'
|
5
|
+
require 'chewy/index/specification'
|
5
6
|
|
6
7
|
module Chewy
|
7
8
|
class Index
|
@@ -17,159 +18,259 @@ module Chewy
|
|
17
18
|
class_attribute :_settings
|
18
19
|
self._settings = Chewy::Index::Settings.new
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
21
|
+
class << self
|
22
|
+
# @overload index_name(suggest)
|
23
|
+
# If suggested name is passed, it is set up as the new base name for
|
24
|
+
# the index. Used for the index base name redefinition.
|
25
|
+
#
|
26
|
+
# @example
|
27
|
+
# class UsersIndex < Chewy::Index
|
28
|
+
# index_name :legacy_users
|
29
|
+
# end
|
30
|
+
# UsersIndex.index_name # => 'legacy_users'
|
31
|
+
#
|
32
|
+
# @param suggest [String, Symbol] suggested base name
|
33
|
+
# @return [String] new base name
|
34
|
+
#
|
35
|
+
# @overload index_name(prefix: nil, suffix: nil)
|
36
|
+
# If suggested name is not passed, returns the base name accompanied
|
37
|
+
# with the prefix (if any) and suffix (if passed).
|
38
|
+
#
|
39
|
+
# @example
|
40
|
+
# class UsersIndex < Chewy::Index
|
41
|
+
# end
|
42
|
+
#
|
43
|
+
# Chewy.settings = {prefix: 'test'}
|
44
|
+
# UsersIndex.index_name # => 'test_users'
|
45
|
+
# UsersIndex.index_name(prefix: 'foobar') # => 'foobar_users'
|
46
|
+
# UsersIndex.index_name(suffix: '2017') # => 'test_users_2017'
|
47
|
+
# UsersIndex.index_name(prefix: '', suffix: '2017') # => 'users_2017'
|
48
|
+
#
|
49
|
+
# @param prefix [String] index name prefix, uses {.prefix} method by default
|
50
|
+
# @param suffix [String] index name suffix, used for creating several indexes for the same alias during the zero-downtime reset
|
51
|
+
# @raise [UndefinedIndex] if the base name is blank
|
52
|
+
# @return [String] result index name
|
53
|
+
def index_name(suggest = nil, prefix: nil, suffix: nil)
|
54
|
+
if suggest
|
55
|
+
@base_name = suggest.to_s.presence
|
56
|
+
else
|
57
|
+
[
|
58
|
+
prefix || prefix_with_deprecation,
|
59
|
+
base_name,
|
60
|
+
suffix
|
61
|
+
].reject(&:blank?).join('_')
|
40
62
|
end
|
41
63
|
end
|
42
|
-
@index_name or raise UndefinedIndex
|
43
|
-
end
|
44
64
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
65
|
+
# Base name for the index. Uses the default value inferred from the
|
66
|
+
# class name unless redefined.
|
67
|
+
#
|
68
|
+
# @example
|
69
|
+
# class Namespace::UsersIndex < Chewy::Index
|
70
|
+
# end
|
71
|
+
# UsersIndex.index_name # => 'users'
|
72
|
+
#
|
73
|
+
# Class.new(Chewy::Index).base_name # => raises UndefinedIndex
|
74
|
+
#
|
75
|
+
# @raise [UndefinedIndex] when the base name is blank
|
76
|
+
# @return [String] current base name
|
77
|
+
def base_name
|
78
|
+
@base_name ||= name.sub(/Index\z/, '').demodulize.underscore if name
|
79
|
+
raise UndefinedIndex if @base_name.blank?
|
80
|
+
@base_name
|
81
|
+
end
|
50
82
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
# ...
|
65
|
-
# end # defines VehiclesIndex::Cars type
|
66
|
-
#
|
67
|
-
# define_type Vehicle.motocycles.includes(:manufacturer), name: 'motocycles' do
|
68
|
-
# ...
|
69
|
-
# end # defines VehiclesIndex::Motocycles type
|
70
|
-
# end
|
71
|
-
#
|
72
|
-
# For plain objects:
|
73
|
-
#
|
74
|
-
# class PlanesIndex < Chewy::Index
|
75
|
-
# define_type :plane do
|
76
|
-
# ...
|
77
|
-
# end # defines PlanesIndex::Plane type
|
78
|
-
# end
|
79
|
-
#
|
80
|
-
# The main difference between using plain objects or ActiveRecord models for indexing
|
81
|
-
# is import. If you will call `CarsIndex::Car.import` - it will import all the cars
|
82
|
-
# automatically, while `PlanesIndex::Plane.import(my_planes)` requires import data to be
|
83
|
-
# passed.
|
84
|
-
#
|
85
|
-
def self.define_type(target, options = {}, &block)
|
86
|
-
type_class = Chewy.create_type(self, target, options, &block)
|
87
|
-
self.type_hash = type_hash.merge(type_class.type_name => type_class)
|
88
|
-
|
89
|
-
unless respond_to?(type_class.type_name)
|
90
|
-
class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
91
|
-
def self.#{type_class.type_name}
|
92
|
-
ActiveSupport::Deprecation.warn("`#{self}.#{type_class.type_name}` accessor is deprecated and will be removed soon. Use `#{type_class}` directly instead.")
|
93
|
-
type_hash['#{type_class.type_name}']
|
94
|
-
end
|
95
|
-
METHOD
|
83
|
+
# Similar to the {.base_name} but respects the class namespace, also,
|
84
|
+
# can't be redefined. Used to reference index with the string identifier
|
85
|
+
#
|
86
|
+
# @example
|
87
|
+
# class Namespace::UsersIndex < Chewy::Index
|
88
|
+
# end
|
89
|
+
# UsersIndex.derivable_name # => 'namespace/users'
|
90
|
+
#
|
91
|
+
# Class.new(Chewy::Index).derivable_name # => nil
|
92
|
+
#
|
93
|
+
# @return [String, nil] derivable name or nil when it is impossible to calculate
|
94
|
+
def derivable_name
|
95
|
+
@derivable_name ||= name.sub(/Index\z/, '').underscore if name
|
96
96
|
end
|
97
|
-
end
|
98
97
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
else
|
114
|
-
type_hash.values
|
98
|
+
# Used as a default value for {.index_name}. Return prefix from the configuration
|
99
|
+
# but can be redefined per-index to be more dynamic.
|
100
|
+
#
|
101
|
+
# @example
|
102
|
+
# class UsersIndex < Chewy::Index
|
103
|
+
# def self.prefix
|
104
|
+
# 'foobar'
|
105
|
+
# end
|
106
|
+
# end
|
107
|
+
# UsersIndex.index_name # => 'foobar_users'
|
108
|
+
#
|
109
|
+
# @return [String] prefix
|
110
|
+
def prefix
|
111
|
+
Chewy.configuration[:prefix]
|
115
112
|
end
|
116
|
-
end
|
117
113
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
114
|
+
# Defines type for the index. Arguments depends on adapter used. For
|
115
|
+
# ActiveRecord you can pass model or scope and options
|
116
|
+
#
|
117
|
+
# class CarsIndex < Chewy::Index
|
118
|
+
# define_type Car do
|
119
|
+
# ...
|
120
|
+
# end # defines VehiclesIndex::Car type
|
121
|
+
# end
|
122
|
+
#
|
123
|
+
# Type name might be passed in complicated cases:
|
124
|
+
#
|
125
|
+
# class VehiclesIndex < Chewy::Index
|
126
|
+
# define_type Vehicle.cars.includes(:manufacturer), name: 'cars' do
|
127
|
+
# ...
|
128
|
+
# end # defines VehiclesIndex::Cars type
|
129
|
+
#
|
130
|
+
# define_type Vehicle.motocycles.includes(:manufacturer), name: 'motocycles' do
|
131
|
+
# ...
|
132
|
+
# end # defines VehiclesIndex::Motocycles type
|
133
|
+
# end
|
134
|
+
#
|
135
|
+
# For plain objects:
|
136
|
+
#
|
137
|
+
# class PlanesIndex < Chewy::Index
|
138
|
+
# define_type :plane do
|
139
|
+
# ...
|
140
|
+
# end # defines PlanesIndex::Plane type
|
141
|
+
# end
|
142
|
+
#
|
143
|
+
# The main difference between using plain objects or ActiveRecord models for indexing
|
144
|
+
# is import. If you will call `CarsIndex::Car.import` - it will import all the cars
|
145
|
+
# automatically, while `PlanesIndex::Plane.import(my_planes)` requires import data to be
|
146
|
+
# passed.
|
147
|
+
#
|
148
|
+
def define_type(target, options = {}, &block)
|
149
|
+
type_class = Chewy.create_type(self, target, options, &block)
|
150
|
+
self.type_hash = type_hash.merge(type_class.type_name => type_class)
|
151
|
+
end
|
125
152
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
#
|
145
|
-
def self.settings(params)
|
146
|
-
self._settings = Chewy::Index::Settings.new params
|
147
|
-
end
|
153
|
+
# Types method has double usage.
|
154
|
+
# If no arguments are passed - it returns array of defined types:
|
155
|
+
#
|
156
|
+
# UsersIndex.types # => [UsersIndex::Admin, UsersIndex::Manager, UsersIndex::User]
|
157
|
+
#
|
158
|
+
# If arguments are passed it treats like a part of chainable query DSL and
|
159
|
+
# adds types array for index to select.
|
160
|
+
#
|
161
|
+
# UsersIndex.filters { name =~ 'ro' }.types(:admin, :manager)
|
162
|
+
# UsersIndex.types(:admin, :manager).filters { name =~ 'ro' } # the same as the first example
|
163
|
+
#
|
164
|
+
def types(*args)
|
165
|
+
if args.present?
|
166
|
+
all.types(*args)
|
167
|
+
else
|
168
|
+
type_hash.values
|
169
|
+
end
|
170
|
+
end
|
148
171
|
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
172
|
+
# Returns defined types names:
|
173
|
+
#
|
174
|
+
# UsersIndex.type_names # => ['admin', 'manager', 'user']
|
175
|
+
#
|
176
|
+
def type_names
|
177
|
+
type_hash.keys
|
178
|
+
end
|
154
179
|
|
155
|
-
|
180
|
+
# Returns named type:
|
181
|
+
#
|
182
|
+
# UserIndex.type('admin') # => UsersIndex::Admin
|
183
|
+
#
|
184
|
+
def type(type_name)
|
185
|
+
type_hash.fetch(type_name) { raise UndefinedType, "Unknown type in #{name}: #{type_name}" }
|
186
|
+
end
|
156
187
|
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
188
|
+
# Used as a part of index definition DSL. Defines settings:
|
189
|
+
#
|
190
|
+
# class UsersIndex < Chewy::Index
|
191
|
+
# settings analysis: {
|
192
|
+
# analyzer: {
|
193
|
+
# name: {
|
194
|
+
# tokenizer: 'standard',
|
195
|
+
# filter: ['lowercase', 'icu_folding', 'names_nysiis']
|
196
|
+
# }
|
197
|
+
# }
|
198
|
+
# }
|
199
|
+
# end
|
200
|
+
#
|
201
|
+
# See http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/indices-update-settings.html
|
202
|
+
# for more details
|
203
|
+
#
|
204
|
+
# It is possible to store analyzers settings in Chewy repositories
|
205
|
+
# and link them form index class. See `Chewy::Index::Settings` for details.
|
206
|
+
#
|
207
|
+
def settings(params = {}, &block)
|
208
|
+
self._settings = Chewy::Index::Settings.new(params, &block)
|
209
|
+
end
|
161
210
|
|
162
|
-
|
163
|
-
|
164
|
-
|
211
|
+
# Returns list of public class methods defined in current index
|
212
|
+
#
|
213
|
+
def scopes
|
214
|
+
public_methods - Chewy::Index.public_methods
|
215
|
+
end
|
165
216
|
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
217
|
+
def settings_hash
|
218
|
+
_settings.to_hash
|
219
|
+
end
|
220
|
+
|
221
|
+
def mappings_hash
|
222
|
+
mappings = types.map(&:mappings_hash).inject(:merge)
|
223
|
+
mappings.present? ? {mappings: mappings} : {}
|
224
|
+
end
|
225
|
+
|
226
|
+
# Returns a hash containing the index settings and mappings
|
227
|
+
# Used for the ES index creation as body.
|
228
|
+
#
|
229
|
+
# @see Chewy::Index::Specification
|
230
|
+
# @return [Hash] specification as a hash
|
231
|
+
def specification_hash
|
232
|
+
[settings_hash, mappings_hash].inject(:merge)
|
233
|
+
end
|
234
|
+
|
235
|
+
def index_params
|
236
|
+
ActiveSupport::Deprecation.warn '`Chewy::Index.index_params` is deprecated and will be removed soon, use `Chewy::Index.specification_hash`'
|
237
|
+
specification_hash
|
238
|
+
end
|
239
|
+
|
240
|
+
# @see Chewy::Index::Specification
|
241
|
+
# @return [Chewy::Index::Specification] a specification object instance for this particular index
|
242
|
+
def specification
|
243
|
+
@specification ||= Specification.new(self)
|
244
|
+
end
|
170
245
|
|
171
|
-
|
172
|
-
|
246
|
+
def derivable_index_name
|
247
|
+
ActiveSupport::Deprecation.warn '`Chewy::Index.derivable_index_name` is deprecated and will be removed soon, use `Chewy::Index.derivable_name` instead'
|
248
|
+
derivable_name
|
249
|
+
end
|
250
|
+
|
251
|
+
# Handling old default_prefix if it is not defined.
|
252
|
+
def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissing
|
253
|
+
if name == :default_prefix
|
254
|
+
ActiveSupport::Deprecation.warn '`Chewy::Index.default_prefix` is deprecated and will be removed soon, use `Chewy::Index.prefix` instead'
|
255
|
+
prefix
|
256
|
+
else
|
257
|
+
super
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
def prefix_with_deprecation
|
262
|
+
if respond_to?(:default_prefix)
|
263
|
+
ActiveSupport::Deprecation.warn '`Chewy::Index.default_prefix` is deprecated and will be removed soon, define `Chewy::Index.prefix` method instead'
|
264
|
+
default_prefix
|
265
|
+
else
|
266
|
+
prefix
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
def build_index_name(*args)
|
271
|
+
ActiveSupport::Deprecation.warn '`Chewy::Index.build_index_name` is deprecated and will be removed soon, use `Chewy::Index.index_name` instead'
|
272
|
+
index_name(args.extract_options!)
|
273
|
+
end
|
173
274
|
end
|
174
275
|
end
|
175
276
|
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Chewy
|
2
|
+
# A class to perform journal-related actions for the specified indexes/types.
|
3
|
+
#
|
4
|
+
# @example
|
5
|
+
# journal = Chewy::Journal.new('places#city', UsersIndex)
|
6
|
+
# journal.apply(20.minutes.ago)
|
7
|
+
# journal.clean
|
8
|
+
#
|
9
|
+
class Journal
|
10
|
+
# @param only [Array<String, Chewy::Index, Chewy::Type>] indexes/types or even string references to perform actions on
|
11
|
+
def initialize(*only)
|
12
|
+
@only = only
|
13
|
+
end
|
14
|
+
|
15
|
+
# Applies all changes that were done since the specified time to the
|
16
|
+
# specified indexes/types.
|
17
|
+
#
|
18
|
+
# @param since_time [Time, DateTime] timestamp from which changes will be applied
|
19
|
+
# @param retries [Integer] maximum number of attempts to make journal empty, 10 by default
|
20
|
+
# @return [Integer] the amount of journal entries found
|
21
|
+
def apply(since_time, retries: 10, **import_options)
|
22
|
+
stage = 1
|
23
|
+
since_time -= 1
|
24
|
+
count = 0
|
25
|
+
while stage <= retries
|
26
|
+
entries = Chewy::Stash::Journal.entries(since_time, only: @only).to_a.presence or break
|
27
|
+
count += entries.size
|
28
|
+
groups = reference_groups(entries)
|
29
|
+
ActiveSupport::Notifications.instrument 'apply_journal.chewy', stage: stage, groups: groups
|
30
|
+
groups.each do |type, references|
|
31
|
+
type.import(references, import_options.merge(journal: false))
|
32
|
+
end
|
33
|
+
stage += 1
|
34
|
+
since_time = entries.map(&:created_at).max
|
35
|
+
end
|
36
|
+
count
|
37
|
+
end
|
38
|
+
|
39
|
+
# Cleans journal for the specified indexes/types.
|
40
|
+
#
|
41
|
+
# @param until_time [Time, DateTime] time to clean up until it
|
42
|
+
# @return [Hash] delete_by_query ES API call result
|
43
|
+
def clean(until_time = nil)
|
44
|
+
Chewy::Stash::Journal.clean(until_time, only: @only)
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def reference_groups(entries)
|
50
|
+
entries.group_by(&:type).map do |type, grouped_entries|
|
51
|
+
[type, grouped_entries.map(&:references).inject(:|)]
|
52
|
+
end.to_h
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/lib/chewy/log_subscriber.rb
CHANGED
@@ -16,17 +16,17 @@ module Chewy
|
|
16
16
|
render_action('Delete by Query', event) { |payload| payload[:request] }
|
17
17
|
end
|
18
18
|
|
19
|
-
def render_action
|
19
|
+
def render_action(action, event)
|
20
20
|
payload = event.payload
|
21
|
-
description =
|
21
|
+
description = yield(payload)
|
22
22
|
|
23
|
-
if description.
|
24
|
-
subject = payload[:type].presence || payload[:index]
|
25
|
-
action = "#{subject} #{action} (#{event.duration.round(1)}ms)"
|
26
|
-
action = color(action, GREEN, true)
|
23
|
+
return if description.blank?
|
27
24
|
|
28
|
-
|
29
|
-
|
25
|
+
subject = payload[:type].presence || payload[:index]
|
26
|
+
action = "#{subject} #{action} (#{event.duration.round(1)}ms)"
|
27
|
+
action = color(action, GREEN, true)
|
28
|
+
|
29
|
+
debug(" #{action} #{description}")
|
30
30
|
end
|
31
31
|
end
|
32
32
|
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require_relative 'search_index_receiver'
|
2
|
+
|
3
|
+
module Chewy
|
4
|
+
module Minitest
|
5
|
+
module Helpers
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
# Assert that an index *changes* during a block.
|
9
|
+
# @param index [Chewy::Type] the index / type to watch, eg EntitiesIndex::Entity.
|
10
|
+
# @param strategy [Symbol] the Chewy strategy to use around the block. See Chewy docs.
|
11
|
+
# @param bypass_actual_index [true, false]
|
12
|
+
# True to preempt the http call to Elastic, false otherwise.
|
13
|
+
# Should be set to true unless actually testing search functionality.
|
14
|
+
#
|
15
|
+
# @return [SearchIndexReceiver] for optional further assertions on the nature of the index changes.
|
16
|
+
#
|
17
|
+
def assert_indexes(index, strategy: :atomic, bypass_actual_index: true)
|
18
|
+
type = Chewy.derive_type index
|
19
|
+
receiver = SearchIndexReceiver.new
|
20
|
+
|
21
|
+
bulk_method = type.method :bulk
|
22
|
+
# Manually mocking #bulk because we need to properly capture `self`
|
23
|
+
bulk_mock = lambda do |*bulk_args|
|
24
|
+
receiver.catch bulk_args, self
|
25
|
+
|
26
|
+
bulk_method.call(*bulk_args) unless bypass_actual_index
|
27
|
+
|
28
|
+
{}
|
29
|
+
end
|
30
|
+
|
31
|
+
type.define_singleton_method :bulk, bulk_mock
|
32
|
+
|
33
|
+
Chewy.strategy(strategy) do
|
34
|
+
yield
|
35
|
+
end
|
36
|
+
|
37
|
+
type.define_singleton_method :bulk, bulk_method
|
38
|
+
|
39
|
+
assert_includes receiver.updated_indexes, index, "Expected #{index} to be updated but it wasn't"
|
40
|
+
|
41
|
+
receiver
|
42
|
+
end
|
43
|
+
|
44
|
+
# Run indexing for the database changes during the block provided.
|
45
|
+
# By default, indexing is run at the end of the block.
|
46
|
+
# @param strategy [Symbol] the Chewy index update strategy see Chewy docs.
|
47
|
+
def run_indexing(strategy: :atomic)
|
48
|
+
Chewy.strategy strategy do
|
49
|
+
yield
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
module ClassMethods
|
54
|
+
# Declare that all tests in this file require real indexing, always.
|
55
|
+
# In my completely unscientific experiments, this roughly doubled test runtime.
|
56
|
+
# Use with trepidation.
|
57
|
+
def index_everything!
|
58
|
+
setup do
|
59
|
+
Chewy.strategy :urgent
|
60
|
+
end
|
61
|
+
|
62
|
+
teardown do
|
63
|
+
Chewy.strategy.pop
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
included do
|
69
|
+
teardown do
|
70
|
+
# always destroy indexes between tests
|
71
|
+
# Prevent croll pollution of test cases due to indexing
|
72
|
+
Chewy.massacre
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# Test helper class to provide minitest hooks for Chewy::Index testing.
|
2
|
+
#
|
3
|
+
# @note Intended to be used in conjunction with a test helper which mocks over the #bulk
|
4
|
+
# method on a {Chewy::Type} class. (See SearchTestHelper)
|
5
|
+
#
|
6
|
+
# The class will capture the data from the *param on the Chewy::Type.bulk method and
|
7
|
+
# aggregate the data for test analysis.
|
8
|
+
class SearchIndexReceiver
|
9
|
+
def initialize
|
10
|
+
@mutations = {}
|
11
|
+
end
|
12
|
+
|
13
|
+
# @param bulk_params [Hash] the bulk_params that should be sent to the Chewy::Type.bulk method.
|
14
|
+
# @param type [Chewy::Type] the type executing this query.
|
15
|
+
def catch(bulk_params, type)
|
16
|
+
Array.wrap(bulk_params).map { |y| y[:body] }.flatten.each do |update|
|
17
|
+
if update[:delete]
|
18
|
+
mutation_for(type).deletes << update[:delete][:_id]
|
19
|
+
elsif update[:index]
|
20
|
+
mutation_for(type).indexes << update[:index]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# @param index [Chewy::Index] return only index requests to the specified {Chewy::Type} index.
|
26
|
+
# @return [Hash] the index changes captured by the mock.
|
27
|
+
def indexes_for(index = nil)
|
28
|
+
if index
|
29
|
+
mutation_for(index).indexes
|
30
|
+
else
|
31
|
+
Hash[
|
32
|
+
@mutations.map { |a, b| [a, b.indexes] }
|
33
|
+
]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
alias_method :indexes, :indexes_for
|
37
|
+
|
38
|
+
# @param index [Chewy::Index] return only delete requests to the specified {Chewy::Type} index.
|
39
|
+
# @return [Hash] the index deletes captured by the mock.
|
40
|
+
def deletes_for(index = nil)
|
41
|
+
if index
|
42
|
+
mutation_for(index).deletes
|
43
|
+
else
|
44
|
+
Hash[
|
45
|
+
@mutations.map { |a, b| [a, b.deletes] }
|
46
|
+
]
|
47
|
+
end
|
48
|
+
end
|
49
|
+
alias_method :deletes, :deletes_for
|
50
|
+
|
51
|
+
# Check to see if a given object has been indexed.
|
52
|
+
# @param obj [#id] obj the object to look for.
|
53
|
+
# @param type [Chewy::Type] what type the object should be indexed as.
|
54
|
+
# @return [true, false] if the object was indexed.
|
55
|
+
def indexed?(obj, type)
|
56
|
+
indexes_for(type).map { |i| i[:_id] }.include? obj.id
|
57
|
+
end
|
58
|
+
|
59
|
+
# Check to see if a given object has been deleted.
|
60
|
+
# @param obj [#id] obj the object to look for.
|
61
|
+
# @param type [Chewy::Type] what type the object should have been deleted from.
|
62
|
+
# @return [true, false] if the object was deleted.
|
63
|
+
def deleted?(obj, type)
|
64
|
+
deletes_for(type).include? obj.id
|
65
|
+
end
|
66
|
+
|
67
|
+
# @return [Array<Chewy::Type>] a list of types indexes changed.
|
68
|
+
def updated_indexes
|
69
|
+
@mutations.keys
|
70
|
+
end
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
# Get the mutation object for a given type.
|
75
|
+
# @param type [Chewy::Type] the index type to fetch.
|
76
|
+
# @return [#indexes, #deletes] an object with a list of indexes and a list of deletes.
|
77
|
+
def mutation_for(type)
|
78
|
+
@mutations[type] ||= OpenStruct.new(indexes: [], deletes: [])
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'chewy/minitest/helpers'
|