chewy 0.10.0 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (157) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +240 -0
  3. data/.rubocop.yml +25 -25
  4. data/Appraisals +12 -10
  5. data/CHANGELOG.md +252 -263
  6. data/Gemfile +5 -1
  7. data/LICENSE.txt +1 -1
  8. data/README.md +142 -78
  9. data/chewy.gemspec +10 -12
  10. data/gemfiles/{rails.4.2.mongoid.5.1.gemfile → rails.5.2.activerecord.gemfile} +6 -4
  11. data/gemfiles/{rails.4.2.activerecord.gemfile → rails.5.2.mongoid.6.4.gemfile} +6 -4
  12. data/gemfiles/{rails.4.0.activerecord.gemfile → rails.6.0.activerecord.gemfile} +6 -3
  13. data/gemfiles/rails.6.1.activerecord.gemfile +19 -0
  14. data/gemfiles/sequel.4.45.gemfile +2 -2
  15. data/lib/chewy.rb +2 -1
  16. data/lib/chewy/backports/duplicable.rb +1 -1
  17. data/lib/chewy/config.rb +10 -39
  18. data/lib/chewy/fields/base.rb +40 -28
  19. data/lib/chewy/fields/root.rb +18 -11
  20. data/lib/chewy/index.rb +3 -1
  21. data/lib/chewy/index/actions.rb +27 -15
  22. data/lib/chewy/index/settings.rb +2 -0
  23. data/lib/chewy/index/specification.rb +12 -10
  24. data/lib/chewy/minitest/helpers.rb +6 -6
  25. data/lib/chewy/minitest/search_index_receiver.rb +17 -17
  26. data/lib/chewy/multi_search.rb +62 -0
  27. data/lib/chewy/railtie.rb +4 -4
  28. data/lib/chewy/rake_helper.rb +5 -5
  29. data/lib/chewy/rspec/update_index.rb +3 -5
  30. data/lib/chewy/search.rb +4 -11
  31. data/lib/chewy/search/loader.rb +1 -1
  32. data/lib/chewy/search/pagination/will_paginate.rb +4 -2
  33. data/lib/chewy/search/parameters.rb +24 -6
  34. data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
  35. data/lib/chewy/search/parameters/concerns/query_storage.rb +4 -3
  36. data/lib/chewy/search/parameters/indices.rb +123 -0
  37. data/lib/chewy/search/parameters/none.rb +1 -3
  38. data/lib/chewy/search/request.rb +100 -74
  39. data/lib/chewy/search/scrolling.rb +7 -6
  40. data/lib/chewy/stash.rb +30 -21
  41. data/lib/chewy/strategy/active_job.rb +1 -1
  42. data/lib/chewy/strategy/atomic.rb +1 -1
  43. data/lib/chewy/strategy/sidekiq.rb +1 -1
  44. data/lib/chewy/type.rb +5 -2
  45. data/lib/chewy/type/adapter/active_record.rb +1 -1
  46. data/lib/chewy/type/adapter/base.rb +9 -9
  47. data/lib/chewy/type/adapter/mongoid.rb +2 -4
  48. data/lib/chewy/type/adapter/orm.rb +7 -4
  49. data/lib/chewy/type/adapter/sequel.rb +5 -7
  50. data/lib/chewy/type/crutch.rb +1 -1
  51. data/lib/chewy/type/import.rb +13 -11
  52. data/lib/chewy/type/import/bulk_builder.rb +1 -1
  53. data/lib/chewy/type/import/bulk_request.rb +4 -2
  54. data/lib/chewy/type/import/journal_builder.rb +3 -3
  55. data/lib/chewy/type/import/routine.rb +3 -3
  56. data/lib/chewy/type/mapping.rb +42 -36
  57. data/lib/chewy/type/observe.rb +16 -12
  58. data/lib/chewy/type/syncer.rb +15 -14
  59. data/lib/chewy/type/witchcraft.rb +11 -7
  60. data/lib/chewy/type/wrapper.rb +14 -4
  61. data/lib/chewy/version.rb +1 -1
  62. data/lib/sequel/plugins/chewy_observe.rb +4 -19
  63. data/migration_guide.md +18 -0
  64. data/spec/chewy/config_spec.rb +16 -21
  65. data/spec/chewy/fields/base_spec.rb +70 -70
  66. data/spec/chewy/fields/root_spec.rb +56 -9
  67. data/spec/chewy/index/actions_spec.rb +63 -7
  68. data/spec/chewy/index/specification_spec.rb +25 -16
  69. data/spec/chewy/index_spec.rb +75 -45
  70. data/spec/chewy/journal_spec.rb +33 -29
  71. data/spec/chewy/minitest/search_index_receiver_spec.rb +11 -9
  72. data/spec/chewy/multi_search_spec.rb +85 -0
  73. data/spec/chewy/rake_helper_spec.rb +123 -95
  74. data/spec/chewy/rspec/update_index_spec.rb +47 -46
  75. data/spec/chewy/runtime_spec.rb +2 -2
  76. data/spec/chewy/search/pagination/kaminari_spec.rb +7 -3
  77. data/spec/chewy/search/pagination/will_paginate_spec.rb +9 -3
  78. data/spec/chewy/search/parameters/indices_spec.rb +190 -0
  79. data/spec/chewy/search/parameters/none_spec.rb +1 -1
  80. data/spec/chewy/search/parameters_spec.rb +21 -4
  81. data/spec/chewy/search/request_spec.rb +101 -70
  82. data/spec/chewy/search/response_spec.rb +27 -17
  83. data/spec/chewy/search/scrolling_spec.rb +25 -16
  84. data/spec/chewy/search_spec.rb +49 -35
  85. data/spec/chewy/stash_spec.rb +15 -13
  86. data/spec/chewy/strategy/active_job_spec.rb +15 -2
  87. data/spec/chewy/strategy/shoryuken_spec.rb +8 -2
  88. data/spec/chewy/strategy/sidekiq_spec.rb +6 -2
  89. data/spec/chewy/type/adapter/active_record_spec.rb +16 -4
  90. data/spec/chewy/type/import/bulk_builder_spec.rb +9 -94
  91. data/spec/chewy/type/import/journal_builder_spec.rb +17 -15
  92. data/spec/chewy/type/import_spec.rb +6 -0
  93. data/spec/chewy/type/mapping_spec.rb +51 -18
  94. data/spec/chewy/type/observe_spec.rb +4 -4
  95. data/spec/chewy/type/witchcraft_spec.rb +31 -0
  96. data/spec/chewy/type/wrapper_spec.rb +3 -1
  97. data/spec/chewy_spec.rb +0 -7
  98. data/spec/spec_helper.rb +5 -1
  99. data/spec/support/active_record.rb +20 -0
  100. metadata +46 -116
  101. data/.travis.yml +0 -53
  102. data/LEGACY_DSL.md +0 -497
  103. data/gemfiles/rails.4.1.activerecord.gemfile +0 -14
  104. data/gemfiles/rails.5.0.activerecord.gemfile +0 -15
  105. data/gemfiles/rails.5.0.mongoid.6.0.gemfile +0 -15
  106. data/gemfiles/rails.5.1.activerecord.gemfile +0 -15
  107. data/gemfiles/rails.5.1.mongoid.6.1.gemfile +0 -15
  108. data/lib/chewy/query.rb +0 -1098
  109. data/lib/chewy/query/compose.rb +0 -68
  110. data/lib/chewy/query/criteria.rb +0 -191
  111. data/lib/chewy/query/filters.rb +0 -227
  112. data/lib/chewy/query/loading.rb +0 -111
  113. data/lib/chewy/query/nodes/and.rb +0 -25
  114. data/lib/chewy/query/nodes/base.rb +0 -17
  115. data/lib/chewy/query/nodes/bool.rb +0 -34
  116. data/lib/chewy/query/nodes/equal.rb +0 -34
  117. data/lib/chewy/query/nodes/exists.rb +0 -20
  118. data/lib/chewy/query/nodes/expr.rb +0 -28
  119. data/lib/chewy/query/nodes/field.rb +0 -110
  120. data/lib/chewy/query/nodes/has_child.rb +0 -15
  121. data/lib/chewy/query/nodes/has_parent.rb +0 -15
  122. data/lib/chewy/query/nodes/has_relation.rb +0 -59
  123. data/lib/chewy/query/nodes/match_all.rb +0 -11
  124. data/lib/chewy/query/nodes/missing.rb +0 -20
  125. data/lib/chewy/query/nodes/not.rb +0 -25
  126. data/lib/chewy/query/nodes/or.rb +0 -25
  127. data/lib/chewy/query/nodes/prefix.rb +0 -19
  128. data/lib/chewy/query/nodes/query.rb +0 -20
  129. data/lib/chewy/query/nodes/range.rb +0 -63
  130. data/lib/chewy/query/nodes/raw.rb +0 -15
  131. data/lib/chewy/query/nodes/regexp.rb +0 -35
  132. data/lib/chewy/query/nodes/script.rb +0 -20
  133. data/lib/chewy/query/pagination.rb +0 -25
  134. data/spec/chewy/query/criteria_spec.rb +0 -700
  135. data/spec/chewy/query/filters_spec.rb +0 -201
  136. data/spec/chewy/query/loading_spec.rb +0 -124
  137. data/spec/chewy/query/nodes/and_spec.rb +0 -12
  138. data/spec/chewy/query/nodes/bool_spec.rb +0 -14
  139. data/spec/chewy/query/nodes/equal_spec.rb +0 -32
  140. data/spec/chewy/query/nodes/exists_spec.rb +0 -18
  141. data/spec/chewy/query/nodes/has_child_spec.rb +0 -59
  142. data/spec/chewy/query/nodes/has_parent_spec.rb +0 -59
  143. data/spec/chewy/query/nodes/match_all_spec.rb +0 -11
  144. data/spec/chewy/query/nodes/missing_spec.rb +0 -16
  145. data/spec/chewy/query/nodes/not_spec.rb +0 -13
  146. data/spec/chewy/query/nodes/or_spec.rb +0 -12
  147. data/spec/chewy/query/nodes/prefix_spec.rb +0 -16
  148. data/spec/chewy/query/nodes/query_spec.rb +0 -12
  149. data/spec/chewy/query/nodes/range_spec.rb +0 -32
  150. data/spec/chewy/query/nodes/raw_spec.rb +0 -11
  151. data/spec/chewy/query/nodes/regexp_spec.rb +0 -43
  152. data/spec/chewy/query/nodes/script_spec.rb +0 -15
  153. data/spec/chewy/query/pagination/kaminari_spec.rb +0 -5
  154. data/spec/chewy/query/pagination/will_paginate_spec.rb +0 -5
  155. data/spec/chewy/query/pagination_spec.rb +0 -39
  156. data/spec/chewy/query_spec.rb +0 -636
  157. data/spec/chewy/search/parameters/indices_boost_spec.rb +0 -83
@@ -4,12 +4,14 @@ module Chewy
4
4
  # hash. At first, you need to store some analyzers or other
5
5
  # analysis options to the corresponding repository:
6
6
  #
7
+ # @example
7
8
  # Chewy.analyzer :title_analyzer, type: 'custom', filter: %w(lowercase icu_folding title_nysiis)
8
9
  # Chewy.filter :title_nysiis, type: 'phonetic', encoder: 'nysiis', replace: false
9
10
  #
10
11
  # `title_nysiis` filter here will be expanded automatically when
11
12
  # `title_analyzer` analyser will be used in index settings:
12
13
  #
14
+ # @example
13
15
  # class ProductsIndex < Chewy::Index
14
16
  # settings analysis: {
15
17
  # analyzer: [
@@ -2,21 +2,21 @@ module Chewy
2
2
  class Index
3
3
  # Index specification is a combination of index settings and
4
4
  # mappings. The idea behind this class is that specification
5
- # can be locked in the `Chewy::Stash` between resets, so it is
6
- # possible to track changes. In the future it is planned to
7
- # be way smarter but right now `rake chewy:deploy` checks
8
- # if there were changes and resets the index only if anything
9
- # was changed. Otherwise, the index reset is skipped.
5
+ # can be locked in the `Chewy::Stash::Specification` between
6
+ # resets, so it is possible to track changes. In the future
7
+ # it is planned to be way smarter but right now `rake chewy:deploy`
8
+ # checks if there were changes and resets the index only if
9
+ # anything was changed. Otherwise, the index reset is skipped.
10
10
  #
11
- # @see Chewy::Stash
11
+ # @see Chewy::Stash::Specification
12
12
  class Specification
13
- # @see Chewy::Index.specification
13
+ # @see Chewy::Index::Specification
14
14
  # @param index [Chewy::Index] Just a chewy index
15
15
  def initialize(index)
16
16
  @index = index
17
17
  end
18
18
 
19
- # Stores the current index specification to the `Chewy::Stash`
19
+ # Stores the current index specification to the `Chewy::Stash::Specification`
20
20
  # as json.
21
21
  #
22
22
  # @raise [Chewy::ImportFailed] if something went wrong
@@ -24,7 +24,7 @@ module Chewy
24
24
  def lock!
25
25
  Chewy::Stash::Specification.import!([
26
26
  id: @index.derivable_name,
27
- value: current.to_json
27
+ specification: Base64.encode64(current.to_json)
28
28
  ], journal: false)
29
29
  end
30
30
 
@@ -34,7 +34,9 @@ module Chewy
34
34
  # @return [Hash] hash produced with JSON parser
35
35
  def locked
36
36
  filter = {ids: {values: [@index.derivable_name]}}
37
- JSON.parse(Chewy::Stash::Specification.filter(filter).first.try!(:value) || '{}')
37
+ document = Chewy::Stash::Specification.filter(filter).first
38
+ return {} unless document
39
+ JSON.load(Base64.decode64(document.specification)) # rubocop:disable Security/JSONLoad
38
40
  end
39
41
 
40
42
  # Simply returns `Chewy::Index.specification_hash`, but
@@ -6,14 +6,14 @@ module Chewy
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  # Assert that an index *changes* during a block.
9
- # @param (Chewy::Type) index the index / type to watch, eg EntitiesIndex::Entity.
10
- # @param (Symbol) strategy the Chewy strategy to use around the block. See Chewy docs.
11
- # @param (boolean) assert the index changes
12
- # @param (boolean) bypass_actual_index
9
+ # @param index [Chewy::Type] the index / type to watch, eg EntitiesIndex::Entity.
10
+ # @param strategy [Symbol] the Chewy strategy to use around the block. See Chewy docs.
11
+ # @param bypass_actual_index [true, false]
13
12
  # True to preempt the http call to Elastic, false otherwise.
14
13
  # Should be set to true unless actually testing search functionality.
15
14
  #
16
- # @return (SearchIndexReceiver) for optional further assertions on the nature of the index changes.
15
+ # @return [SearchIndexReceiver] for optional further assertions on the nature of the index changes.
16
+ #
17
17
  def assert_indexes(index, strategy: :atomic, bypass_actual_index: true)
18
18
  type = Chewy.derive_type index
19
19
  receiver = SearchIndexReceiver.new
@@ -43,7 +43,7 @@ module Chewy
43
43
 
44
44
  # Run indexing for the database changes during the block provided.
45
45
  # By default, indexing is run at the end of the block.
46
- # @param (Symbol) strategy the Chewy index update strategy see Chewy docs.
46
+ # @param strategy [Symbol] the Chewy index update strategy see Chewy docs.
47
47
  def run_indexing(strategy: :atomic)
48
48
  Chewy.strategy strategy do
49
49
  yield
@@ -1,17 +1,17 @@
1
1
  # Test helper class to provide minitest hooks for Chewy::Index testing.
2
2
  #
3
3
  # @note Intended to be used in conjunction with a test helper which mocks over the #bulk
4
- # method on a Chewy::Type class. (See SearchTestHelper)
4
+ # method on a {Chewy::Type} class. (See SearchTestHelper)
5
5
  #
6
- # The class will capture the data from the *param on the Chewy::Type#bulk method and
6
+ # The class will capture the data from the *param on the Chewy::Type.bulk method and
7
7
  # aggregate the data for test analysis.
8
8
  class SearchIndexReceiver
9
9
  def initialize
10
10
  @mutations = {}
11
11
  end
12
12
 
13
- # @param bulk_params the bulk_params that should be sent to the Chewy::Type#bulk method.
14
- # @param (Chewy::Type) type the Index::Type executing this query.
13
+ # @param bulk_params [Hash] the bulk_params that should be sent to the Chewy::Type.bulk method.
14
+ # @param type [Chewy::Type] the type executing this query.
15
15
  def catch(bulk_params, type)
16
16
  Array.wrap(bulk_params).map { |y| y[:body] }.flatten.each do |update|
17
17
  if update[:delete]
@@ -22,8 +22,8 @@ class SearchIndexReceiver
22
22
  end
23
23
  end
24
24
 
25
- # @param index return only index requests to the specified Chewy::Type index.
26
- # @return the index changes captured by the mock.
25
+ # @param index [Chewy::Index] return only index requests to the specified {Chewy::Type} index.
26
+ # @return [Hash] the index changes captured by the mock.
27
27
  def indexes_for(index = nil)
28
28
  if index
29
29
  mutation_for(index).indexes
@@ -35,8 +35,8 @@ class SearchIndexReceiver
35
35
  end
36
36
  alias_method :indexes, :indexes_for
37
37
 
38
- # @param index return only delete requests to the specified Chewy::Type index.
39
- # @return the index deletes captured by the mock.
38
+ # @param index [Chewy::Index] return only delete requests to the specified {Chewy::Type} index.
39
+ # @return [Hash] the index deletes captured by the mock.
40
40
  def deletes_for(index = nil)
41
41
  if index
42
42
  mutation_for(index).deletes
@@ -49,22 +49,22 @@ class SearchIndexReceiver
49
49
  alias_method :deletes, :deletes_for
50
50
 
51
51
  # Check to see if a given object has been indexed.
52
- # @param (#id) obj the object to look for.
53
- # @param Chewy::Type what type the object should be indexed as.
54
- # @return bool if the object was indexed.
52
+ # @param obj [#id] obj the object to look for.
53
+ # @param type [Chewy::Type] what type the object should be indexed as.
54
+ # @return [true, false] if the object was indexed.
55
55
  def indexed?(obj, type)
56
56
  indexes_for(type).map { |i| i[:_id] }.include? obj.id
57
57
  end
58
58
 
59
59
  # Check to see if a given object has been deleted.
60
- # @param (#id) obj the object to look for.
61
- # @param Chewy::Type what type the object should have been deleted from.
62
- # @return bool if the object was deleted.
60
+ # @param obj [#id] obj the object to look for.
61
+ # @param type [Chewy::Type] what type the object should have been deleted from.
62
+ # @return [true, false] if the object was deleted.
63
63
  def deleted?(obj, type)
64
64
  deletes_for(type).include? obj.id
65
65
  end
66
66
 
67
- # @return a list of Chewy::Type indexes changed.
67
+ # @return [Array<Chewy::Type>] a list of types indexes changed.
68
68
  def updated_indexes
69
69
  @mutations.keys
70
70
  end
@@ -72,8 +72,8 @@ class SearchIndexReceiver
72
72
  private
73
73
 
74
74
  # Get the mutation object for a given type.
75
- # @param (Chewy::Type) type the index type to fetch.
76
- # @return (#indexes, #deletes) an object with a list of indexes and a list of deletes.
75
+ # @param type [Chewy::Type] the index type to fetch.
76
+ # @return [#indexes, #deletes] an object with a list of indexes and a list of deletes.
77
77
  def mutation_for(type)
78
78
  @mutations[type] ||= OpenStruct.new(indexes: [], deletes: [])
79
79
  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
@@ -7,7 +7,6 @@ module Chewy
7
7
  class RequestStrategy
8
8
  def initialize(app)
9
9
  @app = app
10
- Chewy.logger.debug("Chewy strategies stack: [1] <- #{Chewy.request_strategy}")
11
10
  end
12
11
 
13
12
  def call(env)
@@ -15,6 +14,8 @@ module Chewy
15
14
  if Rails.application.config.respond_to?(:assets) && env['PATH_INFO'].start_with?(Rails.application.config.assets.prefix)
16
15
  @app.call(env)
17
16
  else
17
+ Chewy.logger.info("Chewy request strategy is `#{Chewy.request_strategy}`") if Chewy.logger && @request_strategy != Chewy.request_strategy
18
+ @request_strategy = Chewy.request_strategy
18
19
  Chewy.strategy(Chewy.request_strategy) { @app.call(env) }
19
20
  end
20
21
  end
@@ -42,13 +43,12 @@ module Chewy
42
43
  end
43
44
 
44
45
  console do |app|
45
- Chewy.logger = ActiveRecord::Base.logger if defined?(ActiveRecord)
46
-
47
46
  if app.sandbox?
48
47
  Chewy.strategy(:bypass)
49
48
  else
50
49
  Chewy.strategy(:urgent)
51
50
  end
51
+ puts "Chewy console strategy is `#{Chewy.strategy.current.name}`"
52
52
  end
53
53
 
54
54
  initializer 'chewy.logger', after: 'active_record.logger' do
@@ -68,7 +68,7 @@ module Chewy
68
68
  end
69
69
 
70
70
  initializer 'chewy.request_strategy' do |app|
71
- app.config.middleware.insert_after(Rails::Rack::Logger, RequestStrategy)
71
+ app.config.middleware.insert_before(ActionDispatch::ShowExceptions, RequestStrategy)
72
72
  end
73
73
 
74
74
  initializer 'chewy.add_indices_path' do |_app|
@@ -1,9 +1,9 @@
1
1
  module Chewy
2
2
  module RakeHelper
3
- IMPORT_CALLBACK = lambda do |output, _name, start, finish, _id, payload| # rubocop:disable Metrics/ParameterLists
3
+ IMPORT_CALLBACK = lambda do |output, _name, start, finish, _id, payload|
4
4
  duration = (finish - start).ceil
5
5
  stats = payload.fetch(:import, {}).map { |key, count| "#{key} #{count}" }.join(', ')
6
- output.puts " Imported #{payload[:type]} for #{human_duration(duration)}, stats: #{stats}"
6
+ output.puts " Imported #{payload[:type]} in #{human_duration(duration)}, stats: #{stats}"
7
7
  if payload[:errors]
8
8
  payload[:errors].each do |action, errors|
9
9
  output.puts " #{action.to_s.humanize} errors:"
@@ -15,7 +15,7 @@ module Chewy
15
15
  end
16
16
  end
17
17
 
18
- JOURNAL_CALLBACK = lambda do |output, _, _, _, _, payload| # rubocop:disable Metrics/ParameterLists
18
+ JOURNAL_CALLBACK = lambda do |output, _, _, _, _, payload|
19
19
  count = payload[:groups].values.map(&:size).sum
20
20
  targets = payload[:groups].keys.sort_by(&:derivable_name)
21
21
  output.puts " Applying journal to #{targets}, #{count} entries, stage #{payload[:stage]}"
@@ -196,12 +196,12 @@ module Chewy
196
196
  end
197
197
 
198
198
  # Eager loads and returns all the indexes defined in the application
199
- # except the Chewy::Stash.
199
+ # except Chewy::Stash::Specification and Chewy::Stash::Journal.
200
200
  #
201
201
  # @return [Array<Chewy::Index>] indexes found
202
202
  def all_indexes
203
203
  Chewy.eager_load!
204
- Chewy::Index.descendants - [Chewy::Stash]
204
+ Chewy::Index.descendants - [Chewy::Stash::Journal, Chewy::Stash::Specification]
205
205
  end
206
206
 
207
207
  def normalize_indexes(*identifiers)
@@ -83,9 +83,7 @@ RSpec::Matchers.define :update_index do |type_name, options = {}| # rubocop:disa
83
83
  # .to update_index(UsersIndex.user).and_reindex(user1).only }
84
84
  #
85
85
  chain(:only) do |*_args|
86
- if @reindex.blank? && @delete.blank?
87
- raise 'Use `only` in conjunction with `and_reindex` or `and_delete`'
88
- end
86
+ raise 'Use `only` in conjunction with `and_reindex` or `and_delete`' if @reindex.blank? && @delete.blank?
89
87
 
90
88
  @only = true
91
89
  end
@@ -128,13 +126,13 @@ RSpec::Matchers.define :update_index do |type_name, options = {}| # rubocop:disa
128
126
  end
129
127
  end
130
128
 
131
- @reindex.each do |_, document|
129
+ @reindex.each_value do |document|
132
130
  document[:match_count] = (!document[:expected_count] && document[:real_count] > 0) ||
133
131
  (document[:expected_count] && document[:expected_count] == document[:real_count])
134
132
  document[:match_attributes] = document[:expected_attributes].blank? ||
135
133
  compare_attributes(document[:expected_attributes], document[:real_attributes])
136
134
  end
137
- @delete.each do |_, document|
135
+ @delete.each_value do |document|
138
136
  document[:match_count] = (!document[:expected_count] && document[:real_count] > 0) ||
139
137
  (document[:expected_count] && document[:expected_count] == document[:real_count])
140
138
  end
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
 
@@ -91,12 +84,12 @@ module Chewy
91
84
 
92
85
  def build_search_class(base)
93
86
  search_class = Class.new(base)
87
+
94
88
  if self < Chewy::Type
95
89
  index_scopes = index.scopes - scopes
96
-
97
90
  delegate_scoped index, search_class, index_scopes
98
- delegate_scoped index, self, index_scopes
99
91
  end
92
+
100
93
  delegate_scoped self, search_class, scopes
101
94
  const_set('Query', search_class)
102
95
  end
@@ -52,7 +52,7 @@ module Chewy
52
52
 
53
53
  type = derive_type(index_name, type_name)
54
54
  ids = hit_group.map { |hit| hit['_id'] }
55
- loaded = type.adapter.load(ids, @options.merge(_type: type))
55
+ loaded = type.adapter.load(ids, **@options.merge(_type: type))
56
56
  loaded ||= hit_group.map { |hit| type.build(hit) }
57
57
 
58
58
  result.merge!(hit_group.zip(loaded).to_h)
@@ -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
 
@@ -31,7 +31,9 @@ module Chewy
31
31
  private
32
32
 
33
33
  def paginated_collection(collection)
34
- ::WillPaginate::Collection.create(current_page, per_page, total_entries) do |pager|
34
+ page = current_page || 1
35
+ per = per_page || ::WillPaginate.per_page
36
+ ::WillPaginate::Collection.create(page, per, total_entries) do |pager|
35
37
  pager.replace collection
36
38
  end
37
39
  end
@@ -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