chewy 5.0.0 → 7.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (147) hide show
  1. checksums.yaml +4 -4
  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/.github/workflows/ruby.yml +94 -0
  7. data/Appraisals +1 -17
  8. data/CHANGELOG.md +308 -356
  9. data/CODE_OF_CONDUCT.md +14 -0
  10. data/CONTRIBUTING.md +63 -0
  11. data/Gemfile +2 -0
  12. data/LICENSE.txt +1 -1
  13. data/README.md +71 -55
  14. data/chewy.gemspec +5 -5
  15. data/gemfiles/rails.5.2.activerecord.gemfile +4 -3
  16. data/gemfiles/{rails.5.0.activerecord.gemfile → rails.5.2.mongoid.6.4.gemfile} +4 -3
  17. data/gemfiles/{rails.5.0.mongoid.6.1.gemfile → rails.6.0.activerecord.gemfile} +4 -3
  18. data/gemfiles/{rails.5.1.activerecord.gemfile → rails.6.1.activerecord.gemfile} +6 -3
  19. data/lib/chewy.rb +1 -1
  20. data/lib/chewy/backports/duplicable.rb +1 -1
  21. data/lib/chewy/config.rb +2 -20
  22. data/lib/chewy/fields/base.rb +1 -7
  23. data/lib/chewy/fields/root.rb +2 -2
  24. data/lib/chewy/index.rb +2 -0
  25. data/lib/chewy/index/actions.rb +15 -5
  26. data/lib/chewy/index/aliases.rb +16 -5
  27. data/lib/chewy/multi_search.rb +62 -0
  28. data/lib/chewy/railtie.rb +1 -1
  29. data/lib/chewy/search.rb +2 -9
  30. data/lib/chewy/search/loader.rb +1 -1
  31. data/lib/chewy/search/pagination/will_paginate.rb +1 -1
  32. data/lib/chewy/search/parameters.rb +24 -6
  33. data/lib/chewy/search/parameters/allow_partial_search_results.rb +27 -0
  34. data/lib/chewy/search/parameters/indices.rb +123 -0
  35. data/lib/chewy/search/parameters/none.rb +1 -3
  36. data/lib/chewy/search/request.rb +101 -74
  37. data/lib/chewy/search/scrolling.rb +7 -6
  38. data/lib/chewy/stash.rb +1 -1
  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 +4 -2
  48. data/lib/chewy/type/import/journal_builder.rb +1 -1
  49. data/lib/chewy/type/import/routine.rb +1 -1
  50. data/lib/chewy/type/mapping.rb +4 -4
  51. data/lib/chewy/type/observe.rb +3 -3
  52. data/lib/chewy/type/syncer.rb +4 -5
  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/migration_guide.md +56 -0
  57. data/spec/chewy/config_spec.rb +1 -22
  58. data/spec/chewy/fields/base_spec.rb +11 -9
  59. data/spec/chewy/index/actions_spec.rb +120 -33
  60. data/spec/chewy/index/aliases_spec.rb +3 -3
  61. data/spec/chewy/index_spec.rb +16 -39
  62. data/spec/chewy/journal_spec.rb +22 -18
  63. data/spec/chewy/minitest/search_index_receiver_spec.rb +11 -9
  64. data/spec/chewy/multi_search_spec.rb +85 -0
  65. data/spec/chewy/rake_helper_spec.rb +102 -87
  66. data/spec/chewy/rspec/update_index_spec.rb +47 -46
  67. data/spec/chewy/runtime_spec.rb +2 -2
  68. data/spec/chewy/search/parameters/indices_spec.rb +190 -0
  69. data/spec/chewy/search/parameters/none_spec.rb +1 -1
  70. data/spec/chewy/search/parameters_spec.rb +21 -4
  71. data/spec/chewy/search/request_spec.rb +103 -70
  72. data/spec/chewy/search/response_spec.rb +27 -17
  73. data/spec/chewy/search/scrolling_spec.rb +27 -17
  74. data/spec/chewy/search_spec.rb +45 -41
  75. data/spec/chewy/stash_spec.rb +14 -12
  76. data/spec/chewy/strategy/active_job_spec.rb +15 -2
  77. data/spec/chewy/strategy/shoryuken_spec.rb +6 -2
  78. data/spec/chewy/strategy/sidekiq_spec.rb +6 -2
  79. data/spec/chewy/type/adapter/active_record_spec.rb +16 -4
  80. data/spec/chewy/type/import/bulk_builder_spec.rb +9 -94
  81. data/spec/chewy/type/import/journal_builder_spec.rb +9 -7
  82. data/spec/chewy/type/import_spec.rb +9 -0
  83. data/spec/chewy/type/mapping_spec.rb +3 -1
  84. data/spec/chewy/type/observe_spec.rb +4 -4
  85. data/spec/chewy/type/witchcraft_spec.rb +15 -0
  86. data/spec/chewy/type/wrapper_spec.rb +3 -1
  87. data/spec/chewy_spec.rb +0 -7
  88. data/spec/spec_helper.rb +4 -8
  89. data/spec/support/active_record.rb +21 -0
  90. metadata +32 -97
  91. data/.travis.yml +0 -45
  92. data/LEGACY_DSL.md +0 -497
  93. data/gemfiles/rails.4.0.activerecord.gemfile +0 -15
  94. data/gemfiles/rails.4.1.activerecord.gemfile +0 -15
  95. data/gemfiles/rails.4.2.activerecord.gemfile +0 -16
  96. data/gemfiles/rails.4.2.mongoid.5.2.gemfile +0 -16
  97. data/gemfiles/rails.5.1.mongoid.6.3.gemfile +0 -16
  98. data/lib/chewy/query.rb +0 -1137
  99. data/lib/chewy/query/compose.rb +0 -68
  100. data/lib/chewy/query/criteria.rb +0 -191
  101. data/lib/chewy/query/filters.rb +0 -244
  102. data/lib/chewy/query/loading.rb +0 -110
  103. data/lib/chewy/query/nodes/and.rb +0 -25
  104. data/lib/chewy/query/nodes/base.rb +0 -17
  105. data/lib/chewy/query/nodes/bool.rb +0 -34
  106. data/lib/chewy/query/nodes/equal.rb +0 -34
  107. data/lib/chewy/query/nodes/exists.rb +0 -20
  108. data/lib/chewy/query/nodes/expr.rb +0 -28
  109. data/lib/chewy/query/nodes/field.rb +0 -110
  110. data/lib/chewy/query/nodes/has_child.rb +0 -15
  111. data/lib/chewy/query/nodes/has_parent.rb +0 -15
  112. data/lib/chewy/query/nodes/has_relation.rb +0 -59
  113. data/lib/chewy/query/nodes/match_all.rb +0 -11
  114. data/lib/chewy/query/nodes/missing.rb +0 -20
  115. data/lib/chewy/query/nodes/not.rb +0 -25
  116. data/lib/chewy/query/nodes/or.rb +0 -25
  117. data/lib/chewy/query/nodes/prefix.rb +0 -19
  118. data/lib/chewy/query/nodes/query.rb +0 -20
  119. data/lib/chewy/query/nodes/range.rb +0 -63
  120. data/lib/chewy/query/nodes/raw.rb +0 -15
  121. data/lib/chewy/query/nodes/regexp.rb +0 -35
  122. data/lib/chewy/query/nodes/script.rb +0 -20
  123. data/lib/chewy/query/pagination.rb +0 -25
  124. data/spec/chewy/query/criteria_spec.rb +0 -700
  125. data/spec/chewy/query/filters_spec.rb +0 -201
  126. data/spec/chewy/query/loading_spec.rb +0 -124
  127. data/spec/chewy/query/nodes/and_spec.rb +0 -12
  128. data/spec/chewy/query/nodes/bool_spec.rb +0 -14
  129. data/spec/chewy/query/nodes/equal_spec.rb +0 -32
  130. data/spec/chewy/query/nodes/exists_spec.rb +0 -18
  131. data/spec/chewy/query/nodes/has_child_spec.rb +0 -59
  132. data/spec/chewy/query/nodes/has_parent_spec.rb +0 -59
  133. data/spec/chewy/query/nodes/match_all_spec.rb +0 -11
  134. data/spec/chewy/query/nodes/missing_spec.rb +0 -16
  135. data/spec/chewy/query/nodes/not_spec.rb +0 -14
  136. data/spec/chewy/query/nodes/or_spec.rb +0 -12
  137. data/spec/chewy/query/nodes/prefix_spec.rb +0 -16
  138. data/spec/chewy/query/nodes/query_spec.rb +0 -12
  139. data/spec/chewy/query/nodes/range_spec.rb +0 -32
  140. data/spec/chewy/query/nodes/raw_spec.rb +0 -11
  141. data/spec/chewy/query/nodes/regexp_spec.rb +0 -43
  142. data/spec/chewy/query/nodes/script_spec.rb +0 -15
  143. data/spec/chewy/query/pagination/kaminari_spec.rb +0 -5
  144. data/spec/chewy/query/pagination/will_paginate_spec.rb +0 -5
  145. data/spec/chewy/query/pagination_spec.rb +0 -39
  146. data/spec/chewy/query_spec.rb +0 -637
  147. data/spec/chewy/search/parameters/indices_boost_spec.rb +0 -83
data/lib/chewy.rb CHANGED
@@ -132,7 +132,7 @@ module Chewy
132
132
  def create_type(index, target, options = {}, &block)
133
133
  type = Class.new(Chewy::Type)
134
134
 
135
- adapter = adapters.find { |klass| klass.accepts?(target) }.new(target, options)
135
+ adapter = adapters.find { |klass| klass.accepts?(target) }.new(target, **options)
136
136
 
137
137
  index.const_set(adapter.name, type)
138
138
  type.send(:define_singleton_method, :index) { index }
@@ -79,7 +79,7 @@ end
79
79
  require 'bigdecimal'
80
80
  class BigDecimal
81
81
  begin
82
- BigDecimal.new('4.56').dup
82
+ BigDecimal('4.56').dup
83
83
 
84
84
  def duplicable?
85
85
  true
data/lib/chewy/config.rb CHANGED
@@ -3,18 +3,6 @@ module Chewy
3
3
  include Singleton
4
4
 
5
5
  attr_accessor :settings, :logger,
6
- # Default query compilation mode. `:must` by default.
7
- # See Chewy::Query#query_mode for details
8
- #
9
- :query_mode,
10
- # Default filters compilation mode. `:and` by default.
11
- # See Chewy::Query#filter_mode for details
12
- #
13
- :filter_mode,
14
- # Default post_filters compilation mode. `nil` by default.
15
- # See Chewy::Query#post_filter_mode for details
16
- #
17
- :post_filter_mode,
18
6
  # The first strategy in stack. `:base` by default.
19
7
  # If you need to return to the previous chewy behavior -
20
8
  # just set it to `:bypass`
@@ -46,7 +34,7 @@ module Chewy
46
34
  # Default options for root of Chewy type. Allows to set default options
47
35
  # for type mappings like `_all`.
48
36
  :default_root_options,
49
- # Default field type for any field in any Chewy type. Defaults to 'string'.
37
+ # Default field type for any field in any Chewy type. Defaults to 'text'.
50
38
  :default_field_type
51
39
 
52
40
  attr_reader :transport_logger, :transport_tracer,
@@ -59,8 +47,6 @@ module Chewy
59
47
 
60
48
  def initialize
61
49
  @settings = {}
62
- @query_mode = :must
63
- @filter_mode = :and
64
50
  @root_strategy = :base
65
51
  @request_strategy = :atomic
66
52
  @use_after_commit_callbacks = true
@@ -70,7 +56,7 @@ module Chewy
70
56
  @indices_path = 'app/chewy'
71
57
  @default_root_options = {}
72
58
  @default_field_type = 'text'.freeze
73
- self.search_class = Chewy::Search::Request
59
+ @search_class = build_search_class(Chewy::Search::Request)
74
60
  end
75
61
 
76
62
  def transport_logger=(logger)
@@ -83,10 +69,6 @@ module Chewy
83
69
  @transport_tracer = tracer
84
70
  end
85
71
 
86
- def search_class=(base)
87
- @search_class = build_search_class(base)
88
- end
89
-
90
72
  # Chewy core configurations. There is two ways to set it up:
91
73
  # use `Chewy.settings=` method or, for Rails application,
92
74
  # create `config/chewy.yml` file. Btw, `config/chewy.yml` supports
@@ -7,7 +7,7 @@ module Chewy
7
7
  def initialize(name, value: nil, **options)
8
8
  @name = name.to_sym
9
9
  @options = {}
10
- update_options!(options)
10
+ update_options!(**options)
11
11
  @value = value
12
12
  @children = []
13
13
  end
@@ -34,12 +34,6 @@ module Chewy
34
34
  mapping.reverse_merge!(options)
35
35
  mapping.reverse_merge!(type: (children.present? ? 'object' : Chewy.default_field_type))
36
36
 
37
- # This is done to support ES2 journaling and will be removed soon
38
- if mapping[:type] == 'keyword' && Chewy::Runtime.version < '5.0'
39
- mapping[:type] = 'string'
40
- mapping[:index] = 'not_analyzed'
41
- end
42
-
43
37
  {name => mapping}
44
38
  end
45
39
 
@@ -6,8 +6,8 @@ module Chewy
6
6
  attr_reader :parent
7
7
  attr_reader :parent_id
8
8
 
9
- def initialize(*)
10
- super
9
+ def initialize(name, **options)
10
+ super(name, **options)
11
11
 
12
12
  @value ||= -> { self }
13
13
  @dynamic_templates = []
data/lib/chewy/index.rb CHANGED
@@ -146,6 +146,8 @@ module Chewy
146
146
  # passed.
147
147
  #
148
148
  def define_type(target, options = {}, &block)
149
+ raise 'Multiple types are deprecated' if type_hash.present?
150
+
149
151
  type_class = Chewy.create_type(self, target, options, &block)
150
152
  self.type_hash = type_hash.merge(type_class.type_name => type_class)
151
153
  end
@@ -30,8 +30,8 @@ module Chewy
30
30
  # Suffixed index names might be used for zero-downtime mapping change, for example.
31
31
  # Description: (http://www.elasticsearch.org/blog/changing-mapping-with-zero-downtime/).
32
32
  #
33
- def create(*args)
34
- create!(*args)
33
+ def create(*args, **kwargs)
34
+ create!(*args, **kwargs)
35
35
  rescue Elasticsearch::Transport::Transport::Errors::BadRequest
36
36
  false
37
37
  end
@@ -59,7 +59,9 @@ module Chewy
59
59
 
60
60
  body = specification_hash
61
61
  body[:aliases] = {general_name => {}} if options[:alias] && suffixed_name != general_name
62
- result = client.indices.create(index: suffixed_name, body: body)
62
+ args = {index: suffixed_name, body: body}
63
+ args[:include_type_name] = true if Runtime.version >= '6.7.0'
64
+ result = client.indices.create(**args)
63
65
 
64
66
  Chewy.wait_for_status if result
65
67
  result
@@ -74,7 +76,13 @@ module Chewy
74
76
  # UsersIndex.delete '01-2014' # deletes `users_01-2014` index
75
77
  #
76
78
  def delete(suffix = nil)
77
- result = client.indices.delete index: index_name(suffix: suffix)
79
+ # Verify that the index_name is really the index_name and not an alias.
80
+ #
81
+ # "The index parameter in the delete index API no longer accepts alias names.
82
+ # Instead, it accepts only index names (or wildcards which will expand to matching indices)."
83
+ # https://www.elastic.co/guide/en/elasticsearch/reference/6.8/breaking-changes-6.0.html#_delete_index_api_resolves_indices_expressions_only_against_indices
84
+ index_names = client.indices.get_alias(index: index_name(suffix: suffix)).keys
85
+ result = client.indices.delete index: index_names.join(',')
78
86
  Chewy.wait_for_status if result
79
87
  result
80
88
  # es-ruby >= 1.0.10 handles Elasticsearch::Transport::Transport::Errors::NotFound
@@ -136,6 +144,8 @@ module Chewy
136
144
  %i[import import!].each do |method|
137
145
  class_eval <<-METHOD, __FILE__, __LINE__ + 1
138
146
  def #{method}(*args)
147
+ return true if args.first.blank? && !args.first.nil?
148
+
139
149
  options = args.extract_options!
140
150
  if args.one? && type_names.one?
141
151
  objects = {type_names.first.to_sym => args.first}
@@ -172,7 +182,7 @@ module Chewy
172
182
  def reset!(suffix = nil, apply_journal: true, journal: false, **import_options)
173
183
  result = if suffix.present?
174
184
  start_time = Time.now
175
- indexes = self.indexes
185
+ indexes = self.indexes - [index_name]
176
186
  create! suffix, alias: false
177
187
 
178
188
  general_name = index_name
@@ -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
@@ -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
@@ -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|
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
 
@@ -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
 
@@ -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,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