chewy 5.0.0 → 7.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (154) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +214 -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/Appraisals +1 -17
  7. data/CHANGELOG.md +312 -356
  8. data/CODE_OF_CONDUCT.md +14 -0
  9. data/CONTRIBUTING.md +63 -0
  10. data/Gemfile +2 -0
  11. data/LICENSE.txt +1 -1
  12. data/README.md +97 -92
  13. data/chewy.gemspec +5 -5
  14. data/gemfiles/rails.5.2.activerecord.gemfile +4 -3
  15. data/gemfiles/{rails.5.0.activerecord.gemfile → rails.5.2.mongoid.6.4.gemfile} +4 -3
  16. data/gemfiles/{rails.5.0.mongoid.6.1.gemfile → rails.6.0.activerecord.gemfile} +4 -3
  17. data/gemfiles/{rails.5.1.activerecord.gemfile → rails.6.1.activerecord.gemfile} +6 -3
  18. data/lib/chewy.rb +1 -1
  19. data/lib/chewy/backports/duplicable.rb +1 -1
  20. data/lib/chewy/config.rb +2 -20
  21. data/lib/chewy/fields/base.rb +1 -7
  22. data/lib/chewy/fields/root.rb +3 -4
  23. data/lib/chewy/index.rb +6 -15
  24. data/lib/chewy/index/actions.rb +12 -4
  25. data/lib/chewy/index/aliases.rb +14 -5
  26. data/lib/chewy/multi_search.rb +62 -0
  27. data/lib/chewy/railtie.rb +3 -19
  28. data/lib/chewy/search.rb +2 -9
  29. data/lib/chewy/search/loader.rb +3 -13
  30. data/lib/chewy/search/pagination/will_paginate.rb +1 -1
  31. data/lib/chewy/search/parameters.rb +24 -6
  32. data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
  33. data/lib/chewy/search/parameters/indices.rb +78 -0
  34. data/lib/chewy/search/parameters/none.rb +1 -3
  35. data/lib/chewy/search/request.rb +92 -85
  36. data/lib/chewy/search/response.rb +1 -1
  37. data/lib/chewy/search/scrolling.rb +8 -7
  38. data/lib/chewy/stash.rb +3 -6
  39. data/lib/chewy/strategy/active_job.rb +1 -1
  40. data/lib/chewy/strategy/sidekiq.rb +1 -1
  41. data/lib/chewy/type.rb +4 -1
  42. data/lib/chewy/type/adapter/active_record.rb +1 -1
  43. data/lib/chewy/type/adapter/mongoid.rb +1 -1
  44. data/lib/chewy/type/adapter/orm.rb +7 -4
  45. data/lib/chewy/type/adapter/sequel.rb +1 -1
  46. data/lib/chewy/type/import.rb +14 -4
  47. data/lib/chewy/type/import/bulk_request.rb +5 -4
  48. data/lib/chewy/type/import/journal_builder.rb +2 -3
  49. data/lib/chewy/type/import/routine.rb +3 -3
  50. data/lib/chewy/type/mapping.rb +5 -5
  51. data/lib/chewy/type/observe.rb +3 -3
  52. data/lib/chewy/type/syncer.rb +2 -6
  53. data/lib/chewy/type/witchcraft.rb +4 -2
  54. data/lib/chewy/type/wrapper.rb +12 -2
  55. data/lib/chewy/version.rb +1 -1
  56. data/lib/tasks/chewy.rake +10 -10
  57. data/migration_guide.md +37 -0
  58. data/spec/chewy/config_spec.rb +1 -22
  59. data/spec/chewy/fields/base_spec.rb +15 -13
  60. data/spec/chewy/fields/root_spec.rb +4 -4
  61. data/spec/chewy/index/actions_spec.rb +120 -33
  62. data/spec/chewy/index/aliases_spec.rb +3 -3
  63. data/spec/chewy/index/specification_spec.rb +13 -13
  64. data/spec/chewy/index_spec.rb +17 -42
  65. data/spec/chewy/journal_spec.rb +25 -21
  66. data/spec/chewy/minitest/search_index_receiver_spec.rb +11 -9
  67. data/spec/chewy/multi_search_spec.rb +85 -0
  68. data/spec/chewy/rake_helper_spec.rb +102 -87
  69. data/spec/chewy/rspec/update_index_spec.rb +47 -46
  70. data/spec/chewy/runtime_spec.rb +2 -2
  71. data/spec/chewy/search/loader_spec.rb +0 -16
  72. data/spec/chewy/search/parameters/indices_spec.rb +105 -0
  73. data/spec/chewy/search/parameters/none_spec.rb +1 -1
  74. data/spec/chewy/search/parameters_spec.rb +21 -4
  75. data/spec/chewy/search/request_spec.rb +94 -78
  76. data/spec/chewy/search/response_spec.rb +27 -17
  77. data/spec/chewy/search/scrolling_spec.rb +22 -18
  78. data/spec/chewy/search_spec.rb +45 -41
  79. data/spec/chewy/stash_spec.rb +14 -12
  80. data/spec/chewy/strategy/active_job_spec.rb +15 -2
  81. data/spec/chewy/strategy/shoryuken_spec.rb +6 -2
  82. data/spec/chewy/strategy/sidekiq_spec.rb +6 -2
  83. data/spec/chewy/type/adapter/active_record_spec.rb +16 -4
  84. data/spec/chewy/type/import/bulk_builder_spec.rb +9 -94
  85. data/spec/chewy/type/import/bulk_request_spec.rb +0 -6
  86. data/spec/chewy/type/import/journal_builder_spec.rb +9 -11
  87. data/spec/chewy/type/import_spec.rb +11 -2
  88. data/spec/chewy/type/mapping_spec.rb +8 -38
  89. data/spec/chewy/type/observe_spec.rb +4 -4
  90. data/spec/chewy/type/witchcraft_spec.rb +15 -0
  91. data/spec/chewy/type/wrapper_spec.rb +3 -1
  92. data/spec/chewy_spec.rb +0 -7
  93. data/spec/spec_helper.rb +4 -8
  94. data/spec/support/active_record.rb +21 -0
  95. metadata +31 -100
  96. data/.travis.yml +0 -45
  97. data/LEGACY_DSL.md +0 -497
  98. data/gemfiles/rails.4.0.activerecord.gemfile +0 -15
  99. data/gemfiles/rails.4.1.activerecord.gemfile +0 -15
  100. data/gemfiles/rails.4.2.activerecord.gemfile +0 -16
  101. data/gemfiles/rails.4.2.mongoid.5.2.gemfile +0 -16
  102. data/gemfiles/rails.5.1.mongoid.6.3.gemfile +0 -16
  103. data/lib/chewy/query.rb +0 -1137
  104. data/lib/chewy/query/compose.rb +0 -68
  105. data/lib/chewy/query/criteria.rb +0 -191
  106. data/lib/chewy/query/filters.rb +0 -244
  107. data/lib/chewy/query/loading.rb +0 -110
  108. data/lib/chewy/query/nodes/and.rb +0 -25
  109. data/lib/chewy/query/nodes/base.rb +0 -17
  110. data/lib/chewy/query/nodes/bool.rb +0 -34
  111. data/lib/chewy/query/nodes/equal.rb +0 -34
  112. data/lib/chewy/query/nodes/exists.rb +0 -20
  113. data/lib/chewy/query/nodes/expr.rb +0 -28
  114. data/lib/chewy/query/nodes/field.rb +0 -110
  115. data/lib/chewy/query/nodes/has_child.rb +0 -15
  116. data/lib/chewy/query/nodes/has_parent.rb +0 -15
  117. data/lib/chewy/query/nodes/has_relation.rb +0 -59
  118. data/lib/chewy/query/nodes/match_all.rb +0 -11
  119. data/lib/chewy/query/nodes/missing.rb +0 -20
  120. data/lib/chewy/query/nodes/not.rb +0 -25
  121. data/lib/chewy/query/nodes/or.rb +0 -25
  122. data/lib/chewy/query/nodes/prefix.rb +0 -19
  123. data/lib/chewy/query/nodes/query.rb +0 -20
  124. data/lib/chewy/query/nodes/range.rb +0 -63
  125. data/lib/chewy/query/nodes/raw.rb +0 -15
  126. data/lib/chewy/query/nodes/regexp.rb +0 -35
  127. data/lib/chewy/query/nodes/script.rb +0 -20
  128. data/lib/chewy/query/pagination.rb +0 -25
  129. data/lib/chewy/search/parameters/types.rb +0 -20
  130. data/spec/chewy/query/criteria_spec.rb +0 -700
  131. data/spec/chewy/query/filters_spec.rb +0 -201
  132. data/spec/chewy/query/loading_spec.rb +0 -124
  133. data/spec/chewy/query/nodes/and_spec.rb +0 -12
  134. data/spec/chewy/query/nodes/bool_spec.rb +0 -14
  135. data/spec/chewy/query/nodes/equal_spec.rb +0 -32
  136. data/spec/chewy/query/nodes/exists_spec.rb +0 -18
  137. data/spec/chewy/query/nodes/has_child_spec.rb +0 -59
  138. data/spec/chewy/query/nodes/has_parent_spec.rb +0 -59
  139. data/spec/chewy/query/nodes/match_all_spec.rb +0 -11
  140. data/spec/chewy/query/nodes/missing_spec.rb +0 -16
  141. data/spec/chewy/query/nodes/not_spec.rb +0 -14
  142. data/spec/chewy/query/nodes/or_spec.rb +0 -12
  143. data/spec/chewy/query/nodes/prefix_spec.rb +0 -16
  144. data/spec/chewy/query/nodes/query_spec.rb +0 -12
  145. data/spec/chewy/query/nodes/range_spec.rb +0 -32
  146. data/spec/chewy/query/nodes/raw_spec.rb +0 -11
  147. data/spec/chewy/query/nodes/regexp_spec.rb +0 -43
  148. data/spec/chewy/query/nodes/script_spec.rb +0 -15
  149. data/spec/chewy/query/pagination/kaminari_spec.rb +0 -5
  150. data/spec/chewy/query/pagination/will_paginate_spec.rb +0 -5
  151. data/spec/chewy/query/pagination_spec.rb +0 -39
  152. data/spec/chewy/query_spec.rb +0 -637
  153. data/spec/chewy/search/parameters/indices_boost_spec.rb +0 -83
  154. data/spec/chewy/search/parameters/types_spec.rb +0 -5
data/chewy.gemspec CHANGED
@@ -5,8 +5,8 @@ require 'chewy/version'
5
5
  Gem::Specification.new do |spec| # rubocop:disable BlockLength
6
6
  spec.name = 'chewy'
7
7
  spec.version = Chewy::VERSION
8
- spec.authors = ['pyromaniac']
9
- spec.email = ['kinwizard@gmail.com']
8
+ spec.authors = ['Toptal, LLC', 'pyromaniac']
9
+ spec.email = ['open-source@toptal.com', 'kinwizard@gmail.com']
10
10
  spec.summary = 'Elasticsearch ODM client wrapper'
11
11
  spec.description = 'Chewy provides functionality for Elasticsearch index handling, documents import mappings and chainable query DSL'
12
12
  spec.homepage = 'https://github.com/toptal/chewy'
@@ -22,7 +22,7 @@ Gem::Specification.new do |spec| # rubocop:disable BlockLength
22
22
  spec.add_development_dependency 'elasticsearch-extensions'
23
23
  spec.add_development_dependency 'rake'
24
24
  spec.add_development_dependency 'resque_spec'
25
- spec.add_development_dependency 'rspec'
25
+ spec.add_development_dependency 'rspec', '>= 3.7.0'
26
26
  spec.add_development_dependency 'rspec-collection_matchers'
27
27
  spec.add_development_dependency 'rspec-its'
28
28
  spec.add_development_dependency 'rubocop', '0.52.1'
@@ -32,7 +32,7 @@ Gem::Specification.new do |spec| # rubocop:disable BlockLength
32
32
  spec.add_development_dependency 'method_source'
33
33
  spec.add_development_dependency 'unparser'
34
34
 
35
- spec.add_dependency 'activesupport', '>= 4.0'
36
- spec.add_dependency 'elasticsearch', '>= 2.0.0'
35
+ spec.add_dependency 'activesupport', '>= 5.2'
36
+ spec.add_dependency 'elasticsearch', '>= 6.3.0'
37
37
  spec.add_dependency 'elasticsearch-dsl'
38
38
  end
@@ -2,9 +2,9 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "activerecord", "~> 5.2.0.rc1"
6
- gem "activesupport", "~> 5.2.0.rc1"
7
- gem "activejob", "~> 5.2.0.rc1"
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
10
  gem "aws-sdk-sqs", require: false
@@ -12,5 +12,6 @@ gem "sidekiq", require: false
12
12
  gem "kaminari-core", "~> 1.1.0", require: false
13
13
  gem "will_paginate", require: false
14
14
  gem "parallel", require: false
15
+ gem 'rspec_junit_formatter', '~> 0.4.1'
15
16
 
16
17
  gemspec path: "../"
@@ -2,9 +2,9 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "activerecord", "~> 5.0.0"
6
- gem "activesupport", "~> 5.0.0"
7
- gem "activejob", "~> 5.0.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
10
  gem "aws-sdk-sqs", require: false
@@ -12,5 +12,6 @@ gem "sidekiq", require: false
12
12
  gem "kaminari-core", "~> 1.1.0", require: false
13
13
  gem "will_paginate", require: false
14
14
  gem "parallel", require: false
15
+ gem 'rspec_junit_formatter', '~> 0.4.1'
15
16
 
16
17
  gemspec path: "../"
@@ -2,9 +2,9 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "mongoid", "~> 6.1.0"
6
- gem "activesupport", "~> 5.0.0"
7
- gem "activejob", "~> 5.0.0"
5
+ gem "activerecord", "~> 6.0.0"
6
+ gem "activesupport", "~> 6.0.0"
7
+ gem "activejob", "~> 6.0.0"
8
8
  gem "resque", require: false
9
9
  gem "shoryuken", require: false
10
10
  gem "aws-sdk-sqs", require: false
@@ -12,5 +12,6 @@ gem "sidekiq", require: false
12
12
  gem "kaminari-core", "~> 1.1.0", require: false
13
13
  gem "will_paginate", require: false
14
14
  gem "parallel", require: false
15
+ gem 'rspec_junit_formatter', '~> 0.4.1'
15
16
 
16
17
  gemspec path: "../"
@@ -2,9 +2,9 @@
2
2
 
3
3
  source "https://rubygems.org"
4
4
 
5
- gem "activerecord", "~> 5.1.0"
6
- gem "activesupport", "~> 5.1.0"
7
- gem "activejob", "~> 5.1.0"
5
+ gem "activerecord", "~> 6.1.0"
6
+ gem "activesupport", "~> 6.1.0"
7
+ gem "activejob", "~> 6.1.0"
8
8
  gem "resque", require: false
9
9
  gem "shoryuken", require: false
10
10
  gem "aws-sdk-sqs", require: false
@@ -12,5 +12,8 @@ gem "sidekiq", require: false
12
12
  gem "kaminari-core", "~> 1.1.0", require: false
13
13
  gem "will_paginate", require: false
14
14
  gem "parallel", require: false
15
+ gem 'rspec_junit_formatter', '~> 0.4.1'
16
+
17
+ gem 'rexml' if RUBY_VERSION >= '3.0.0'
15
18
 
16
19
  gemspec path: "../"
data/lib/chewy.rb CHANGED
@@ -132,7 +132,7 @@ module Chewy
132
132
  def create_type(index, target, options = {}, &block)
133
133
  type = Class.new(Chewy::Type)
134
134
 
135
- adapter = adapters.find { |klass| klass.accepts?(target) }.new(target, options)
135
+ adapter = adapters.find { |klass| klass.accepts?(target) }.new(target, **options)
136
136
 
137
137
  index.const_set(adapter.name, type)
138
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
@@ -3,18 +3,6 @@ module Chewy
3
3
  include Singleton
4
4
 
5
5
  attr_accessor :settings, :logger,
6
- # Default query compilation mode. `:must` by default.
7
- # See Chewy::Query#query_mode for details
8
- #
9
- :query_mode,
10
- # Default filters compilation mode. `:and` by default.
11
- # See Chewy::Query#filter_mode for details
12
- #
13
- :filter_mode,
14
- # Default post_filters compilation mode. `nil` by default.
15
- # See Chewy::Query#post_filter_mode for details
16
- #
17
- :post_filter_mode,
18
6
  # The first strategy in stack. `:base` by default.
19
7
  # If you need to return to the previous chewy behavior -
20
8
  # just set it to `:bypass`
@@ -46,7 +34,7 @@ module Chewy
46
34
  # Default options for root of Chewy type. Allows to set default options
47
35
  # for type mappings like `_all`.
48
36
  :default_root_options,
49
- # Default field type for any field in any Chewy type. Defaults to 'string'.
37
+ # Default field type for any field in any Chewy type. Defaults to 'text'.
50
38
  :default_field_type
51
39
 
52
40
  attr_reader :transport_logger, :transport_tracer,
@@ -59,8 +47,6 @@ module Chewy
59
47
 
60
48
  def initialize
61
49
  @settings = {}
62
- @query_mode = :must
63
- @filter_mode = :and
64
50
  @root_strategy = :base
65
51
  @request_strategy = :atomic
66
52
  @use_after_commit_callbacks = true
@@ -70,7 +56,7 @@ module Chewy
70
56
  @indices_path = 'app/chewy'
71
57
  @default_root_options = {}
72
58
  @default_field_type = 'text'.freeze
73
- self.search_class = Chewy::Search::Request
59
+ @search_class = build_search_class(Chewy::Search::Request)
74
60
  end
75
61
 
76
62
  def transport_logger=(logger)
@@ -83,10 +69,6 @@ module Chewy
83
69
  @transport_tracer = tracer
84
70
  end
85
71
 
86
- def search_class=(base)
87
- @search_class = build_search_class(base)
88
- end
89
-
90
72
  # Chewy core configurations. There is two ways to set it up:
91
73
  # use `Chewy.settings=` method or, for Rails application,
92
74
  # create `config/chewy.yml` file. Btw, `config/chewy.yml` supports
@@ -7,7 +7,7 @@ module Chewy
7
7
  def initialize(name, value: nil, **options)
8
8
  @name = name.to_sym
9
9
  @options = {}
10
- update_options!(options)
10
+ update_options!(**options)
11
11
  @value = value
12
12
  @children = []
13
13
  end
@@ -34,12 +34,6 @@ module Chewy
34
34
  mapping.reverse_merge!(options)
35
35
  mapping.reverse_merge!(type: (children.present? ? 'object' : Chewy.default_field_type))
36
36
 
37
- # This is done to support ES2 journaling and will be removed soon
38
- if mapping[:type] == 'keyword' && Chewy::Runtime.version < '5.0'
39
- mapping[:type] = 'string'
40
- mapping[:index] = 'not_analyzed'
41
- end
42
-
43
37
  {name => mapping}
44
38
  end
45
39
 
@@ -6,8 +6,8 @@ module Chewy
6
6
  attr_reader :parent
7
7
  attr_reader :parent_id
8
8
 
9
- def initialize(*)
10
- super
9
+ def initialize(name, **options)
10
+ super(name, **options)
11
11
 
12
12
  @value ||= -> { self }
13
13
  @dynamic_templates = []
@@ -29,8 +29,7 @@ module Chewy
29
29
  mappings[name][:dynamic_templates].concat dynamic_templates
30
30
  end
31
31
 
32
- mappings[name][:_parent] = parent.is_a?(Hash) ? parent : {type: parent} if parent
33
- mappings
32
+ mappings[name]
34
33
  end
35
34
 
36
35
  def dynamic_template(*args)
data/lib/chewy/index.rb CHANGED
@@ -146,27 +146,18 @@ 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
152
154
 
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.
155
+ # Returns defined type:
160
156
  #
161
- # UsersIndex.filters { name =~ 'ro' }.types(:admin, :manager)
162
- # UsersIndex.types(:admin, :manager).filters { name =~ 'ro' } # the same as the first example
157
+ # UsersIndex.types # => [UsersIndex::User]
163
158
  #
164
- def types(*args)
165
- if args.present?
166
- all.types(*args)
167
- else
168
- type_hash.values
169
- end
159
+ def types
160
+ type_hash.values
170
161
  end
171
162
 
172
163
  # Returns defined types names:
@@ -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
@@ -74,7 +74,13 @@ module Chewy
74
74
  # UsersIndex.delete '01-2014' # deletes `users_01-2014` index
75
75
  #
76
76
  def delete(suffix = nil)
77
- result = client.indices.delete index: index_name(suffix: suffix)
77
+ # Verify that the index_name is really the index_name and not an alias.
78
+ #
79
+ # "The index parameter in the delete index API no longer accepts alias names.
80
+ # Instead, it accepts only index names (or wildcards which will expand to matching indices)."
81
+ # https://www.elastic.co/guide/en/elasticsearch/reference/6.8/breaking-changes-6.0.html#_delete_index_api_resolves_indices_expressions_only_against_indices
82
+ index_names = client.indices.get_alias(index: index_name(suffix: suffix)).keys
83
+ result = client.indices.delete index: index_names.join(',')
78
84
  Chewy.wait_for_status if result
79
85
  result
80
86
  # es-ruby >= 1.0.10 handles Elasticsearch::Transport::Transport::Errors::NotFound
@@ -136,6 +142,8 @@ module Chewy
136
142
  %i[import import!].each do |method|
137
143
  class_eval <<-METHOD, __FILE__, __LINE__ + 1
138
144
  def #{method}(*args)
145
+ return true if args.first.blank? && !args.first.nil?
146
+
139
147
  options = args.extract_options!
140
148
  if args.one? && type_names.one?
141
149
  objects = {type_names.first.to_sym => args.first}
@@ -172,7 +180,7 @@ module Chewy
172
180
  def reset!(suffix = nil, apply_journal: true, journal: false, **import_options)
173
181
  result = if suffix.present?
174
182
  start_time = Time.now
175
- indexes = self.indexes
183
+ indexes = self.indexes - [index_name]
176
184
  create! suffix, alias: false
177
185
 
178
186
  general_name = index_name
@@ -5,14 +5,23 @@ module Chewy
5
5
 
6
6
  module ClassMethods
7
7
  def indexes
8
- client.indices.get_alias(name: index_name).keys
9
- rescue Elasticsearch::Transport::Transport::Errors::NotFound
10
- []
8
+ indexes = empty_if_not_found { client.indices.get(index: index_name).keys }
9
+ indexes += empty_if_not_found { client.indices.get_alias(name: index_name).keys }
10
+ indexes.compact.uniq
11
11
  end
12
12
 
13
13
  def aliases
14
- name = index_name
15
- client.indices.get_alias(index: name, name: '*')[name].try(:[], 'aliases').try(:keys) || []
14
+ empty_if_not_found do
15
+ client.indices.get_alias(index: index_name, name: '*').values.flat_map do |aliases|
16
+ aliases['aliases'].keys
17
+ end
18
+ end.compact.uniq
19
+ end
20
+
21
+ private
22
+
23
+ def empty_if_not_found
24
+ yield
16
25
  rescue Elasticsearch::Transport::Transport::Errors::NotFound
17
26
  []
18
27
  end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Chewy
4
+ # `Chewy::MultiSearch` provides an interface for executing multiple
5
+ # queries via the Elasticsearch Multi Search API. When a MultiSearch
6
+ # is performed it wraps the responses from Elasticsearch and assigns
7
+ # them to the appropriate queries.
8
+ class MultiSearch
9
+ attr_reader :queries
10
+
11
+ # Instantiate a new MultiSearch instance.
12
+ #
13
+ # @param queries [Array<Chewy::Search::Request>]
14
+ # @option [Elasticsearch::Transport::Client] :client (Chewy.client)
15
+ # The Elasticsearch client that should be used for issuing requests.
16
+ def initialize(queries, client: Chewy.client)
17
+ @client = client
18
+ @queries = Array(queries)
19
+ end
20
+
21
+ # Adds a query to be performed by the MultiSearch
22
+ #
23
+ # @param query [Chewy::Search::Request]
24
+ def add_query(query)
25
+ @queries << query
26
+ end
27
+
28
+ # Performs any unperformed queries and returns the responses for all queries.
29
+ #
30
+ # @return [Array<Chewy::Search::Response>]
31
+ def responses
32
+ perform
33
+ queries.map(&:response)
34
+ end
35
+
36
+ # Performs any unperformed queries.
37
+ def perform
38
+ unperformed_queries = queries.reject(&:performed?)
39
+ return if unperformed_queries.empty?
40
+
41
+ responses = msearch(unperformed_queries)['responses']
42
+ unperformed_queries.zip(responses).map { |query, response| query.response = response }
43
+ end
44
+
45
+ private
46
+
47
+ attr_reader :client
48
+
49
+ def msearch(queries_to_search)
50
+ body = queries_to_search.flat_map do |query|
51
+ rendered = query.render
52
+ [rendered.except(:body), rendered[:body]]
53
+ end
54
+
55
+ client.msearch(body: body)
56
+ end
57
+ end
58
+
59
+ def self.msearch(queries)
60
+ Chewy::MultiSearch.new(queries)
61
+ end
62
+ end
data/lib/chewy/railtie.rb CHANGED
@@ -22,17 +22,6 @@ module Chewy
22
22
  end
23
23
 
24
24
  module MigrationStrategy
25
- extend ActiveSupport::Concern
26
- included do
27
- alias_method_chain :migrate, :chewy
28
- end
29
-
30
- def migrate_with_chewy(*args)
31
- Chewy.strategy(:bypass) { migrate_without_chewy(*args) }
32
- end
33
- end
34
-
35
- module Rails5MigrationStrategy
36
25
  def migrate(*args)
37
26
  Chewy.strategy(:bypass) { super }
38
27
  end
@@ -57,18 +46,13 @@ module Chewy
57
46
 
58
47
  initializer 'chewy.migration_strategy' do
59
48
  ActiveSupport.on_load(:active_record) do
60
- if Rails::VERSION::MAJOR >= 5
61
- ActiveRecord::Migration.prepend(Rails5MigrationStrategy)
62
- ActiveRecord::Migrator.prepend(Rails5MigrationStrategy) if defined? ActiveRecord::Migrator
63
- else
64
- ActiveRecord::Migration.send(:include, MigrationStrategy)
65
- ActiveRecord::Migrator.send(:include, MigrationStrategy) if defined? ActiveRecord::Migrator
66
- end
49
+ ActiveRecord::Migration.prepend(MigrationStrategy)
50
+ ActiveRecord::Migrator.prepend(MigrationStrategy) if defined? ActiveRecord::Migrator
67
51
  end
68
52
  end
69
53
 
70
54
  initializer 'chewy.request_strategy' do |app|
71
- app.config.middleware.insert_after(Rails::Rack::Logger, RequestStrategy)
55
+ app.config.middleware.insert_before(ActionDispatch::ShowExceptions, RequestStrategy)
72
56
  end
73
57
 
74
58
  initializer 'chewy.add_indices_path' do |_app|