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
@@ -5,14 +5,25 @@ 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
+ get_args = {index: index_name}
9
+ get_args[:include_type_name] = true if Runtime.version >= '6.7.0'
10
+ indexes = empty_if_not_found { client.indices.get(**get_args).keys }
11
+ indexes += empty_if_not_found { client.indices.get_alias(name: index_name).keys }
12
+ indexes.compact.uniq
11
13
  end
12
14
 
13
15
  def aliases
14
- name = index_name
15
- client.indices.get_alias(index: name, name: '*')[name].try(:[], 'aliases').try(:keys) || []
16
+ empty_if_not_found do
17
+ client.indices.get_alias(index: index_name, name: '*').values.flat_map do |aliases|
18
+ aliases['aliases'].keys
19
+ end
20
+ end.compact.uniq
21
+ end
22
+
23
+ private
24
+
25
+ def empty_if_not_found
26
+ yield
16
27
  rescue Elasticsearch::Transport::Transport::Errors::NotFound
17
28
  []
18
29
  end
@@ -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
@@ -14,7 +14,7 @@ module Chewy
14
14
  if Rails.application.config.respond_to?(:assets) && env['PATH_INFO'].start_with?(Rails.application.config.assets.prefix)
15
15
  @app.call(env)
16
16
  else
17
- Chewy.logger.info("Chewy request strategy is `#{Chewy.request_strategy}`") if @request_strategy != Chewy.request_strategy
17
+ Chewy.logger.info("Chewy request strategy is `#{Chewy.request_strategy}`") if Chewy.logger && @request_strategy != Chewy.request_strategy
18
18
  @request_strategy = Chewy.request_strategy
19
19
  Chewy.strategy(Chewy.request_strategy) { @app.call(env) }
20
20
  end
@@ -43,14 +43,12 @@ module Chewy
43
43
  end
44
44
 
45
45
  console do |app|
46
- Chewy.logger = ActiveRecord::Base.logger if defined?(ActiveRecord)
47
-
48
46
  if app.sandbox?
49
47
  Chewy.strategy(:bypass)
50
48
  else
51
49
  Chewy.strategy(:urgent)
52
50
  end
53
- Chewy.logger.info("Chewy console strategy is `#{Chewy.strategy.current.name}`")
51
+ puts "Chewy console strategy is `#{Chewy.strategy.current.name}`"
54
52
  end
55
53
 
56
54
  initializer 'chewy.logger', after: 'active_record.logger' do
@@ -70,7 +68,7 @@ module Chewy
70
68
  end
71
69
 
72
70
  initializer 'chewy.request_strategy' do |app|
73
- app.config.middleware.insert_after(Rails::Rack::Logger, RequestStrategy)
71
+ app.config.middleware.insert_before(ActionDispatch::ShowExceptions, RequestStrategy)
74
72
  end
75
73
 
76
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
 
@@ -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