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/lib/chewy/search.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require 'chewy/search/scoping'
2
- require 'chewy/query'
3
2
  require 'chewy/search/scrolling'
4
3
  require 'chewy/search/query_proxy'
5
4
  require 'chewy/search/parameters'
@@ -15,9 +14,7 @@ module Chewy
15
14
  # {Chewy::Type}.
16
15
  #
17
16
  # The class used as a request DSL provider is
18
- # inherited from {Chewy::Search::Request} by default, but if you
19
- # need ES < 2.0 DSL support - you can switch it to {Chewy::Query}
20
- # using {Chewy::Config#search_class}
17
+ # inherited from {Chewy::Search::Request}
21
18
  #
22
19
  # Also, the search class is refined with one of the pagination-
23
20
  # providing modules: {Chewy::Search::Pagination::Kaminari} or
@@ -58,11 +55,7 @@ module Chewy
58
55
  # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html
59
56
  # @return [Hash] the request result
60
57
  def search_string(query, options = {})
61
- options = options.merge(
62
- index: all._indexes.map(&:index_name),
63
- type: all._types.map(&:type_name),
64
- q: query
65
- )
58
+ options = options.merge(all.render.slice(:index, :type).merge(q: query))
66
59
  Chewy.client.search(options)
67
60
  end
68
61
 
@@ -9,14 +9,10 @@ module Chewy
9
9
  # @see Chewy::Search::Scrolling#scroll_objects
10
10
  class Loader
11
11
  # @param indexes [Array<Chewy::Index>] list of indexes to lookup types
12
- # @param only [Array<String, Symbol>] list of selected type names to load
13
- # @param except [Array<String, Symbol>] list of type names which will not be loaded
14
12
  # @param options [Hash] adapter-specific load options
15
13
  # @see Chewy::Type::Adapter::Base#load
16
- def initialize(indexes: [], only: [], except: [], **options)
14
+ def initialize(indexes: [], **options)
17
15
  @indexes = indexes
18
- @only = Array.wrap(only).map(&:to_s)
19
- @except = Array.wrap(except).map(&:to_s)
20
16
  @options = options
21
17
  end
22
18
 
@@ -31,7 +27,7 @@ module Chewy
31
27
  (@derive_type ||= {})[[index, type]] ||= begin
32
28
  index_class = derive_index(index)
33
29
  raise Chewy::UnderivableType, "Can not find index named `#{index}`" unless index_class
34
- index_class.type_hash[type] or raise Chewy::UnderivableType, "Index `#{index}` doesn`t have type named `#{type}`"
30
+ index_class.type_hash.values.first
35
31
  end
36
32
  end
37
33
 
@@ -48,11 +44,9 @@ module Chewy
48
44
  def load(hits)
49
45
  hit_groups = hits.group_by { |hit| [hit['_index'], hit['_type']] }
50
46
  loaded_objects = hit_groups.each_with_object({}) do |((index_name, type_name), hit_group), result|
51
- next if skip_type?(type_name)
52
-
53
47
  type = derive_type(index_name, type_name)
54
48
  ids = hit_group.map { |hit| hit['_id'] }
55
- loaded = type.adapter.load(ids, @options.merge(_type: type))
49
+ loaded = type.adapter.load(ids, **@options.merge(_type: type))
56
50
  loaded ||= hit_group.map { |hit| type.build(hit) }
57
51
 
58
52
  result.merge!(hit_group.zip(loaded).to_h)
@@ -74,10 +68,6 @@ module Chewy
74
68
  def indexes_hash
75
69
  @indexes_hash ||= @indexes.index_by(&:index_name)
76
70
  end
77
-
78
- def skip_type?(type_name)
79
- @except.include?(type_name) || @only.present? && !@only.include?(type_name)
80
- end
81
71
  end
82
72
  end
83
73
  end
@@ -20,7 +20,7 @@ module Chewy
20
20
  @page_multiplier = @current_page - 1
21
21
  @per_page = (options[:per_page] || @per_page || ::WillPaginate.per_page).to_i
22
22
 
23
- # call Chewy::Query methods to limit results
23
+ # call Chewy::Search::Request methods to limit results
24
24
  limit(@per_page).offset(@page_multiplier * @per_page)
25
25
  end
26
26
 
@@ -10,6 +10,8 @@ module Chewy
10
10
  # @see Chewy::Search::Request#parameters
11
11
  # @see Chewy::Search::Parameters::Storage
12
12
  class Parameters
13
+ QUERY_STRING_STORAGES = %i[indices search_type request_cache allow_partial_search_results].freeze
14
+
13
15
  # Default storage classes warehouse. It is probably possible to
14
16
  # add your own classes here if necessary, but I'm not sure it will work.
15
17
  #
@@ -33,10 +35,11 @@ module Chewy
33
35
  # limit: Chewy::Search::Parameters::Offset.new(10)
34
36
  # )
35
37
  # @param initial [{Symbol => Object, Chewy::Search::Parameters::Storage}]
36
- def initialize(initial = {})
38
+ def initialize(initial = {}, **kinitial)
37
39
  @storages = Hash.new do |hash, name|
38
40
  hash[name] = self.class.storages[name].new
39
41
  end
42
+ initial = initial.deep_dup.merge(kinitial)
40
43
  initial.each_with_object(@storages) do |(name, value), result|
41
44
  storage_class = self.class.storages[name]
42
45
  storage = value.is_a?(storage_class) ? value : storage_class.new(value)
@@ -101,11 +104,7 @@ module Chewy
101
104
  #
102
105
  # @return [Hash] request body
103
106
  def render
104
- body = @storages.except(:filter, :query, :none).values.inject({}) do |result, storage|
105
- result.merge!(storage.render || {})
106
- end
107
- body.merge!(render_query || {})
108
- body.present? ? {body: body} : {}
107
+ render_query_string_params.merge(render_body)
109
108
  end
110
109
 
111
110
  protected
@@ -126,6 +125,25 @@ module Chewy
126
125
  names
127
126
  end
128
127
 
128
+ def render_query_string_params
129
+ query_string_storages = @storages.select do |storage_name, _|
130
+ QUERY_STRING_STORAGES.include?(storage_name)
131
+ end
132
+
133
+ query_string_storages.values.inject({}) do |result, storage|
134
+ result.merge!(storage.render || {})
135
+ end
136
+ end
137
+
138
+ def render_body
139
+ exceptions = %i[filter query none] + QUERY_STRING_STORAGES
140
+ body = @storages.except(*exceptions).values.inject({}) do |result, storage|
141
+ result.merge!(storage.render || {})
142
+ end
143
+ body.merge!(render_query || {})
144
+ {body: body}
145
+ end
146
+
129
147
  def render_query
130
148
  none = @storages[:none].render
131
149
 
@@ -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
@@ -0,0 +1,78 @@
1
+ require 'chewy/search/parameters/storage'
2
+
3
+ module Chewy
4
+ module Search
5
+ class Parameters
6
+ # Stores indices to query.
7
+ # Renders it to lists of string accepted by ElasticSearch
8
+ # API.
9
+ #
10
+ # If index is added to the storage, no matter, a class
11
+ # or a string/symbol, it gets appended to the list.
12
+ class Indices < Storage
13
+ # Two index storages are equal if they produce the
14
+ # same output on render.
15
+ #
16
+ # @see Chewy::Search::Parameters::Storage#==
17
+ # @param other [Chewy::Search::Parameters::Storage] any storage instance
18
+ # @return [true, false] the result of comparison
19
+ def ==(other)
20
+ super || other.class == self.class && other.render == render
21
+ end
22
+
23
+ # Just adds indices to indices.
24
+ #
25
+ # @see Chewy::Search::Parameters::Storage#update!
26
+ # @param other_value [{Symbol => Array<Chewy::Index, String, Symbol>}] any acceptable storage value
27
+ # @return [{Symbol => Array<Chewy::Index, String, Symbol>}] updated value
28
+ def update!(other_value)
29
+ new_value = normalize(other_value)
30
+
31
+ @value = {indices: value[:indices] | new_value[:indices]}
32
+ end
33
+
34
+ # Returns desired index names.
35
+ #
36
+ # @see Chewy::Search::Parameters::Storage#render
37
+ # @return [{Symbol => Array<String>}] rendered value with the parameter name
38
+ def render
39
+ {index: index_names.uniq.sort}.reject { |_, v| v.blank? }
40
+ end
41
+
42
+ # Returns index classes used for the request.
43
+ # No strings/symbols included.
44
+ #
45
+ # @return [Array<Chewy::Index>] a list of index classes
46
+ def indices
47
+ index_classes
48
+ end
49
+
50
+ private
51
+
52
+ def initialize_clone(origin)
53
+ @value = origin.value.dup
54
+ end
55
+
56
+ def normalize(value)
57
+ value ||= {}
58
+
59
+ {indices: Array.wrap(value[:indices]).flatten.compact}
60
+ end
61
+
62
+ def index_classes
63
+ value[:indices].select do |klass|
64
+ klass.is_a?(Class) && klass < Chewy::Index
65
+ end
66
+ end
67
+
68
+ def index_identifiers
69
+ value[:indices] - index_classes
70
+ end
71
+
72
+ def index_names
73
+ indices.map(&:index_name) | index_identifiers.map(&:to_s)
74
+ end
75
+ end
76
+ end
77
+ end
78
+ 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
@@ -40,7 +41,7 @@ module Chewy
40
41
  EXTRA_STORAGES = %i[aggs suggest].freeze
41
42
  # An array of storage names that are changing the returned hist collection in any way.
42
43
  WHERE_STORAGES = %i[
43
- query filter post_filter none types min_score rescore indices_boost
44
+ query filter post_filter none min_score rescore indices_boost
44
45
  ].freeze
45
46
 
46
47
  delegate :hits, :wrappers, :objects, :records, :documents,
@@ -51,24 +52,34 @@ 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
- # # => <Chewy::Search::Request {:index=>["places"], :type=>["city", "country"]}>
63
+ # # => <Chewy::Search::Request {:index=>["places"]}>
62
64
  # Chewy::Search::Request.new(PlacesIndex::City)
63
- # # => <Chewy::Search::Request {:index=>["places"], :type=>["city"]}>
65
+ # # => <Chewy::Search::Request {:index=>["places"]}>
64
66
  # Chewy::Search::Request.new(UsersIndex, PlacesIndex::City)
65
- # # => <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)
67
+ # # => <Chewy::Search::Request {:index=>["users", "places"]}>
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
+ indices += types.map(&:index)
79
+
80
+ parameters.modify!(:indices) do
81
+ replace!(indices: indices)
82
+ end
72
83
  end
73
84
 
74
85
  # Underlying parameter storage collection.
@@ -103,14 +114,23 @@ module Chewy
103
114
  # @see Chewy::Search::Response
104
115
  # @return [Chewy::Search::Response] a response object instance
105
116
  def response
106
- @response ||= Response.new(perform, loader, collection_paginator)
117
+ @response ||= build_response(perform)
118
+ end
119
+
120
+ # Wraps and sets the raw Elasticsearch response to provide access
121
+ # to convenience methods.
122
+ #
123
+ # @see Chewy::Search::Response
124
+ # @param from_elasticsearch [Hash] An Elasticsearch response
125
+ def response=(from_elasticsearch)
126
+ @response = build_response(from_elasticsearch)
107
127
  end
108
128
 
109
129
  # ES request body
110
130
  #
111
131
  # @return [Hash] request body
112
132
  def render
113
- @render ||= render_base.merge(parameters.render)
133
+ @render ||= parameters.render
114
134
  end
115
135
 
116
136
  # Includes the class name and the result of rendering.
@@ -281,24 +301,26 @@ module Chewy
281
301
  # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-docvalue-fields.html
282
302
  # @param values [Array<String, Symbol>] field names
283
303
  # @return [Chewy::Search::Request]
284
- #
285
- # @!method types(*values)
286
- # Modifies `types` request parameter. Updates the storage on every call.
287
- # Constrains types passed on the request initialization.
288
- #
289
- # @example
290
- # PlacesIndex.types(:city).types(:unexistent)
291
- # # => <PlacesIndex::Query {:index=>["places"], :type=>["city"]}>
292
- # @see Chewy::Search::Parameters::Types
293
- # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html
294
- # @param values [Array<String, Symbol>] type names
295
- # @return [Chewy::Search::Request]
296
- %i[order docvalue_fields types].each do |name|
304
+ %i[order docvalue_fields].each do |name|
297
305
  define_method name do |value, *values|
298
306
  modify(name) { update!([value, *values]) }
299
307
  end
300
308
  end
301
309
 
310
+ # Modifies `index` request parameter. Updates the storage on every call.
311
+ # Added passed indexes to the parameter list.
312
+ #
313
+ # @example
314
+ # UsersIndex.indices(CitiesIndex).indices(:another)
315
+ # # => <UsersIndex::Query {:index=>["another", "cities", "users"]}>
316
+ # @see Chewy::Search::Parameters::Indices
317
+ # @see https://www.elastic.co/guide/en/elasticsearch/reference/current/search-search.html
318
+ # @param values [Array<Chewy::Index, String, Symbol>] index names
319
+ # @return [Chewy::Search::Request]
320
+ def indices(value, *values)
321
+ modify(:indices) { update!(indices: [value, *values]) }
322
+ end
323
+
302
324
  # @overload reorder(*values)
303
325
  # Replaces the value of the `sort` parameter with the provided value.
304
326
  #
@@ -759,7 +781,7 @@ module Chewy
759
781
  # @param values [Array<String, Symbol>]
760
782
  # @return [Chewy::Search::Request] new scope
761
783
  def only(*values)
762
- chain { parameters.only!(values.flatten(1)) }
784
+ chain { parameters.only!(values.flatten(1) + [:indices]) }
763
785
  end
764
786
 
765
787
  # Returns a new scope containing all the storages except specified.
@@ -901,28 +923,28 @@ module Chewy
901
923
  end
902
924
 
903
925
  # 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.
926
+ # `delete_by_query`
906
927
  #
907
928
  # @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
929
  # @note The result hash is different for different API used.
910
930
  # @param refresh [true, false] field names
911
931
  # @return [Hash] the result of query execution
912
932
  def delete_all(refresh: true)
913
933
  request_body = only(WHERE_STORAGES).render.merge(refresh: refresh)
914
934
  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
935
+ notification_payload(request: request_body) do
936
+ request_body[:body] = {query: {match_all: {}}} if request_body[:body].empty?
937
+ Chewy.client.delete_by_query(request_body)
923
938
  end
924
939
  end
925
940
 
941
+ # Returns whether or not the query has been performed.
942
+ #
943
+ # @return [true, false]
944
+ def performed?
945
+ !@response.nil?
946
+ end
947
+
926
948
  protected
927
949
 
928
950
  def initialize_clone(origin)
@@ -932,10 +954,12 @@ module Chewy
932
954
 
933
955
  private
934
956
 
957
+ def build_response(raw_response)
958
+ Response.new(raw_response, loader, collection_paginator)
959
+ end
960
+
935
961
  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
962
+ parameters == other.parameters
939
963
  end
940
964
 
941
965
  def modify(name, &block)
@@ -947,58 +971,45 @@ module Chewy
947
971
  end
948
972
 
949
973
  def reset
950
- @response, @render, @render_base, @type_names, @index_names, @loader = nil
974
+ @response, @render, @loader = nil
951
975
  end
952
976
 
953
977
  def perform(additional = {})
954
978
  request_body = render.merge(additional)
955
979
  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
- {}
980
+ notification_payload(request: request_body) do
981
+ begin
982
+ Chewy.client.search(request_body)
983
+ rescue Elasticsearch::Transport::Transport::Errors::NotFound
984
+ {}
985
+ end
963
986
  end
964
- end
965
987
  end
966
988
 
967
- def raw_limit_value
968
- parameters[:limit].value
989
+ def notification_payload(additional)
990
+ {
991
+ indexes: _indices,
992
+ index: _indices.one? ? _indices.first : _indices
993
+ }.merge(additional)
969
994
  end
970
995
 
971
- def raw_offset_value
972
- parameters[:offset].value
973
- end
974
-
975
- def index_names
976
- @index_names ||= _indexes.map(&:index_name).uniq
996
+ def _indices
997
+ parameters[:indices].indices
977
998
  end
978
999
 
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
985
- end
986
-
987
- def render_base
988
- @render_base ||= {index: index_names, type: type_names, body: {}}
1000
+ def raw_limit_value
1001
+ parameters[:limit].value
989
1002
  end
990
1003
 
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
1004
+ def raw_offset_value
1005
+ parameters[:offset].value
998
1006
  end
999
1007
 
1000
1008
  def loader
1001
- @loader ||= Loader.new(indexes: @_indexes, **parameters[:load].value)
1009
+ @loader ||= Loader.new(
1010
+ indexes: parameters[:indices].indices,
1011
+ **parameters[:load].value
1012
+ )
1002
1013
  end
1003
1014
 
1004
1015
  def fetch_field(hit, field)
@@ -1009,10 +1020,6 @@ module Chewy
1009
1020
  end
1010
1021
  end
1011
1022
 
1012
- def performed?
1013
- !@response.nil?
1014
- end
1015
-
1016
1023
  def collection_paginator
1017
1024
  method(:paginated_collection).to_proc if respond_to?(:paginated_collection, true)
1018
1025
  end