chewy 0.8.4 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +34 -0
  3. data/.rubocop_todo.yml +44 -0
  4. data/.travis.yml +20 -60
  5. data/Appraisals +15 -40
  6. data/CHANGELOG.md +42 -0
  7. data/Gemfile +1 -0
  8. data/Guardfile +5 -5
  9. data/README.md +155 -6
  10. data/Rakefile +11 -1
  11. data/chewy.gemspec +5 -7
  12. data/gemfiles/rails.3.2.activerecord.gemfile +1 -0
  13. data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +1 -0
  14. data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +1 -0
  15. data/gemfiles/rails.4.2.activerecord.gemfile +1 -0
  16. data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +1 -0
  17. data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +1 -0
  18. data/gemfiles/{rails.4.2.mongoid.4.0.0.gemfile → rails.4.2.mongoid.4.0.gemfile} +2 -1
  19. data/gemfiles/{rails.4.2.mongoid.4.0.0.kaminari.gemfile → rails.4.2.mongoid.4.0.kaminari.gemfile} +2 -1
  20. data/gemfiles/{rails.4.2.mongoid.4.0.0.will_paginate.gemfile → rails.4.2.mongoid.4.0.will_paginate.gemfile} +2 -1
  21. data/gemfiles/{rails.4.2.mongoid.5.1.0.gemfile → rails.4.2.mongoid.5.1.gemfile} +2 -1
  22. data/gemfiles/{rails.4.2.mongoid.5.1.0.kaminari.gemfile → rails.4.2.mongoid.5.1.kaminari.gemfile} +2 -1
  23. data/gemfiles/{rails.4.2.mongoid.5.1.0.will_paginate.gemfile → rails.4.2.mongoid.5.1.will_paginate.gemfile} +2 -1
  24. data/gemfiles/{rails.4.0.activerecord.gemfile → rails.5.0.activerecord.gemfile} +4 -2
  25. data/gemfiles/{rails.4.0.mongoid.4.0.0.kaminari.gemfile → rails.5.0.activerecord.kaminari.gemfile} +4 -2
  26. data/gemfiles/{rails.4.0.mongoid.4.0.0.will_paginate.gemfile → rails.5.0.activerecord.will_paginate.gemfile} +4 -2
  27. data/gemfiles/{sequel.4.31.gemfile → sequel.4.38.gemfile} +3 -2
  28. data/lib/chewy.rb +24 -16
  29. data/lib/chewy/backports/deep_dup.rb +1 -1
  30. data/lib/chewy/backports/duplicable.rb +1 -0
  31. data/lib/chewy/config.rb +13 -7
  32. data/lib/chewy/errors.rb +4 -4
  33. data/lib/chewy/fields/base.rb +19 -14
  34. data/lib/chewy/fields/root.rb +11 -9
  35. data/lib/chewy/index.rb +38 -25
  36. data/lib/chewy/index/actions.rb +17 -17
  37. data/lib/chewy/index/settings.rb +3 -4
  38. data/lib/chewy/journal.rb +107 -0
  39. data/lib/chewy/journal/apply.rb +31 -0
  40. data/lib/chewy/journal/clean.rb +24 -0
  41. data/lib/chewy/journal/entry.rb +83 -0
  42. data/lib/chewy/journal/query.rb +87 -0
  43. data/lib/chewy/log_subscriber.rb +8 -8
  44. data/lib/chewy/minitest.rb +1 -0
  45. data/lib/chewy/minitest/helpers.rb +77 -0
  46. data/lib/chewy/minitest/search_index_receiver.rb +80 -0
  47. data/lib/chewy/query.rb +116 -60
  48. data/lib/chewy/query/compose.rb +5 -6
  49. data/lib/chewy/query/criteria.rb +26 -16
  50. data/lib/chewy/query/filters.rb +9 -9
  51. data/lib/chewy/query/loading.rb +2 -2
  52. data/lib/chewy/query/nodes/and.rb +3 -3
  53. data/lib/chewy/query/nodes/base.rb +1 -1
  54. data/lib/chewy/query/nodes/bool.rb +6 -4
  55. data/lib/chewy/query/nodes/equal.rb +6 -6
  56. data/lib/chewy/query/nodes/exists.rb +2 -2
  57. data/lib/chewy/query/nodes/expr.rb +2 -2
  58. data/lib/chewy/query/nodes/field.rb +35 -31
  59. data/lib/chewy/query/nodes/has_child.rb +1 -0
  60. data/lib/chewy/query/nodes/has_parent.rb +1 -0
  61. data/lib/chewy/query/nodes/has_relation.rb +11 -13
  62. data/lib/chewy/query/nodes/match_all.rb +1 -1
  63. data/lib/chewy/query/nodes/missing.rb +2 -2
  64. data/lib/chewy/query/nodes/not.rb +3 -3
  65. data/lib/chewy/query/nodes/or.rb +3 -3
  66. data/lib/chewy/query/nodes/prefix.rb +4 -3
  67. data/lib/chewy/query/nodes/query.rb +3 -3
  68. data/lib/chewy/query/nodes/range.rb +11 -11
  69. data/lib/chewy/query/nodes/raw.rb +1 -1
  70. data/lib/chewy/query/nodes/regexp.rb +15 -11
  71. data/lib/chewy/query/nodes/script.rb +6 -6
  72. data/lib/chewy/query/pagination/will_paginate.rb +2 -2
  73. data/lib/chewy/railtie.rb +3 -3
  74. data/lib/chewy/rake_helper.rb +51 -30
  75. data/lib/chewy/repository.rb +2 -2
  76. data/lib/chewy/rspec.rb +1 -1
  77. data/lib/chewy/rspec/update_index.rb +46 -47
  78. data/lib/chewy/runtime/version.rb +4 -4
  79. data/lib/chewy/search.rb +7 -5
  80. data/lib/chewy/strategy.rb +10 -8
  81. data/lib/chewy/strategy/atomic.rb +2 -2
  82. data/lib/chewy/strategy/base.rb +4 -4
  83. data/lib/chewy/strategy/bypass.rb +1 -2
  84. data/lib/chewy/strategy/sidekiq.rb +2 -0
  85. data/lib/chewy/strategy/urgent.rb +1 -1
  86. data/lib/chewy/type.rb +51 -45
  87. data/lib/chewy/type/adapter/active_record.rb +23 -12
  88. data/lib/chewy/type/adapter/base.rb +4 -4
  89. data/lib/chewy/type/adapter/mongoid.rb +6 -6
  90. data/lib/chewy/type/adapter/object.rb +15 -12
  91. data/lib/chewy/type/adapter/orm.rb +24 -15
  92. data/lib/chewy/type/adapter/sequel.rb +11 -7
  93. data/lib/chewy/type/crutch.rb +4 -3
  94. data/lib/chewy/type/import.rb +51 -32
  95. data/lib/chewy/type/mapping.rb +17 -17
  96. data/lib/chewy/type/observe.rb +9 -7
  97. data/lib/chewy/type/witchcraft.rb +62 -23
  98. data/lib/chewy/type/wrapper.rb +20 -14
  99. data/lib/chewy/version.rb +1 -1
  100. data/lib/generators/chewy/install_generator.rb +3 -3
  101. data/lib/tasks/chewy.rake +28 -23
  102. data/spec/chewy/config_spec.rb +33 -12
  103. data/spec/chewy/fields/base_spec.rb +143 -154
  104. data/spec/chewy/fields/root_spec.rb +22 -20
  105. data/spec/chewy/fields/time_fields_spec.rb +11 -9
  106. data/spec/chewy/index/actions_spec.rb +27 -4
  107. data/spec/chewy/index/aliases_spec.rb +2 -2
  108. data/spec/chewy/index/settings_spec.rb +72 -50
  109. data/spec/chewy/index_spec.rb +95 -43
  110. data/spec/chewy/journal/apply_spec.rb +120 -0
  111. data/spec/chewy/journal/entry_spec.rb +237 -0
  112. data/spec/chewy/journal_spec.rb +173 -0
  113. data/spec/chewy/minitest/helpers_spec.rb +90 -0
  114. data/spec/chewy/minitest/search_index_receiver_spec.rb +120 -0
  115. data/spec/chewy/query/criteria_spec.rb +504 -237
  116. data/spec/chewy/query/filters_spec.rb +94 -66
  117. data/spec/chewy/query/loading_spec.rb +76 -40
  118. data/spec/chewy/query/nodes/and_spec.rb +3 -7
  119. data/spec/chewy/query/nodes/bool_spec.rb +5 -13
  120. data/spec/chewy/query/nodes/equal_spec.rb +20 -20
  121. data/spec/chewy/query/nodes/exists_spec.rb +7 -7
  122. data/spec/chewy/query/nodes/has_child_spec.rb +42 -23
  123. data/spec/chewy/query/nodes/has_parent_spec.rb +42 -23
  124. data/spec/chewy/query/nodes/match_all_spec.rb +2 -2
  125. data/spec/chewy/query/nodes/missing_spec.rb +6 -5
  126. data/spec/chewy/query/nodes/not_spec.rb +3 -7
  127. data/spec/chewy/query/nodes/or_spec.rb +3 -7
  128. data/spec/chewy/query/nodes/prefix_spec.rb +6 -6
  129. data/spec/chewy/query/nodes/query_spec.rb +3 -3
  130. data/spec/chewy/query/nodes/range_spec.rb +19 -19
  131. data/spec/chewy/query/nodes/raw_spec.rb +2 -2
  132. data/spec/chewy/query/nodes/regexp_spec.rb +31 -19
  133. data/spec/chewy/query/nodes/script_spec.rb +5 -5
  134. data/spec/chewy/query/pagination/kaminari_spec.rb +2 -2
  135. data/spec/chewy/query/pagination/will_paginage_spec.rb +6 -7
  136. data/spec/chewy/query/pagination_spec.rb +2 -3
  137. data/spec/chewy/query_spec.rb +208 -145
  138. data/spec/chewy/repository_spec.rb +8 -8
  139. data/spec/chewy/rspec/update_index_spec.rb +180 -111
  140. data/spec/chewy/search_spec.rb +8 -8
  141. data/spec/chewy/strategy/active_job_spec.rb +2 -2
  142. data/spec/chewy/strategy/atomic_spec.rb +4 -1
  143. data/spec/chewy/strategy/resque_spec.rb +2 -2
  144. data/spec/chewy/strategy/sidekiq_spec.rb +2 -2
  145. data/spec/chewy/type/actions_spec.rb +1 -1
  146. data/spec/chewy/type/adapter/active_record_spec.rb +255 -149
  147. data/spec/chewy/type/adapter/mongoid_spec.rb +169 -108
  148. data/spec/chewy/type/adapter/object_spec.rb +56 -40
  149. data/spec/chewy/type/adapter/sequel_spec.rb +248 -163
  150. data/spec/chewy/type/import_spec.rb +78 -47
  151. data/spec/chewy/type/mapping_spec.rb +6 -6
  152. data/spec/chewy/type/observe_spec.rb +20 -14
  153. data/spec/chewy/type/witchcraft_spec.rb +89 -43
  154. data/spec/chewy/type_spec.rb +4 -3
  155. data/spec/chewy_spec.rb +10 -8
  156. data/spec/spec_helper.rb +3 -0
  157. data/spec/support/active_record.rb +1 -1
  158. data/spec/support/class_helpers.rb +10 -11
  159. data/spec/support/mongoid.rb +2 -2
  160. data/spec/support/sequel.rb +1 -1
  161. metadata +65 -35
  162. data/gemfiles/rails.4.0.activerecord.kaminari.gemfile +0 -14
  163. data/gemfiles/rails.4.0.activerecord.will_paginate.gemfile +0 -14
  164. data/gemfiles/rails.4.0.mongoid.4.0.0.gemfile +0 -15
  165. data/gemfiles/rails.4.0.mongoid.5.1.0.gemfile +0 -15
  166. data/gemfiles/rails.4.0.mongoid.5.1.0.kaminari.gemfile +0 -14
  167. data/gemfiles/rails.4.0.mongoid.5.1.0.will_paginate.gemfile +0 -14
  168. data/gemfiles/rails.4.1.activerecord.gemfile +0 -15
  169. data/gemfiles/rails.4.1.activerecord.kaminari.gemfile +0 -14
  170. data/gemfiles/rails.4.1.activerecord.will_paginate.gemfile +0 -14
  171. data/gemfiles/rails.4.1.mongoid.4.0.0.gemfile +0 -15
  172. data/gemfiles/rails.4.1.mongoid.4.0.0.kaminari.gemfile +0 -14
  173. data/gemfiles/rails.4.1.mongoid.4.0.0.will_paginate.gemfile +0 -14
  174. data/gemfiles/rails.4.1.mongoid.5.1.0.gemfile +0 -15
  175. data/gemfiles/rails.4.1.mongoid.5.1.0.kaminari.gemfile +0 -14
  176. data/gemfiles/rails.4.1.mongoid.5.1.0.will_paginate.gemfile +0 -14
  177. data/gemfiles/rails.5.0.0.beta3.activerecord.gemfile +0 -16
  178. data/gemfiles/rails.5.0.0.beta3.activerecord.kaminari.gemfile +0 -16
  179. data/gemfiles/rails.5.0.0.beta3.activerecord.will_paginate.gemfile +0 -15
@@ -1,10 +1,9 @@
1
1
  module Chewy
2
2
  class Query
3
3
  module Compose
4
-
5
4
  protected
6
5
 
7
- def _filtered_query query, filter, options = {}
6
+ def _filtered_query(query, filter, options = {})
8
7
  query = { match_all: {} } if !query.present? && filter.present?
9
8
 
10
9
  if filter.present?
@@ -18,16 +17,16 @@ module Chewy
18
17
  filter: filter
19
18
  } } }
20
19
  end
21
- filtered[:query][:filtered].merge!(strategy: options[:strategy].to_s) if options[:strategy].present?
20
+ filtered[:query][:filtered][:strategy] = options[:strategy].to_s if options[:strategy].present?
22
21
  filtered
23
22
  elsif query.present?
24
23
  { query: query }
25
24
  else
26
- { }
25
+ {}
27
26
  end
28
27
  end
29
28
 
30
- def _queries_join queries, logic
29
+ def _queries_join(queries, logic)
31
30
  queries = queries.compact
32
31
 
33
32
  if queries.many? || (queries.present? && logic == :must_not)
@@ -48,7 +47,7 @@ module Chewy
48
47
  end
49
48
  end
50
49
 
51
- def _filters_join filters, logic
50
+ def _filters_join(filters, logic)
52
51
  filters = filters.compact
53
52
 
54
53
  if filters.many? || (filters.present? && logic == :must_not)
@@ -4,11 +4,11 @@ module Chewy
4
4
  class Query
5
5
  class Criteria
6
6
  include Compose
7
- ARRAY_STORAGES = [:queries, :filters, :post_filters, :sort, :fields, :types, :scores]
8
- HASH_STORAGES = [:options, :request_options, :facets, :aggregations, :suggest, :script_fields]
7
+ ARRAY_STORAGES = [:queries, :filters, :post_filters, :sort, :fields, :types, :scores].freeze
8
+ HASH_STORAGES = [:options, :search_options, :request_options, :facets, :aggregations, :suggest, :script_fields].freeze
9
9
  STORAGES = ARRAY_STORAGES + HASH_STORAGES
10
10
 
11
- def initialize options = {}
11
+ def initialize(options = {})
12
12
  @options = options.merge(
13
13
  query_mode: Chewy.query_mode,
14
14
  filter_mode: Chewy.filter_mode,
@@ -16,7 +16,7 @@ module Chewy
16
16
  )
17
17
  end
18
18
 
19
- def == other
19
+ def ==(other)
20
20
  other.is_a?(self.class) && storages == other.storages
21
21
  end
22
22
 
@@ -32,7 +32,7 @@ module Chewy
32
32
 
33
33
  STORAGES.each do |storage|
34
34
  define_method "#{storage}?" do
35
- send(storage).present?
35
+ send(storage).any?
36
36
  end
37
37
  end
38
38
 
@@ -48,6 +48,10 @@ module Chewy
48
48
  request_options.merge!(modifier)
49
49
  end
50
50
 
51
+ def update_search_options(modifier)
52
+ search_options.merge!(modifier)
53
+ end
54
+
51
55
  def update_facets(modifier)
52
56
  facets.merge!(modifier)
53
57
  end
@@ -79,7 +83,7 @@ module Chewy
79
83
  def update_sort(modifier, options = {})
80
84
  @sort = nil if options[:purge]
81
85
  modifier = Array.wrap(modifier).flatten.map do |element|
82
- element.is_a?(Hash) ? element.map { |k, v| {k => v} } : element
86
+ element.is_a?(Hash) ? element.map { |k, v| { k => v } } : element
83
87
  end.flatten
84
88
  @sort = sort + modifier
85
89
  end
@@ -93,14 +97,14 @@ module Chewy
93
97
  end
94
98
  end
95
99
 
96
- def merge! other
100
+ def merge!(other)
97
101
  STORAGES.each do |storage|
98
102
  send("update_#{storage}", other.send(storage))
99
103
  end
100
104
  self
101
105
  end
102
106
 
103
- def merge other
107
+ def merge(other)
104
108
  clone.merge!(other)
105
109
  end
106
110
 
@@ -110,17 +114,17 @@ module Chewy
110
114
  if options[:simple]
111
115
  { body: body.presence || { query: { match_all: {} } } }
112
116
  else
113
- body.merge!(post_filter: _request_post_filter) if post_filters?
114
- body.merge!(facets: facets) if facets?
115
- body.merge!(aggregations: aggregations) if aggregations?
116
- body.merge!(suggest: suggest) if suggest?
117
- body.merge!(sort: sort) if sort?
118
- body.merge!(_source: fields) if fields?
119
- body.merge!(script_fields: script_fields) if script_fields?
117
+ body[:post_filter] = _request_post_filter if post_filters?
118
+ body[:facets] = facets if facets?
119
+ body[:aggregations] = aggregations if aggregations?
120
+ body[:suggest] = suggest if suggest?
121
+ body[:sort] = sort if sort?
122
+ body[:_source] = fields if fields?
123
+ body[:script_fields] = script_fields if script_fields?
120
124
 
121
125
  body = _boost_query(body)
122
126
 
123
- { body: body.merge!(request_options) }
127
+ { body: body.merge!(_request_options) }.merge!(search_options)
124
128
  end
125
129
  end
126
130
 
@@ -154,6 +158,12 @@ module Chewy
154
158
  body.tap { |b| b[:query] = { function_score: score } }
155
159
  end
156
160
 
161
+ def _request_options
162
+ Hash[request_options.map do |key, value|
163
+ [key, value.is_a?(Proc) ? value.call : value]
164
+ end]
165
+ end
166
+
157
167
  def _request_query
158
168
  _queries_join(queries, options[:query_mode])
159
169
  end
@@ -28,7 +28,7 @@ module Chewy
28
28
  #
29
29
  #
30
30
  class Filters
31
- def initialize outer = nil, &block
31
+ def initialize(outer = nil, &block)
32
32
  @block = block
33
33
  @outer = outer || eval('self', block.binding)
34
34
  end
@@ -42,7 +42,7 @@ module Chewy
42
42
  #
43
43
  # UsersIndex.filter{ name == o{ name } } # => {filter: {term: {name: 'Friend'}}}
44
44
  #
45
- def o &block
45
+ def o(&block)
46
46
  @outer.instance_exec(&block)
47
47
  end
48
48
 
@@ -62,7 +62,7 @@ module Chewy
62
62
  #
63
63
  # UsersIndex.filter{ f{ field } == 'Name' } == UsersIndex.filter{ name == 'Name' } # => true
64
64
  #
65
- def f name = nil, *args, &block
65
+ def f(name = nil, *args, &block)
66
66
  name = block ? o(&block) : name
67
67
  Nodes::Field.new name, *args
68
68
  end
@@ -82,7 +82,7 @@ module Chewy
82
82
  # UsersIndex.filter{ s{ script } } == UsersIndex.filter{ s('doc["num1"].value > 1') } # => true
83
83
  # UsersIndex.filter{ s(param1: 42) { script } } == UsersIndex.filter{ s('doc["num1"].value > 1', param1: 42) } # => true
84
84
  #
85
- def s *args, &block
85
+ def s(*args, &block)
86
86
  params = args.extract_options!
87
87
  script = block ? o(&block) : args.first
88
88
  Nodes::Script.new script, params
@@ -100,7 +100,7 @@ module Chewy
100
100
  #
101
101
  # UsersIndex.filter{ q{ query } } == UsersIndex.filter{ q(query_string: {query: 'name: hello'}) } # => true
102
102
  #
103
- def q query = nil, &block
103
+ def q(query = nil, &block)
104
104
  Nodes::Query.new block ? o(&block) : query
105
105
  end
106
106
 
@@ -119,7 +119,7 @@ module Chewy
119
119
  # UsersIndex.filter{ r{ filter } } == UsersIndex.filter{ r(term: {name: 'Name'}) } # => true
120
120
  # UsersIndex.filter{ r{ filter } } == UsersIndex.filter(term: {name: 'Name'}) # => true
121
121
  #
122
- def r raw = nil, &block
122
+ def r(raw = nil, &block)
123
123
  Nodes::Raw.new block ? o(&block) : raw
124
124
  end
125
125
 
@@ -155,7 +155,7 @@ module Chewy
155
155
  # .filter_mode(:or)
156
156
  # end
157
157
  #
158
- def has_child type
158
+ def has_child(type) # rubocop:disable Style/PredicateName
159
159
  Nodes::HasChild.new(type, @outer)
160
160
  end
161
161
 
@@ -177,7 +177,7 @@ module Chewy
177
177
  # .filter_mode(:or)
178
178
  # end
179
179
  #
180
- def has_parent type
180
+ def has_parent(type) # rubocop:disable Style/PredicateName
181
181
  Nodes::HasParent.new(type, @outer)
182
182
  end
183
183
 
@@ -200,7 +200,7 @@ module Chewy
200
200
  # UsersIndex.filter{ article.title =~ 'Hello' }
201
201
  # UsersIndex.filter{ article.tags? }
202
202
  #
203
- def method_missing method, *args, &block
203
+ def method_missing(method, *args) # rubocop:disable Style/MethodMissing
204
204
  method = method.to_s
205
205
  if method =~ /\?\Z/
206
206
  Nodes::Exists.new method.gsub(/\?\Z/, '')
@@ -96,8 +96,8 @@ module Chewy
96
96
  next if only.present? && !only.include?(type.type_name)
97
97
 
98
98
  loaded = type.adapter.load(objects, options.merge(_type: type))
99
- [type, loaded.index_by.with_index do |loaded, i|
100
- objects[i]._object = loaded
99
+ [type, loaded.index_by.with_index do |loaded_object, i|
100
+ objects[i]._object = loaded_object
101
101
  objects[i]
102
102
  end]
103
103
  end.compact]
@@ -2,7 +2,7 @@ module Chewy
2
2
  class Query
3
3
  module Nodes
4
4
  class And < Expr
5
- def initialize *nodes
5
+ def initialize(*nodes)
6
6
  @options = nodes.extract_options!
7
7
  @nodes = nodes.flatten.map { |node| node.is_a?(self.class) ? node.__nodes__ : node }.flatten
8
8
  end
@@ -14,9 +14,9 @@ module Chewy
14
14
  def __render__
15
15
  nodes = @nodes.map(&:__render__)
16
16
  if @options.key?(:cache)
17
- {and: {filters: nodes, _cache: !!@options[:cache]}}
17
+ { and: { filters: nodes, _cache: !!@options[:cache] } }
18
18
  else
19
- {and: nodes}
19
+ { and: nodes }
20
20
  end
21
21
  end
22
22
  end
@@ -6,7 +6,7 @@ module Chewy
6
6
  raise NotImplementedError
7
7
  end
8
8
 
9
- def eql? other
9
+ def eql?(other)
10
10
  other.is_a?(self.class) && instance_variables.all? do |ivar|
11
11
  instance_variable_get(ivar).eql? other.instance_variable_get(ivar)
12
12
  end
@@ -2,16 +2,18 @@ module Chewy
2
2
  class Query
3
3
  module Nodes
4
4
  class Bool < Expr
5
- METHODS = %w(must must_not should)
5
+ METHODS = %w(must must_not should).freeze
6
6
 
7
- def initialize options = {}
7
+ def initialize(options = {})
8
8
  @options = options
9
- @must, @must_not, @should = [], [], []
9
+ @must = []
10
+ @must_not = []
11
+ @should = []
10
12
  end
11
13
 
12
14
  METHODS.each do |method|
13
15
  define_method method do |*exprs|
14
- instance_variable_get("@#{method}").push(*exprs)
16
+ instance_variable_get("@#{method}").concat(exprs)
15
17
  self
16
18
  end
17
19
  end
@@ -10,10 +10,10 @@ module Chewy
10
10
  :b => :bool,
11
11
  :bool => :bool,
12
12
  :f => :fielddata,
13
- :fielddata => :fielddata,
14
- }
13
+ :fielddata => :fielddata
14
+ }.freeze
15
15
 
16
- def initialize name, value, *args
16
+ def initialize(name, value, *args)
17
17
  @name = name.to_s
18
18
  @value = value
19
19
  @options = args.extract_options!
@@ -23,10 +23,10 @@ module Chewy
23
23
 
24
24
  def __render__
25
25
  filter = (@value.is_a?(Array) ? :terms : :term)
26
- body = {@name => @value}
26
+ body = { @name => @value }
27
27
  body.merge!(@options.slice(:execution)) if filter == :terms
28
- body.merge!(_cache: !!@options[:cache]) if @options.key?(:cache)
29
- {filter => body}
28
+ body[:_cache] = !!@options[:cache] if @options.key?(:cache)
29
+ { filter => body }
30
30
  end
31
31
  end
32
32
  end
@@ -2,7 +2,7 @@ module Chewy
2
2
  class Query
3
3
  module Nodes
4
4
  class Exists < Expr
5
- def initialize name, options = {}
5
+ def initialize(name, options = {})
6
6
  @name = name.to_s
7
7
  @options = options
8
8
  end
@@ -12,7 +12,7 @@ module Chewy
12
12
  end
13
13
 
14
14
  def __render__
15
- {exists: {field: @name}}
15
+ { exists: { field: @name } }
16
16
  end
17
17
  end
18
18
  end
@@ -2,11 +2,11 @@ module Chewy
2
2
  class Query
3
3
  module Nodes
4
4
  class Expr < Base
5
- def & other
5
+ def &(other)
6
6
  Nodes::And.new self, other
7
7
  end
8
8
 
9
- def | other
9
+ def |(other)
10
10
  Nodes::Or.new self, other
11
11
  end
12
12
 
@@ -2,7 +2,7 @@ module Chewy
2
2
  class Query
3
3
  module Nodes
4
4
  class Field < Base
5
- def initialize name, *args
5
+ def initialize(name, *args)
6
6
  @name = name.to_s
7
7
  @args = args
8
8
  end
@@ -16,70 +16,74 @@ module Chewy
16
16
  self
17
17
  end
18
18
 
19
- def > value
20
- Nodes::Range.new @name, *__options_merge__(gt: value)
19
+ def >(other)
20
+ Nodes::Range.new @name, *__options_merge__(gt: other)
21
21
  end
22
22
 
23
- def < value
24
- Nodes::Range.new @name, *__options_merge__(lt: value)
23
+ def <(other)
24
+ Nodes::Range.new @name, *__options_merge__(lt: other)
25
25
  end
26
26
 
27
- def >= value
28
- Nodes::Range.new @name, *__options_merge__(gt: value, left_closed: true)
27
+ def >=(other)
28
+ Nodes::Range.new @name, *__options_merge__(gt: other, left_closed: true)
29
29
  end
30
30
 
31
- def <= value
32
- Nodes::Range.new @name, *__options_merge__(lt: value, right_closed: true)
31
+ def <=(other)
32
+ Nodes::Range.new @name, *__options_merge__(lt: other, right_closed: true)
33
33
  end
34
34
 
35
- def == value
36
- case value
35
+ def ==(other)
36
+ case other
37
37
  when nil
38
- Nodes::Missing.new @name, existence: false, null_value: true
38
+ nil?
39
39
  when ::Regexp
40
- Nodes::Regexp.new @name, value, *@args
40
+ Nodes::Regexp.new @name, other, *@args
41
41
  when ::Range
42
- Nodes::Range.new @name, *__options_merge__(gt: value.first, lt: value.last)
42
+ Nodes::Range.new @name, *__options_merge__(gt: other.first, lt: other.last)
43
43
  else
44
- if value.is_a?(Array) && value.first.is_a?(::Range)
44
+ if other.is_a?(Array) && other.first.is_a?(::Range)
45
45
  Nodes::Range.new @name, *__options_merge__(
46
- gt: value.first.first, lt: value.first.last,
46
+ gt: other.first.first, lt: other.first.last,
47
47
  left_closed: true, right_closed: true
48
48
  )
49
49
  else
50
- Nodes::Equal.new @name, value, *@args
50
+ Nodes::Equal.new @name, other, *@args
51
51
  end
52
52
  end
53
53
  end
54
54
 
55
- def != value
56
- case value
55
+ def !=(other)
56
+ case other
57
57
  when nil
58
58
  Nodes::Exists.new @name
59
59
  else
60
- Nodes::Not.new self == value
60
+ Nodes::Not.new self == other
61
61
  end
62
62
  end
63
63
 
64
- def =~ value
65
- case value
64
+ def =~(other)
65
+ case other
66
66
  when ::Regexp
67
- Nodes::Regexp.new @name, value, *@args
67
+ Nodes::Regexp.new @name, other, *@args
68
68
  else
69
- Nodes::Prefix.new @name, value, @args.extract_options!
69
+ Nodes::Prefix.new @name, other, @args.extract_options!
70
70
  end
71
71
  end
72
72
 
73
- def !~ value
74
- Not.new(self =~ value)
73
+ def !~(other)
74
+ Not.new(self =~ other)
75
75
  end
76
76
 
77
- def method_missing method, *args, &block
77
+ def nil?
78
+ Nodes::Missing.new @name, existence: false, null_value: true
79
+ end
80
+
81
+ def method_missing(method, *args) # rubocop:disable Style/MethodMissing
78
82
  method = method.to_s
79
83
  if method =~ /\?\Z/
80
- Nodes::Exists.new [@name, method.gsub(/\?\Z/, '')].join(?.)
84
+ Nodes::Exists.new [@name, method.gsub(/\?\Z/, '')].join('.')
81
85
  else
82
- self.class.new [@name, method].join(?.), *args
86
+ self.class.new [@name, method].join('.'), *args
83
87
  end
84
88
  end
85
89
 
@@ -89,13 +93,13 @@ module Chewy
89
93
 
90
94
  private
91
95
 
92
- def __options_merge__! additional = {}
96
+ def __options_merge__!(additional = {})
93
97
  options = @args.extract_options!
94
98
  options = options.merge(additional)
95
99
  @args.push(options)
96
100
  end
97
101
 
98
- def __options_merge__ additional = {}
102
+ def __options_merge__(additional = {})
99
103
  options = @args.extract_options!
100
104
  options = options.merge(additional)
101
105
  @args + [options]