chewy 0.10.0 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (157) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +240 -0
  3. data/.rubocop.yml +25 -25
  4. data/Appraisals +12 -10
  5. data/CHANGELOG.md +252 -263
  6. data/Gemfile +5 -1
  7. data/LICENSE.txt +1 -1
  8. data/README.md +142 -78
  9. data/chewy.gemspec +10 -12
  10. data/gemfiles/{rails.4.2.mongoid.5.1.gemfile → rails.5.2.activerecord.gemfile} +6 -4
  11. data/gemfiles/{rails.4.2.activerecord.gemfile → rails.5.2.mongoid.6.4.gemfile} +6 -4
  12. data/gemfiles/{rails.4.0.activerecord.gemfile → rails.6.0.activerecord.gemfile} +6 -3
  13. data/gemfiles/rails.6.1.activerecord.gemfile +19 -0
  14. data/gemfiles/sequel.4.45.gemfile +2 -2
  15. data/lib/chewy.rb +2 -1
  16. data/lib/chewy/backports/duplicable.rb +1 -1
  17. data/lib/chewy/config.rb +10 -39
  18. data/lib/chewy/fields/base.rb +40 -28
  19. data/lib/chewy/fields/root.rb +18 -11
  20. data/lib/chewy/index.rb +3 -1
  21. data/lib/chewy/index/actions.rb +27 -15
  22. data/lib/chewy/index/settings.rb +2 -0
  23. data/lib/chewy/index/specification.rb +12 -10
  24. data/lib/chewy/minitest/helpers.rb +6 -6
  25. data/lib/chewy/minitest/search_index_receiver.rb +17 -17
  26. data/lib/chewy/multi_search.rb +62 -0
  27. data/lib/chewy/railtie.rb +4 -4
  28. data/lib/chewy/rake_helper.rb +5 -5
  29. data/lib/chewy/rspec/update_index.rb +3 -5
  30. data/lib/chewy/search.rb +4 -11
  31. data/lib/chewy/search/loader.rb +1 -1
  32. data/lib/chewy/search/pagination/will_paginate.rb +4 -2
  33. data/lib/chewy/search/parameters.rb +24 -6
  34. data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
  35. data/lib/chewy/search/parameters/concerns/query_storage.rb +4 -3
  36. data/lib/chewy/search/parameters/indices.rb +123 -0
  37. data/lib/chewy/search/parameters/none.rb +1 -3
  38. data/lib/chewy/search/request.rb +100 -74
  39. data/lib/chewy/search/scrolling.rb +7 -6
  40. data/lib/chewy/stash.rb +30 -21
  41. data/lib/chewy/strategy/active_job.rb +1 -1
  42. data/lib/chewy/strategy/atomic.rb +1 -1
  43. data/lib/chewy/strategy/sidekiq.rb +1 -1
  44. data/lib/chewy/type.rb +5 -2
  45. data/lib/chewy/type/adapter/active_record.rb +1 -1
  46. data/lib/chewy/type/adapter/base.rb +9 -9
  47. data/lib/chewy/type/adapter/mongoid.rb +2 -4
  48. data/lib/chewy/type/adapter/orm.rb +7 -4
  49. data/lib/chewy/type/adapter/sequel.rb +5 -7
  50. data/lib/chewy/type/crutch.rb +1 -1
  51. data/lib/chewy/type/import.rb +13 -11
  52. data/lib/chewy/type/import/bulk_builder.rb +1 -1
  53. data/lib/chewy/type/import/bulk_request.rb +4 -2
  54. data/lib/chewy/type/import/journal_builder.rb +3 -3
  55. data/lib/chewy/type/import/routine.rb +3 -3
  56. data/lib/chewy/type/mapping.rb +42 -36
  57. data/lib/chewy/type/observe.rb +16 -12
  58. data/lib/chewy/type/syncer.rb +15 -14
  59. data/lib/chewy/type/witchcraft.rb +11 -7
  60. data/lib/chewy/type/wrapper.rb +14 -4
  61. data/lib/chewy/version.rb +1 -1
  62. data/lib/sequel/plugins/chewy_observe.rb +4 -19
  63. data/migration_guide.md +18 -0
  64. data/spec/chewy/config_spec.rb +16 -21
  65. data/spec/chewy/fields/base_spec.rb +70 -70
  66. data/spec/chewy/fields/root_spec.rb +56 -9
  67. data/spec/chewy/index/actions_spec.rb +63 -7
  68. data/spec/chewy/index/specification_spec.rb +25 -16
  69. data/spec/chewy/index_spec.rb +75 -45
  70. data/spec/chewy/journal_spec.rb +33 -29
  71. data/spec/chewy/minitest/search_index_receiver_spec.rb +11 -9
  72. data/spec/chewy/multi_search_spec.rb +85 -0
  73. data/spec/chewy/rake_helper_spec.rb +123 -95
  74. data/spec/chewy/rspec/update_index_spec.rb +47 -46
  75. data/spec/chewy/runtime_spec.rb +2 -2
  76. data/spec/chewy/search/pagination/kaminari_spec.rb +7 -3
  77. data/spec/chewy/search/pagination/will_paginate_spec.rb +9 -3
  78. data/spec/chewy/search/parameters/indices_spec.rb +190 -0
  79. data/spec/chewy/search/parameters/none_spec.rb +1 -1
  80. data/spec/chewy/search/parameters_spec.rb +21 -4
  81. data/spec/chewy/search/request_spec.rb +101 -70
  82. data/spec/chewy/search/response_spec.rb +27 -17
  83. data/spec/chewy/search/scrolling_spec.rb +25 -16
  84. data/spec/chewy/search_spec.rb +49 -35
  85. data/spec/chewy/stash_spec.rb +15 -13
  86. data/spec/chewy/strategy/active_job_spec.rb +15 -2
  87. data/spec/chewy/strategy/shoryuken_spec.rb +8 -2
  88. data/spec/chewy/strategy/sidekiq_spec.rb +6 -2
  89. data/spec/chewy/type/adapter/active_record_spec.rb +16 -4
  90. data/spec/chewy/type/import/bulk_builder_spec.rb +9 -94
  91. data/spec/chewy/type/import/journal_builder_spec.rb +17 -15
  92. data/spec/chewy/type/import_spec.rb +6 -0
  93. data/spec/chewy/type/mapping_spec.rb +51 -18
  94. data/spec/chewy/type/observe_spec.rb +4 -4
  95. data/spec/chewy/type/witchcraft_spec.rb +31 -0
  96. data/spec/chewy/type/wrapper_spec.rb +3 -1
  97. data/spec/chewy_spec.rb +0 -7
  98. data/spec/spec_helper.rb +5 -1
  99. data/spec/support/active_record.rb +20 -0
  100. metadata +46 -116
  101. data/.travis.yml +0 -53
  102. data/LEGACY_DSL.md +0 -497
  103. data/gemfiles/rails.4.1.activerecord.gemfile +0 -14
  104. data/gemfiles/rails.5.0.activerecord.gemfile +0 -15
  105. data/gemfiles/rails.5.0.mongoid.6.0.gemfile +0 -15
  106. data/gemfiles/rails.5.1.activerecord.gemfile +0 -15
  107. data/gemfiles/rails.5.1.mongoid.6.1.gemfile +0 -15
  108. data/lib/chewy/query.rb +0 -1098
  109. data/lib/chewy/query/compose.rb +0 -68
  110. data/lib/chewy/query/criteria.rb +0 -191
  111. data/lib/chewy/query/filters.rb +0 -227
  112. data/lib/chewy/query/loading.rb +0 -111
  113. data/lib/chewy/query/nodes/and.rb +0 -25
  114. data/lib/chewy/query/nodes/base.rb +0 -17
  115. data/lib/chewy/query/nodes/bool.rb +0 -34
  116. data/lib/chewy/query/nodes/equal.rb +0 -34
  117. data/lib/chewy/query/nodes/exists.rb +0 -20
  118. data/lib/chewy/query/nodes/expr.rb +0 -28
  119. data/lib/chewy/query/nodes/field.rb +0 -110
  120. data/lib/chewy/query/nodes/has_child.rb +0 -15
  121. data/lib/chewy/query/nodes/has_parent.rb +0 -15
  122. data/lib/chewy/query/nodes/has_relation.rb +0 -59
  123. data/lib/chewy/query/nodes/match_all.rb +0 -11
  124. data/lib/chewy/query/nodes/missing.rb +0 -20
  125. data/lib/chewy/query/nodes/not.rb +0 -25
  126. data/lib/chewy/query/nodes/or.rb +0 -25
  127. data/lib/chewy/query/nodes/prefix.rb +0 -19
  128. data/lib/chewy/query/nodes/query.rb +0 -20
  129. data/lib/chewy/query/nodes/range.rb +0 -63
  130. data/lib/chewy/query/nodes/raw.rb +0 -15
  131. data/lib/chewy/query/nodes/regexp.rb +0 -35
  132. data/lib/chewy/query/nodes/script.rb +0 -20
  133. data/lib/chewy/query/pagination.rb +0 -25
  134. data/spec/chewy/query/criteria_spec.rb +0 -700
  135. data/spec/chewy/query/filters_spec.rb +0 -201
  136. data/spec/chewy/query/loading_spec.rb +0 -124
  137. data/spec/chewy/query/nodes/and_spec.rb +0 -12
  138. data/spec/chewy/query/nodes/bool_spec.rb +0 -14
  139. data/spec/chewy/query/nodes/equal_spec.rb +0 -32
  140. data/spec/chewy/query/nodes/exists_spec.rb +0 -18
  141. data/spec/chewy/query/nodes/has_child_spec.rb +0 -59
  142. data/spec/chewy/query/nodes/has_parent_spec.rb +0 -59
  143. data/spec/chewy/query/nodes/match_all_spec.rb +0 -11
  144. data/spec/chewy/query/nodes/missing_spec.rb +0 -16
  145. data/spec/chewy/query/nodes/not_spec.rb +0 -13
  146. data/spec/chewy/query/nodes/or_spec.rb +0 -12
  147. data/spec/chewy/query/nodes/prefix_spec.rb +0 -16
  148. data/spec/chewy/query/nodes/query_spec.rb +0 -12
  149. data/spec/chewy/query/nodes/range_spec.rb +0 -32
  150. data/spec/chewy/query/nodes/raw_spec.rb +0 -11
  151. data/spec/chewy/query/nodes/regexp_spec.rb +0 -43
  152. data/spec/chewy/query/nodes/script_spec.rb +0 -15
  153. data/spec/chewy/query/pagination/kaminari_spec.rb +0 -5
  154. data/spec/chewy/query/pagination/will_paginate_spec.rb +0 -5
  155. data/spec/chewy/query/pagination_spec.rb +0 -39
  156. data/spec/chewy/query_spec.rb +0 -636
  157. data/spec/chewy/search/parameters/indices_boost_spec.rb +0 -83
@@ -2,14 +2,16 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "mongoid", "~> 5.1.0"
6
- gem "activesupport", "~> 4.2.0"
7
- gem "activejob", "~> 4.2.0"
5
+ gem "activerecord", "~> 5.2.0"
6
+ gem "activesupport", "~> 5.2.0"
7
+ gem "activejob", "~> 5.2.0"
8
8
  gem "resque", require: false
9
9
  gem "shoryuken", require: false
10
+ gem "aws-sdk-sqs", require: false
10
11
  gem "sidekiq", require: false
11
- gem "kaminari", "~> 0.17.0", require: false
12
+ gem "kaminari-core", "~> 1.1.0", require: false
12
13
  gem "will_paginate", require: false
13
14
  gem "parallel", require: false
15
+ gem 'rspec_junit_formatter', '~> 0.4.1'
14
16
 
15
17
  gemspec path: "../"
@@ -2,14 +2,16 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "activerecord", "~> 4.2.0"
6
- gem "activesupport", "~> 4.2.0"
7
- gem "activejob", "~> 4.2.0"
5
+ gem "mongoid", "~> 6.4.0"
6
+ gem "activesupport", "~> 5.2.0"
7
+ gem "activejob", "~> 5.2.0"
8
8
  gem "resque", require: false
9
9
  gem "shoryuken", require: false
10
+ gem "aws-sdk-sqs", require: false
10
11
  gem "sidekiq", require: false
11
- gem "kaminari", "~> 0.17.0", require: false
12
+ gem "kaminari-core", "~> 1.1.0", require: false
12
13
  gem "will_paginate", require: false
13
14
  gem "parallel", require: false
15
+ gem 'rspec_junit_formatter', '~> 0.4.1'
14
16
 
15
17
  gemspec path: "../"
@@ -2,13 +2,16 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "activerecord", "~> 4.0.0"
6
- gem "activesupport", "~> 4.0.0"
5
+ gem "activerecord", "~> 6.0.0"
6
+ gem "activesupport", "~> 6.0.0"
7
+ gem "activejob", "~> 6.0.0"
7
8
  gem "resque", require: false
8
9
  gem "shoryuken", require: false
10
+ gem "aws-sdk-sqs", require: false
9
11
  gem "sidekiq", require: false
10
- gem "kaminari", "~> 0.17.0", require: false
12
+ gem "kaminari-core", "~> 1.1.0", require: false
11
13
  gem "will_paginate", require: false
12
14
  gem "parallel", require: false
15
+ gem 'rspec_junit_formatter', '~> 0.4.1'
13
16
 
14
17
  gemspec path: "../"
@@ -0,0 +1,19 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "activerecord", "~> 6.1.0"
6
+ gem "activesupport", "~> 6.1.0"
7
+ gem "activejob", "~> 6.1.0"
8
+ gem "resque", require: false
9
+ gem "shoryuken", require: false
10
+ gem "aws-sdk-sqs", require: false
11
+ gem "sidekiq", require: false
12
+ gem "kaminari-core", "~> 1.1.0", require: false
13
+ gem "will_paginate", require: false
14
+ gem "parallel", require: false
15
+ gem 'rspec_junit_formatter', '~> 0.4.1'
16
+
17
+ gem 'rexml' if RUBY_VERSION >= '3.0.0'
18
+
19
+ gemspec path: "../"
@@ -3,8 +3,8 @@
3
3
  source "https://rubygems.org"
4
4
 
5
5
  gem "sequel", "~> 4.45.0"
6
- gem "activesupport", "~> 5.0.0"
7
- gem "kaminari-core", "~> 1.0.0", require: false
6
+ gem "activesupport", "~> 5.1.0"
7
+ gem "kaminari-core", "~> 1.1.0", require: false
8
8
  gem "will_paginate", require: false
9
9
  gem "parallel", require: false
10
10
 
data/lib/chewy.rb CHANGED
@@ -17,6 +17,7 @@ require 'active_support/core_ext/string/inflections'
17
17
  require 'i18n/core_ext/hash'
18
18
  require 'chewy/backports/deep_dup' unless Object.respond_to?(:deep_dup)
19
19
  require 'singleton'
20
+ require 'base64'
20
21
 
21
22
  require 'elasticsearch'
22
23
 
@@ -131,7 +132,7 @@ module Chewy
131
132
  def create_type(index, target, options = {}, &block)
132
133
  type = Class.new(Chewy::Type)
133
134
 
134
- adapter = adapters.find { |klass| klass.accepts?(target) }.new(target, options)
135
+ adapter = adapters.find { |klass| klass.accepts?(target) }.new(target, **options)
135
136
 
136
137
  index.const_set(adapter.name, type)
137
138
  type.send(:define_singleton_method, :index) { index }
@@ -79,7 +79,7 @@ end
79
79
  require 'bigdecimal'
80
80
  class BigDecimal
81
81
  begin
82
- BigDecimal.new('4.56').dup
82
+ BigDecimal('4.56').dup
83
83
 
84
84
  def duplicable?
85
85
  true
data/lib/chewy/config.rb CHANGED
@@ -2,65 +2,42 @@ module Chewy
2
2
  class Config
3
3
  include Singleton
4
4
 
5
- attr_accessor :settings, :transport_logger, :transport_tracer, :logger,
6
-
7
- # Default query compilation mode. `:must` by default.
8
- # See Chewy::Query#query_mode for details
9
- #
10
- :query_mode,
11
-
12
- # Default filters compilation mode. `:and` by default.
13
- # See Chewy::Query#filter_mode for details
14
- #
15
- :filter_mode,
16
-
17
- # Default post_filters compilation mode. `nil` by default.
18
- # See Chewy::Query#post_filter_mode for details
19
- #
20
- :post_filter_mode,
21
-
5
+ attr_accessor :settings, :logger,
22
6
  # The first strategy in stack. `:base` by default.
23
7
  # If you need to return to the previous chewy behavior -
24
8
  # just set it to `:bypass`
25
9
  #
26
10
  :root_strategy,
27
-
28
11
  # Default request strategy middleware, used in e.g
29
12
  # Rails controllers. See Chewy::Railtie::RequestStrategy
30
13
  # for more info.
31
14
  #
32
15
  :request_strategy,
33
-
34
16
  # Use after_commit callbacks for RDBMS instead of
35
17
  # after_save and after_destroy. True by default. Useful
36
18
  # in tests with transactional fixtures or transactional
37
19
  # DatabaseCleaner strategy.
38
20
  #
39
21
  :use_after_commit_callbacks,
40
-
41
22
  # Where Chewy expects to find index definitions
42
23
  # within a Rails app folder.
43
24
  :indices_path,
44
-
45
25
  # Set index refresh_interval setting to -1 before reset and put the original value after.
46
26
  # If setting not present, put back to default 1s
47
27
  # https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html
48
28
  :reset_disable_refresh_interval,
49
-
50
29
  # Set number_of_replicas to 0 before reset and put the original value after
51
30
  # https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-update-settings.html
52
31
  :reset_no_replicas,
53
-
54
32
  # Refresh or not when import async (sidekiq, resque, activejob)
55
33
  :disable_refresh_async,
56
-
57
34
  # Default options for root of Chewy type. Allows to set default options
58
35
  # for type mappings like `_all`.
59
36
  :default_root_options,
37
+ # Default field type for any field in any Chewy type. Defaults to 'text'.
38
+ :default_field_type
60
39
 
61
- # Default field type for any field in any Chewy type. Defaults to 'string'.
62
- :default_field_type,
63
-
40
+ attr_reader :transport_logger, :transport_tracer,
64
41
  # Chewy search request DSL base class, used by every index.
65
42
  :search_class
66
43
 
@@ -70,8 +47,6 @@ module Chewy
70
47
 
71
48
  def initialize
72
49
  @settings = {}
73
- @query_mode = :must
74
- @filter_mode = :and
75
50
  @root_strategy = :base
76
51
  @request_strategy = :atomic
77
52
  @use_after_commit_callbacks = true
@@ -80,8 +55,8 @@ module Chewy
80
55
  @disable_refresh_async = false
81
56
  @indices_path = 'app/chewy'
82
57
  @default_root_options = {}
83
- @default_field_type = 'string'.freeze
84
- self.search_class = Chewy::Search::Request
58
+ @default_field_type = 'text'.freeze
59
+ @search_class = build_search_class(Chewy::Search::Request)
85
60
  end
86
61
 
87
62
  def transport_logger=(logger)
@@ -94,10 +69,6 @@ module Chewy
94
69
  @transport_tracer = tracer
95
70
  end
96
71
 
97
- def search_class=(base)
98
- @search_class = build_search_class(base)
99
- end
100
-
101
72
  # Chewy core configurations. There is two ways to set it up:
102
73
  # use `Chewy.settings=` method or, for Rails application,
103
74
  # create `config/chewy.yml` file. Btw, `config/chewy.yml` supports
@@ -124,7 +95,7 @@ module Chewy
124
95
  #
125
96
  # :wait_for_status - if this option set - chewy actions such
126
97
  # as creating or deleting index, importing data will wait for
127
- # the status specified. Extremely useful for tests under havy
98
+ # the status specified. Extremely useful for tests under heavy
128
99
  # indexes manipulations.
129
100
  #
130
101
  # test:
@@ -153,7 +124,7 @@ module Chewy
153
124
 
154
125
  def yaml_settings
155
126
  @yaml_settings ||= begin
156
- if defined?(Rails)
127
+ if defined?(Rails::VERSION)
157
128
  file = Rails.root.join('config', 'chewy.yml')
158
129
 
159
130
  if File.exist?(file)
@@ -168,9 +139,9 @@ module Chewy
168
139
  def build_search_class(base)
169
140
  Class.new(base).tap do |search_class|
170
141
  if defined?(::Kaminari)
171
- search_class.include Chewy::Search::Pagination::Kaminari
142
+ search_class.send :include, Chewy::Search::Pagination::Kaminari
172
143
  elsif defined?(::WillPaginate)
173
- search_class.include Chewy::Search::Pagination::WillPaginate
144
+ search_class.send :include, Chewy::Search::Pagination::WillPaginate
174
145
  end
175
146
  end
176
147
  end
@@ -4,13 +4,18 @@ module Chewy
4
4
  attr_reader :name, :options, :value, :children
5
5
  attr_accessor :parent
6
6
 
7
- def initialize(name, options = {})
7
+ def initialize(name, value: nil, **options)
8
8
  @name = name.to_sym
9
- @options = options.deep_symbolize_keys
10
- @value = @options.delete(:value)
9
+ @options = {}
10
+ update_options!(**options)
11
+ @value = value
11
12
  @children = []
12
13
  end
13
14
 
15
+ def update_options!(**options)
16
+ @options = options
17
+ end
18
+
14
19
  def multi_field?
15
20
  children.present? && !object_field?
16
21
  end
@@ -28,38 +33,19 @@ module Chewy
28
33
  end
29
34
  mapping.reverse_merge!(options)
30
35
  mapping.reverse_merge!(type: (children.present? ? 'object' : Chewy.default_field_type))
36
+
31
37
  {name => mapping}
32
38
  end
33
39
 
34
40
  def compose(*objects)
35
- object = objects.first
41
+ result = evaluate(objects)
36
42
 
37
- result =
38
- if value && value.is_a?(Proc)
39
- if value.arity.zero?
40
- object.instance_exec(&value)
41
- elsif value.arity < 0
42
- value.call(*object)
43
- else
44
- value.call(*objects.first(value.arity))
45
- end
46
- elsif object.is_a?(Hash)
47
- if object.key?(name)
48
- object[name]
49
- else
50
- object[name.to_s]
51
- end
43
+ if children.present? && !multi_field?
44
+ result = if result.respond_to?(:to_ary)
45
+ result.to_ary.map { |item| compose_children(item, *objects) }
52
46
  else
53
- object.send(name)
47
+ compose_children(result, *objects)
54
48
  end
55
-
56
- if children.present? && !multi_field?
57
- result =
58
- if result.respond_to?(:to_ary)
59
- result.to_ary.map { |item| compose_children(item, *objects) }
60
- else
61
- compose_children(result, *objects)
62
- end
63
49
  end
64
50
 
65
51
  {name => result}
@@ -67,6 +53,32 @@ module Chewy
67
53
 
68
54
  private
69
55
 
56
+ def evaluate(objects)
57
+ object = objects.first
58
+
59
+ if value.is_a?(Proc)
60
+ if value.arity.zero?
61
+ object.instance_exec(&value)
62
+ elsif value.arity < 0
63
+ value.call(*object)
64
+ else
65
+ value.call(*objects.first(value.arity))
66
+ end
67
+ else
68
+ message = value.is_a?(Symbol) || value.is_a?(String) ? value.to_sym : name
69
+
70
+ if object.is_a?(Hash)
71
+ if object.key?(message)
72
+ object[message]
73
+ else
74
+ object[message.to_s]
75
+ end
76
+ else
77
+ object.send(message)
78
+ end
79
+ end
80
+ end
81
+
70
82
  def compose_children(value, *parent_objects)
71
83
  return unless value
72
84
 
@@ -6,14 +6,18 @@ module Chewy
6
6
  attr_reader :parent
7
7
  attr_reader :parent_id
8
8
 
9
- def initialize(*args)
10
- super(*args)
9
+ def initialize(name, **options)
10
+ super(name, **options)
11
11
 
12
- @id = @options.delete(:id) || options.delete(:_id)
13
- @parent = @options.delete(:parent) || options.delete(:_parent)
14
- @parent_id = @options.delete(:parent_id)
12
+ @value ||= -> { self }
15
13
  @dynamic_templates = []
16
- @options.delete(:type)
14
+ end
15
+
16
+ def update_options!(**options)
17
+ @id = options.fetch(:id, options.fetch(:_id, @id))
18
+ @parent = options.fetch(:parent, options.fetch(:_parent, @parent))
19
+ @parent_id = options.fetch(:parent_id, @parent_id)
20
+ @options.merge!(options.except(:id, :_id, :parent, :_parent, :parent_id, :type))
17
21
  end
18
22
 
19
23
  def mappings_hash
@@ -65,8 +69,11 @@ module Chewy
65
69
  # @param object [Object] a base object for composition
66
70
  # @param crutches [Object] any object that will be passed to every field value proc as a last argument
67
71
  # @param fields [Array<Symbol>] a list of fields to compose, every field will be composed if empty
68
- # @return [Hash] JSON-ready heash with stringifyed keys
72
+ # @return [Hash] JSON-ready hash with stringified keys
73
+ #
69
74
  def compose(object, crutches = nil, fields: [])
75
+ result = evaluate([object, crutches])
76
+
70
77
  if children.present?
71
78
  child_fields = if fields.present?
72
79
  child_hash.slice(*fields).values
@@ -74,13 +81,13 @@ module Chewy
74
81
  children
75
82
  end
76
83
 
77
- child_fields.each_with_object({}) do |field, result|
78
- result.merge!(field.compose(object, crutches) || {})
84
+ child_fields.each_with_object({}) do |field, memo|
85
+ memo.merge!(field.compose(result, crutches) || {})
79
86
  end.as_json
80
87
  elsif fields.present?
81
- object.as_json(only: fields)
88
+ result.as_json(only: fields, root: false)
82
89
  else
83
- object.as_json
90
+ result.as_json(root: false)
84
91
  end
85
92
  end
86
93
 
data/lib/chewy/index.rb CHANGED
@@ -52,7 +52,7 @@ module Chewy
52
52
  # @return [String] result index name
53
53
  def index_name(suggest = nil, prefix: nil, suffix: nil)
54
54
  if suggest
55
- @base_name = suggest.to_s.underscore.presence
55
+ @base_name = suggest.to_s.presence
56
56
  else
57
57
  [
58
58
  prefix || prefix_with_deprecation,
@@ -146,6 +146,8 @@ module Chewy
146
146
  # passed.
147
147
  #
148
148
  def define_type(target, options = {}, &block)
149
+ raise 'Multiple types are deprecated' if type_hash.present?
150
+
149
151
  type_class = Chewy.create_type(self, target, options, &block)
150
152
  self.type_hash = type_hash.merge(type_class.type_name => type_class)
151
153
  end
@@ -30,8 +30,8 @@ module Chewy
30
30
  # Suffixed index names might be used for zero-downtime mapping change, for example.
31
31
  # Description: (http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/).
32
32
  #
33
- def create(*args)
34
- create!(*args)
33
+ def create(*args, **kwargs)
34
+ create!(*args, **kwargs)
35
35
  rescue Elasticsearch::Transport::Transport::Errors::BadRequest
36
36
  false
37
37
  end
@@ -57,14 +57,11 @@ module Chewy
57
57
  general_name = index_name
58
58
  suffixed_name = index_name(suffix: suffix)
59
59
 
60
- if Chewy::Runtime.version >= 1.1
61
- body = specification_hash
62
- body[:aliases] = {general_name => {}} if options[:alias] && suffixed_name != general_name
63
- result = client.indices.create(index: suffixed_name, body: body)
64
- else
65
- result = client.indices.create(index: suffixed_name, body: specification_hash)
66
- result &&= client.indices.put_alias(index: suffixed_name, name: general_name) if options[:alias] && name != index_name
67
- end
60
+ body = specification_hash
61
+ body[:aliases] = {general_name => {}} if options[:alias] && suffixed_name != general_name
62
+ args = {index: suffixed_name, body: body}
63
+ args[:include_type_name] = true if Runtime.version >= '6.7.0'
64
+ result = client.indices.create(**args)
68
65
 
69
66
  Chewy.wait_for_status if result
70
67
  result
@@ -79,7 +76,13 @@ module Chewy
79
76
  # UsersIndex.delete '01-2014' # deletes `users_01-2014` index
80
77
  #
81
78
  def delete(suffix = nil)
82
- result = client.indices.delete index: index_name(suffix: suffix)
79
+ # Verify that the index_name is really the index_name and not an alias.
80
+ #
81
+ # "The index parameter in the delete index API no longer accepts alias names.
82
+ # Instead, it accepts only index names (or wildcards which will expand to matching indices)."
83
+ # https://www.elastic.co/guide/en/elasticsearch/reference/6.8/breaking-changes-6.0.html#_delete_index_api_resolves_indices_expressions_only_against_indices
84
+ index_names = client.indices.get_alias(index: index_name(suffix: suffix)).keys
85
+ result = client.indices.delete index: index_names.join(',')
83
86
  Chewy.wait_for_status if result
84
87
  result
85
88
  # es-ruby >= 1.0.10 handles Elasticsearch::Transport::Transport::Errors::NotFound
@@ -140,8 +143,17 @@ module Chewy
140
143
  #
141
144
  %i[import import!].each do |method|
142
145
  class_eval <<-METHOD, __FILE__, __LINE__ + 1
143
- def #{method} options = {}
144
- objects = options.reject { |k, v| !type_names.map(&:to_sym).include?(k) }
146
+ def #{method}(*args)
147
+ return true if args.first.blank? && !args.first.nil?
148
+
149
+ options = args.extract_options!
150
+ if args.one? && type_names.one?
151
+ objects = {type_names.first.to_sym => args.first}
152
+ elsif args.one?
153
+ fail ArgumentError, "Please pass objects for `#{method}` as a hash with type names"
154
+ else
155
+ objects = options.reject { |k, v| !type_names.map(&:to_sym).include?(k) }
156
+ end
145
157
  types.map do |type|
146
158
  args = [objects[type.type_name.to_sym], options.dup].reject(&:blank?)
147
159
  type.#{method} *args
@@ -164,7 +176,7 @@ module Chewy
164
176
  # @see http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime
165
177
  # @param suffix [String] a suffix for the newly created index
166
178
  # @param apply_journal [true, false] if true, journal is applied after the import is completed
167
- # @param journal [true, false] journalig is switched off for import during reset by default
179
+ # @param journal [true, false] journaling is switched off for import during reset by default
168
180
  # @param import_options [Hash] options, passed to the import call
169
181
  # @return [true, false] false in case of errors
170
182
  def reset!(suffix = nil, apply_journal: true, journal: false, **import_options)
@@ -231,7 +243,7 @@ module Chewy
231
243
  end
232
244
 
233
245
  def index_settings(setting_name)
234
- return {} unless settings_hash.key?(:settings) || settings_hash[:settings].key?(:index)
246
+ return {} unless settings_hash.key?(:settings) && settings_hash[:settings].key?(:index)
235
247
  settings_hash[:settings][:index].slice(setting_name)
236
248
  end
237
249
  end