chewy 0.10.1 → 7.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +240 -0
  3. data/.github/ISSUE_TEMPLATE/bug_report.md +39 -0
  4. data/.github/ISSUE_TEMPLATE/feature_request.md +20 -0
  5. data/.github/PULL_REQUEST_TEMPLATE.md +16 -0
  6. data/.rubocop.yml +25 -25
  7. data/Appraisals +12 -10
  8. data/CHANGELOG.md +312 -331
  9. data/CODE_OF_CONDUCT.md +14 -0
  10. data/CONTRIBUTING.md +63 -0
  11. data/Gemfile +5 -1
  12. data/LICENSE.txt +1 -1
  13. data/README.md +143 -77
  14. data/chewy.gemspec +10 -12
  15. data/gemfiles/{rails.4.2.mongoid.5.1.gemfile → rails.5.2.activerecord.gemfile} +6 -4
  16. data/gemfiles/{rails.4.2.activerecord.gemfile → rails.5.2.mongoid.6.4.gemfile} +6 -4
  17. data/gemfiles/{rails.4.0.activerecord.gemfile → rails.6.0.activerecord.gemfile} +6 -3
  18. data/gemfiles/rails.6.1.activerecord.gemfile +19 -0
  19. data/gemfiles/sequel.4.45.gemfile +2 -2
  20. data/lib/chewy.rb +2 -1
  21. data/lib/chewy/backports/duplicable.rb +1 -1
  22. data/lib/chewy/config.rb +10 -39
  23. data/lib/chewy/fields/base.rb +9 -3
  24. data/lib/chewy/fields/root.rb +13 -9
  25. data/lib/chewy/index.rb +3 -1
  26. data/lib/chewy/index/actions.rb +28 -16
  27. data/lib/chewy/index/aliases.rb +16 -5
  28. data/lib/chewy/index/settings.rb +2 -0
  29. data/lib/chewy/index/specification.rb +12 -10
  30. data/lib/chewy/minitest/helpers.rb +6 -6
  31. data/lib/chewy/minitest/search_index_receiver.rb +17 -17
  32. data/lib/chewy/multi_search.rb +62 -0
  33. data/lib/chewy/railtie.rb +3 -5
  34. data/lib/chewy/rake_helper.rb +5 -5
  35. data/lib/chewy/rspec/update_index.rb +3 -5
  36. data/lib/chewy/search.rb +4 -11
  37. data/lib/chewy/search/loader.rb +1 -1
  38. data/lib/chewy/search/pagination/will_paginate.rb +1 -1
  39. data/lib/chewy/search/parameters.rb +24 -6
  40. data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
  41. data/lib/chewy/search/parameters/concerns/query_storage.rb +4 -3
  42. data/lib/chewy/search/parameters/indices.rb +123 -0
  43. data/lib/chewy/search/parameters/none.rb +1 -3
  44. data/lib/chewy/search/request.rb +101 -74
  45. data/lib/chewy/search/scrolling.rb +7 -6
  46. data/lib/chewy/stash.rb +31 -22
  47. data/lib/chewy/strategy/active_job.rb +1 -1
  48. data/lib/chewy/strategy/atomic.rb +1 -1
  49. data/lib/chewy/strategy/sidekiq.rb +1 -1
  50. data/lib/chewy/type.rb +5 -2
  51. data/lib/chewy/type/adapter/active_record.rb +1 -1
  52. data/lib/chewy/type/adapter/base.rb +9 -9
  53. data/lib/chewy/type/adapter/mongoid.rb +2 -4
  54. data/lib/chewy/type/adapter/orm.rb +7 -4
  55. data/lib/chewy/type/adapter/sequel.rb +5 -7
  56. data/lib/chewy/type/crutch.rb +1 -1
  57. data/lib/chewy/type/import.rb +16 -5
  58. data/lib/chewy/type/import/bulk_builder.rb +1 -1
  59. data/lib/chewy/type/import/bulk_request.rb +4 -2
  60. data/lib/chewy/type/import/journal_builder.rb +3 -3
  61. data/lib/chewy/type/import/routine.rb +3 -3
  62. data/lib/chewy/type/mapping.rb +42 -36
  63. data/lib/chewy/type/observe.rb +16 -12
  64. data/lib/chewy/type/syncer.rb +6 -7
  65. data/lib/chewy/type/witchcraft.rb +5 -3
  66. data/lib/chewy/type/wrapper.rb +14 -4
  67. data/lib/chewy/version.rb +1 -1
  68. data/lib/sequel/plugins/chewy_observe.rb +4 -19
  69. data/migration_guide.md +34 -0
  70. data/spec/chewy/config_spec.rb +16 -21
  71. data/spec/chewy/fields/base_spec.rb +68 -70
  72. data/spec/chewy/fields/root_spec.rb +13 -13
  73. data/spec/chewy/index/actions_spec.rb +157 -38
  74. data/spec/chewy/index/aliases_spec.rb +3 -3
  75. data/spec/chewy/index/specification_spec.rb +25 -16
  76. data/spec/chewy/index_spec.rb +75 -45
  77. data/spec/chewy/journal_spec.rb +33 -29
  78. data/spec/chewy/minitest/search_index_receiver_spec.rb +11 -9
  79. data/spec/chewy/multi_search_spec.rb +85 -0
  80. data/spec/chewy/rake_helper_spec.rb +123 -95
  81. data/spec/chewy/rspec/update_index_spec.rb +47 -46
  82. data/spec/chewy/runtime_spec.rb +2 -2
  83. data/spec/chewy/search/parameters/indices_spec.rb +190 -0
  84. data/spec/chewy/search/parameters/none_spec.rb +1 -1
  85. data/spec/chewy/search/parameters_spec.rb +21 -4
  86. data/spec/chewy/search/request_spec.rb +103 -70
  87. data/spec/chewy/search/response_spec.rb +27 -17
  88. data/spec/chewy/search/scrolling_spec.rb +27 -17
  89. data/spec/chewy/search_spec.rb +49 -35
  90. data/spec/chewy/stash_spec.rb +15 -13
  91. data/spec/chewy/strategy/active_job_spec.rb +15 -2
  92. data/spec/chewy/strategy/shoryuken_spec.rb +8 -2
  93. data/spec/chewy/strategy/sidekiq_spec.rb +6 -2
  94. data/spec/chewy/type/adapter/active_record_spec.rb +16 -4
  95. data/spec/chewy/type/import/bulk_builder_spec.rb +9 -94
  96. data/spec/chewy/type/import/journal_builder_spec.rb +17 -15
  97. data/spec/chewy/type/import_spec.rb +15 -0
  98. data/spec/chewy/type/mapping_spec.rb +51 -18
  99. data/spec/chewy/type/observe_spec.rb +4 -4
  100. data/spec/chewy/type/witchcraft_spec.rb +15 -0
  101. data/spec/chewy/type/wrapper_spec.rb +3 -1
  102. data/spec/chewy_spec.rb +0 -7
  103. data/spec/spec_helper.rb +5 -1
  104. data/spec/support/active_record.rb +21 -0
  105. metadata +51 -116
  106. data/.travis.yml +0 -53
  107. data/LEGACY_DSL.md +0 -497
  108. data/gemfiles/rails.4.1.activerecord.gemfile +0 -14
  109. data/gemfiles/rails.5.0.activerecord.gemfile +0 -15
  110. data/gemfiles/rails.5.0.mongoid.6.0.gemfile +0 -15
  111. data/gemfiles/rails.5.1.activerecord.gemfile +0 -15
  112. data/gemfiles/rails.5.1.mongoid.6.1.gemfile +0 -15
  113. data/lib/chewy/query.rb +0 -1098
  114. data/lib/chewy/query/compose.rb +0 -68
  115. data/lib/chewy/query/criteria.rb +0 -191
  116. data/lib/chewy/query/filters.rb +0 -227
  117. data/lib/chewy/query/loading.rb +0 -111
  118. data/lib/chewy/query/nodes/and.rb +0 -25
  119. data/lib/chewy/query/nodes/base.rb +0 -17
  120. data/lib/chewy/query/nodes/bool.rb +0 -34
  121. data/lib/chewy/query/nodes/equal.rb +0 -34
  122. data/lib/chewy/query/nodes/exists.rb +0 -20
  123. data/lib/chewy/query/nodes/expr.rb +0 -28
  124. data/lib/chewy/query/nodes/field.rb +0 -110
  125. data/lib/chewy/query/nodes/has_child.rb +0 -15
  126. data/lib/chewy/query/nodes/has_parent.rb +0 -15
  127. data/lib/chewy/query/nodes/has_relation.rb +0 -59
  128. data/lib/chewy/query/nodes/match_all.rb +0 -11
  129. data/lib/chewy/query/nodes/missing.rb +0 -20
  130. data/lib/chewy/query/nodes/not.rb +0 -25
  131. data/lib/chewy/query/nodes/or.rb +0 -25
  132. data/lib/chewy/query/nodes/prefix.rb +0 -19
  133. data/lib/chewy/query/nodes/query.rb +0 -20
  134. data/lib/chewy/query/nodes/range.rb +0 -63
  135. data/lib/chewy/query/nodes/raw.rb +0 -15
  136. data/lib/chewy/query/nodes/regexp.rb +0 -35
  137. data/lib/chewy/query/nodes/script.rb +0 -20
  138. data/lib/chewy/query/pagination.rb +0 -25
  139. data/spec/chewy/query/criteria_spec.rb +0 -700
  140. data/spec/chewy/query/filters_spec.rb +0 -201
  141. data/spec/chewy/query/loading_spec.rb +0 -124
  142. data/spec/chewy/query/nodes/and_spec.rb +0 -12
  143. data/spec/chewy/query/nodes/bool_spec.rb +0 -14
  144. data/spec/chewy/query/nodes/equal_spec.rb +0 -32
  145. data/spec/chewy/query/nodes/exists_spec.rb +0 -18
  146. data/spec/chewy/query/nodes/has_child_spec.rb +0 -59
  147. data/spec/chewy/query/nodes/has_parent_spec.rb +0 -59
  148. data/spec/chewy/query/nodes/match_all_spec.rb +0 -11
  149. data/spec/chewy/query/nodes/missing_spec.rb +0 -16
  150. data/spec/chewy/query/nodes/not_spec.rb +0 -13
  151. data/spec/chewy/query/nodes/or_spec.rb +0 -12
  152. data/spec/chewy/query/nodes/prefix_spec.rb +0 -16
  153. data/spec/chewy/query/nodes/query_spec.rb +0 -12
  154. data/spec/chewy/query/nodes/range_spec.rb +0 -32
  155. data/spec/chewy/query/nodes/raw_spec.rb +0 -11
  156. data/spec/chewy/query/nodes/regexp_spec.rb +0 -43
  157. data/spec/chewy/query/nodes/script_spec.rb +0 -15
  158. data/spec/chewy/query/pagination/kaminari_spec.rb +0 -5
  159. data/spec/chewy/query/pagination/will_paginate_spec.rb +0 -5
  160. data/spec/chewy/query/pagination_spec.rb +0 -39
  161. data/spec/chewy/query_spec.rb +0 -636
  162. data/spec/chewy/search/parameters/indices_boost_spec.rb +0 -83
data/chewy.gemspec CHANGED
@@ -1,5 +1,3 @@
1
- # coding: utf-8
2
-
3
1
  lib = File.expand_path('../lib', __FILE__)
4
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
3
  require 'chewy/version'
@@ -7,8 +5,8 @@ require 'chewy/version'
7
5
  Gem::Specification.new do |spec| # rubocop:disable BlockLength
8
6
  spec.name = 'chewy'
9
7
  spec.version = Chewy::VERSION
10
- spec.authors = ['pyromaniac']
11
- spec.email = ['kinwizard@gmail.com']
8
+ spec.authors = ['Toptal, LLC', 'pyromaniac']
9
+ spec.email = ['open-source@toptal.com', 'kinwizard@gmail.com']
12
10
  spec.summary = 'Elasticsearch ODM client wrapper'
13
11
  spec.description = 'Chewy provides functionality for Elasticsearch index handling, documents import mappings and chainable query DSL'
14
12
  spec.homepage = 'https://github.com/toptal/chewy'
@@ -19,22 +17,22 @@ Gem::Specification.new do |spec| # rubocop:disable BlockLength
19
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
18
  spec.require_paths = ['lib']
21
19
 
22
- spec.add_development_dependency 'rake'
23
20
  spec.add_development_dependency 'appraisal'
24
- spec.add_development_dependency 'rspec'
25
- spec.add_development_dependency 'rspec-its'
26
- spec.add_development_dependency 'rspec-collection_matchers'
27
- spec.add_development_dependency 'sqlite3'
28
21
  spec.add_development_dependency 'database_cleaner'
29
22
  spec.add_development_dependency 'elasticsearch-extensions'
23
+ spec.add_development_dependency 'rake'
30
24
  spec.add_development_dependency 'resque_spec'
25
+ spec.add_development_dependency 'rspec', '>= 3.7.0'
26
+ spec.add_development_dependency 'rspec-collection_matchers'
27
+ spec.add_development_dependency 'rspec-its'
28
+ spec.add_development_dependency 'rubocop', '0.52.1'
29
+ spec.add_development_dependency 'sqlite3'
31
30
  spec.add_development_dependency 'timecop'
32
- spec.add_development_dependency 'rubocop', '0.48.1'
33
31
 
34
32
  spec.add_development_dependency 'method_source'
35
33
  spec.add_development_dependency 'unparser'
36
34
 
37
- spec.add_dependency 'activesupport', '>= 4.0'
38
- spec.add_dependency 'elasticsearch', '>= 2.0.0'
35
+ spec.add_dependency 'activesupport', '>= 5.2'
36
+ spec.add_dependency 'elasticsearch', '>= 6.3.0'
39
37
  spec.add_dependency 'elasticsearch-dsl'
40
38
  end
@@ -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,6 +33,7 @@ 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
 
@@ -6,15 +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)
15
12
  @value ||= -> { self }
16
13
  @dynamic_templates = []
17
- @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))
18
21
  end
19
22
 
20
23
  def mappings_hash
@@ -66,7 +69,8 @@ module Chewy
66
69
  # @param object [Object] a base object for composition
67
70
  # @param crutches [Object] any object that will be passed to every field value proc as a last argument
68
71
  # @param fields [Array<Symbol>] a list of fields to compose, every field will be composed if empty
69
- # @return [Hash] JSON-ready heash with stringifyed keys
72
+ # @return [Hash] JSON-ready hash with stringified keys
73
+ #
70
74
  def compose(object, crutches = nil, fields: [])
71
75
  result = evaluate([object, crutches])
72
76
 
@@ -81,9 +85,9 @@ module Chewy
81
85
  memo.merge!(field.compose(result, crutches) || {})
82
86
  end.as_json
83
87
  elsif fields.present?
84
- result.as_json(only: fields)
88
+ result.as_json(only: fields, root: false)
85
89
  else
86
- result.as_json
90
+ result.as_json(root: false)
87
91
  end
88
92
  end
89
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,13 +176,13 @@ 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)
171
183
  result = if suffix.present?
172
184
  start_time = Time.now
173
- indexes = self.indexes
185
+ indexes = self.indexes - [index_name]
174
186
  create! suffix, alias: false
175
187
 
176
188
  general_name = index_name
@@ -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