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
@@ -0,0 +1,27 @@
1
+ require 'chewy/search/parameters/storage'
2
+
3
+ module Chewy
4
+ module Search
5
+ class Parameters
6
+ # Stores boolean value, but has 3 states: `true`, `false` and `nil`.
7
+ #
8
+ # @see Chewy::Search::Request#allow_partial_search_results
9
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/6.4/search-request-body.html#_parameters_4
10
+ class AllowPartialSearchResults < Storage
11
+ # We don't want to render `nil`, but render `true` and `false` values.
12
+ #
13
+ # @see Chewy::Search::Parameters::Storage#render
14
+ # @return [{Symbol => Object}, nil]
15
+ def render
16
+ {self.class.param_name => value} unless value.nil?
17
+ end
18
+
19
+ private
20
+
21
+ def normalize(value)
22
+ !!value unless value.nil?
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -20,16 +20,17 @@ module Chewy
20
20
  # rendering logic.
21
21
  #
22
22
  # @!attribute must
23
- # @return [Array<Hash>]
23
+ # @return [Array<Hash>, Hash, nil]
24
24
  # @!attribute should
25
- # @return [Array<Hash>]
25
+ # @return [Array<Hash>, Hash, nil]
26
26
  # @!attribute must_not
27
- # @return [Array<Hash>]
27
+ # @return [[Array<Hash>, Hash, nil]
28
28
  # @!attribute minimum_should_match
29
29
  # @return [String, Integer, nil]
30
30
  class Bool
31
31
  # Acceptable bool query keys
32
32
  KEYS = %i[must should must_not minimum_should_match].freeze
33
+ # @!ignorewarning
33
34
  attr_reader(*KEYS)
34
35
 
35
36
  # @param must [Array<Hash>, Hash, nil]
@@ -0,0 +1,123 @@
1
+ require 'chewy/search/parameters/storage'
2
+
3
+ module Chewy
4
+ module Search
5
+ class Parameters
6
+ # Stores indices and/or types to query.
7
+ # Renders it to lists of string accepted by ElasticSearch
8
+ # API.
9
+ #
10
+ # The semantics behind it can be described in the
11
+ # following statements:
12
+ # 1. If index is added to the storage, no matter, a class
13
+ # or a string/symbol, it gets appended to the list.
14
+ # 2. If type is added to the storage, it filters out types
15
+ # assigned via indices.
16
+ # 3. But when a type class with non-existing index is added,
17
+ # this index got also added to the list if indices.
18
+ # 4. In cases when of an index identifier added, type
19
+ # indetifiers also got appended instead of filtering.
20
+ class Indices < Storage
21
+ # Two index storages are equal if they produce the
22
+ # same output on render.
23
+ #
24
+ # @see Chewy::Search::Parameters::Storage#==
25
+ # @param other [Chewy::Search::Parameters::Storage] any storage instance
26
+ # @return [true, false] the result of comparision
27
+ def ==(other)
28
+ super || other.class == self.class && other.render == render
29
+ end
30
+
31
+ # Just adds types to types and indices to indices.
32
+ #
33
+ # @see Chewy::Search::Parameters::Storage#update!
34
+ # @param other_value [{Symbol => Array<Chewy::Index, Chewy::Type, String, Symbol>}] any acceptable storage value
35
+ # @return [{Symbol => Array<Chewy::Index, Chewy::Type, String, Symbol>}] updated value
36
+ def update!(other_value)
37
+ new_value = normalize(other_value)
38
+
39
+ @value = {
40
+ indices: value[:indices] | new_value[:indices],
41
+ types: value[:types] | new_value[:types]
42
+ }
43
+ end
44
+
45
+ # Returns desired index and type names.
46
+ #
47
+ # @see Chewy::Search::Parameters::Storage#render
48
+ # @return [{Symbol => Array<String>}] rendered value with the parameter name
49
+ def render
50
+ {
51
+ index: index_names.uniq.sort,
52
+ type: type_names.uniq.sort
53
+ }.reject { |_, v| v.blank? }
54
+ end
55
+
56
+ # Returns index classes used for the request.
57
+ # No strings/symbos included.
58
+ #
59
+ # @return [Array<Chewy::Index>] a list of index classes
60
+ def indices
61
+ index_classes | type_classes.map(&:index)
62
+ end
63
+
64
+ # Returns type classes used for the request.
65
+ # No strings/symbos included.
66
+ #
67
+ # @return [Array<Chewy::Type>] a list of types classes
68
+ def types
69
+ type_classes | (index_classes - type_classes.map(&:index)).flat_map(&:types)
70
+ end
71
+
72
+ private
73
+
74
+ def initialize_clone(origin)
75
+ @value = origin.value.dup
76
+ end
77
+
78
+ def normalize(value)
79
+ value ||= {}
80
+
81
+ {
82
+ indices: Array.wrap(value[:indices]).flatten.compact,
83
+ types: Array.wrap(value[:types]).flatten.compact
84
+ }
85
+ end
86
+
87
+ def index_classes
88
+ value[:indices].select do |klass|
89
+ klass.is_a?(Class) && klass < Chewy::Index
90
+ end
91
+ end
92
+
93
+ def index_identifiers
94
+ value[:indices] - index_classes
95
+ end
96
+
97
+ def index_names
98
+ indices.map(&:index_name) | index_identifiers.map(&:to_s)
99
+ end
100
+
101
+ def type_classes
102
+ value[:types].select do |klass|
103
+ klass.is_a?(Class) && klass < Chewy::Type
104
+ end
105
+ end
106
+
107
+ def type_identifiers
108
+ value[:types] - type_classes
109
+ end
110
+
111
+ def type_names
112
+ type_names = types.map(&:type_name)
113
+
114
+ if index_identifiers.blank? && type_identifiers.present?
115
+ (type_names & type_identifiers.map(&:to_s)).presence || type_names
116
+ else
117
+ type_names | type_identifiers.map(&:to_s)
118
+ end
119
+ end
120
+ end
121
+ end
122
+ end
123
+ end
@@ -12,14 +12,12 @@ module Chewy
12
12
  include BoolStorage
13
13
 
14
14
  # Renders `match_none` query if the values is set to true.
15
- # Well, we can't really use match none because we need to support
16
- # ES2, so we are simulating it with `match_all` negation.
17
15
  #
18
16
  # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-all-query.html#query-dsl-match-none-query
19
17
  # @see Chewy::Search::Request
20
18
  # @see Chewy::Search::Request#response
21
19
  def render
22
- {query: {bool: {filter: {bool: {must_not: {match_all: {}}}}}}} if value.present?
20
+ {query: {match_none: {}}} if value.present?
23
21
  end
24
22
  end
25
23
  end
@@ -1,7 +1,7 @@
1
1
  module Chewy
2
2
  module Search
3
- # The main requset DSL class. Supports multiple index requests.
4
- # Supports ES2 and ES5 search API and query DSL.
3
+ # The main request DSL class. Supports multiple index requests.
4
+ # Supports ES5 search API and query DSL.
5
5
  #
6
6
  # @note The class tries to be as immutable as possible,
7
7
  # so most of the methods return a new instance of the class.
@@ -26,8 +26,9 @@ module Chewy
26
26
  timeout min_score source stored_fields search_after
27
27
  load script_fields suggest aggs aggregations none
28
28
  indices_boost rescore highlight total total_count
29
- total_entries types delete_all count exists? exist? find pluck
30
- scroll_batches scroll_hits scroll_results scroll_wrappers
29
+ total_entries indices types delete_all count exists?
30
+ exist? find pluck scroll_batches scroll_hits
31
+ scroll_results scroll_wrappers
31
32
  ].to_set.freeze
32
33
  DEFAULT_BATCH_SIZE = 1000
33
34
  DEFAULT_PLUCK_BATCH_SIZE = 10_000
@@ -51,24 +52,32 @@ module Chewy
51
52
  alias_method :total_count, :total
52
53
  alias_method :total_entries, :total
53
54
 
54
- attr_reader :_indexes, :_types
55
-
56
55
  # The class is initialized with the list of chewy indexes and/or
57
56
  # types, which are later used to compose requests.
57
+ # Any symbol/string passed is treated as an index identifier.
58
58
  #
59
59
  # @example
60
+ # Chewy::Search::Request.new(:places)
61
+ # # => <Chewy::Search::Request {:index=>["places"]}>
60
62
  # Chewy::Search::Request.new(PlacesIndex)
61
63
  # # => <Chewy::Search::Request {:index=>["places"], :type=>["city", "country"]}>
62
64
  # Chewy::Search::Request.new(PlacesIndex::City)
63
65
  # # => <Chewy::Search::Request {:index=>["places"], :type=>["city"]}>
64
66
  # Chewy::Search::Request.new(UsersIndex, PlacesIndex::City)
65
67
  # # => <Chewy::Search::Request {:index=>["users", "places"], :type=>["city", "user"]}>
66
- # @param indexes_or_types [Array<Chewy::Index, Chewy::Type>] indexes and types in any combinations
67
- def initialize(*indexes_or_types)
68
- @_types = indexes_or_types.select { |klass| klass < Chewy::Type }
69
- @_indexes = indexes_or_types.select { |klass| klass < Chewy::Index }
70
- @_types |= @_indexes.flat_map(&:types)
71
- @_indexes |= @_types.map(&:index)
68
+ # @param indexes_or_types [Array<Chewy::Index, Chewy::Type, String, Symbol>] indices and types in any combinations
69
+ def initialize(*indices_or_types)
70
+ indices = indices_or_types.reject do |klass|
71
+ klass.is_a?(Class) && klass < Chewy::Type
72
+ end
73
+
74
+ types = indices_or_types.select do |klass|
75
+ klass.is_a?(Class) && klass < Chewy::Type
76
+ end
77
+
78
+ parameters.modify!(:indices) do
79
+ replace!(indices: indices, types: types)
80
+ end
72
81
  end
73
82
 
74
83
  # Underlying parameter storage collection.
@@ -103,14 +112,23 @@ module Chewy
103
112
  # @see Chewy::Search::Response
104
113
  # @return [Chewy::Search::Response] a response object instance
105
114
  def response
106
- @response ||= Response.new(perform, loader, collection_paginator)
115
+ @response ||= build_response(perform)
116
+ end
117
+
118
+ # Wraps and sets the raw Elasticsearch response to provide access
119
+ # to convenience methods.
120
+ #
121
+ # @see Chewy::Search::Response
122
+ # @param from_elasticsearch [Hash] An Elasticsearch response
123
+ def response=(from_elasticsearch)
124
+ @response = build_response(from_elasticsearch)
107
125
  end
108
126
 
109
127
  # ES request body
110
128
  #
111
129
  # @return [Hash] request body
112
130
  def render
113
- @render ||= render_base.merge(parameters.render)
131
+ @render ||= parameters.render
114
132
  end
115
133
 
116
134
  # Includes the class name and the result of rendering.
@@ -281,21 +299,39 @@ module Chewy
281
299
  # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-docvalue-fields.html
282
300
  # @param values [Array<String, Symbol>] field names
283
301
  # @return [Chewy::Search::Request]
302
+ %i[order docvalue_fields].each do |name|
303
+ define_method name do |value, *values|
304
+ modify(name) { update!([value, *values]) }
305
+ end
306
+ end
307
+
308
+ # @!method indices(*values)
309
+ # Modifies `index` request parameter. Updates the storage on every call.
310
+ # Added passed indexes to the parameter list.
311
+ #
312
+ # @example
313
+ # UsersIndex.indices(CitiesIndex).indices(:another)
314
+ # # => <UsersIndex::Query {:index=>["another", "cities", "users"], :type=>["city", "user"]}>
315
+ # @see Chewy::Search::Parameters::Indices
316
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html
317
+ # @param values [Array<Chewy::Index, String, Symbol>] index names
318
+ # @return [Chewy::Search::Request]
284
319
  #
285
320
  # @!method types(*values)
286
- # Modifies `types` request parameter. Updates the storage on every call.
287
- # Constrains types passed on the request initialization.
321
+ # Modifies `type` request parameter. Updates the storage on every call.
322
+ # Constrains types passed on the request initialization or adds them
323
+ # to the list depending on circumstances.
288
324
  #
289
325
  # @example
290
- # PlacesIndex.types(:city).types(:unexistent)
291
- # # => <PlacesIndex::Query {:index=>["places"], :type=>["city"]}>
292
- # @see Chewy::Search::Parameters::Types
326
+ # UsersIndex.types(CitiesIndex::City).types(:unexistent)
327
+ # # => <UsersIndex::Query {:index=>["cities", "users"], :type=>["city", "user"]}>
328
+ # @see Chewy::Search::Parameters::Indices
293
329
  # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html
294
- # @param values [Array<String, Symbol>] type names
330
+ # @param values [Array<Chewy::Type, String, Symbol>] type names
295
331
  # @return [Chewy::Search::Request]
296
- %i[order docvalue_fields types].each do |name|
332
+ %i[indices types].each do |name|
297
333
  define_method name do |value, *values|
298
- modify(name) { update!([value, *values]) }
334
+ modify(:indices) { update!(name => [value, *values]) }
299
335
  end
300
336
  end
301
337
 
@@ -759,7 +795,7 @@ module Chewy
759
795
  # @param values [Array<String, Symbol>]
760
796
  # @return [Chewy::Search::Request] new scope
761
797
  def only(*values)
762
- chain { parameters.only!(values.flatten(1)) }
798
+ chain { parameters.only!(values.flatten(1) + [:indices]) }
763
799
  end
764
800
 
765
801
  # Returns a new scope containing all the storages except specified.
@@ -901,28 +937,28 @@ module Chewy
901
937
  end
902
938
 
903
939
  # Deletes all the documents from the specified scope it uses
904
- # `delete_by_query` API. For ES < 5.0 it uses `delete_by_query`
905
- # plugin, which requires additional installation effort.
940
+ # `delete_by_query`
906
941
  #
907
942
  # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-delete-by-query.html
908
- # @see https://www.elastic.co/guide/en/elasticsearch/plugins/2.0/plugins-delete-by-query.html
909
943
  # @note The result hash is different for different API used.
910
944
  # @param refresh [true, false] field names
911
945
  # @return [Hash] the result of query execution
912
946
  def delete_all(refresh: true)
913
947
  request_body = only(WHERE_STORAGES).render.merge(refresh: refresh)
914
948
  ActiveSupport::Notifications.instrument 'delete_query.chewy',
915
- request: request_body, indexes: _indexes, types: _types,
916
- index: _indexes.one? ? _indexes.first : _indexes,
917
- type: _types.one? ? _types.first : _types do
918
- if Runtime.version < '5.0'
919
- delete_by_query_plugin(request_body)
920
- else
921
- Chewy.client.delete_by_query(request_body)
922
- end
949
+ notification_payload(request: request_body) do
950
+ request_body[:body] = {query: {match_all: {}}} if request_body[:body].empty?
951
+ Chewy.client.delete_by_query(request_body)
923
952
  end
924
953
  end
925
954
 
955
+ # Returns whether or not the query has been performed.
956
+ #
957
+ # @return [true, false]
958
+ def performed?
959
+ !@response.nil?
960
+ end
961
+
926
962
  protected
927
963
 
928
964
  def initialize_clone(origin)
@@ -932,10 +968,12 @@ module Chewy
932
968
 
933
969
  private
934
970
 
971
+ def build_response(raw_response)
972
+ Response.new(raw_response, loader, collection_paginator)
973
+ end
974
+
935
975
  def compare_internals(other)
936
- _indexes.sort_by(&:derivable_name) == other._indexes.sort_by(&:derivable_name) &&
937
- _types.sort_by(&:derivable_name) == other._types.sort_by(&:derivable_name) &&
938
- parameters == other.parameters
976
+ parameters == other.parameters
939
977
  end
940
978
 
941
979
  def modify(name, &block)
@@ -947,58 +985,51 @@ module Chewy
947
985
  end
948
986
 
949
987
  def reset
950
- @response, @render, @render_base, @type_names, @index_names, @loader = nil
988
+ @response, @render, @loader = nil
951
989
  end
952
990
 
953
991
  def perform(additional = {})
954
992
  request_body = render.merge(additional)
993
+ request_body[:rest_total_hits_as_int] = true if Runtime.version >= '7.0.0'
955
994
  ActiveSupport::Notifications.instrument 'search_query.chewy',
956
- request: request_body, indexes: _indexes, types: _types,
957
- index: _indexes.one? ? _indexes.first : _indexes,
958
- type: _types.one? ? _types.first : _types do
959
- begin
960
- Chewy.client.search(request_body)
961
- rescue Elasticsearch::Transport::Transport::Errors::NotFound
962
- {}
995
+ notification_payload(request: request_body) do
996
+ begin
997
+ Chewy.client.search(request_body)
998
+ rescue Elasticsearch::Transport::Transport::Errors::NotFound
999
+ {}
1000
+ end
963
1001
  end
964
- end
965
1002
  end
966
1003
 
967
- def raw_limit_value
968
- parameters[:limit].value
969
- end
970
-
971
- def raw_offset_value
972
- parameters[:offset].value
1004
+ def notification_payload(additional)
1005
+ {
1006
+ indexes: _indices, types: _types,
1007
+ index: _indices.one? ? _indices.first : _indices,
1008
+ type: _types.one? ? _types.first : _types
1009
+ }.merge(additional)
973
1010
  end
974
1011
 
975
- def index_names
976
- @index_names ||= _indexes.map(&:index_name).uniq
1012
+ def _indices
1013
+ parameters[:indices].indices
977
1014
  end
978
1015
 
979
- def type_names
980
- @type_names ||= if parameters[:types].value.present?
981
- _types.map(&:type_name).uniq & parameters[:types].value
982
- else
983
- _types.map(&:type_name).uniq
984
- end
1016
+ def _types
1017
+ parameters[:indices].types
985
1018
  end
986
1019
 
987
- def render_base
988
- @render_base ||= {index: index_names, type: type_names, body: {}}
1020
+ def raw_limit_value
1021
+ parameters[:limit].value
989
1022
  end
990
1023
 
991
- def delete_by_query_plugin(request)
992
- path = Elasticsearch::API::Utils.__pathify(
993
- Elasticsearch::API::Utils.__listify(request[:index]),
994
- Elasticsearch::API::Utils.__listify(request[:type]),
995
- '_query'
996
- )
997
- Chewy.client.perform_request(Elasticsearch::API::HTTP_DELETE, path, {}, request[:body]).body
1024
+ def raw_offset_value
1025
+ parameters[:offset].value
998
1026
  end
999
1027
 
1000
1028
  def loader
1001
- @loader ||= Loader.new(indexes: @_indexes, **parameters[:load].value)
1029
+ @loader ||= Loader.new(
1030
+ indexes: parameters[:indices].indices,
1031
+ **parameters[:load].value
1032
+ )
1002
1033
  end
1003
1034
 
1004
1035
  def fetch_field(hit, field)
@@ -1009,10 +1040,6 @@ module Chewy
1009
1040
  end
1010
1041
  end
1011
1042
 
1012
- def performed?
1013
- !@response.nil?
1014
- end
1015
-
1016
1043
  def collection_paginator
1017
1044
  method(:paginated_collection).to_proc if respond_to?(:paginated_collection, true)
1018
1045
  end