forked from toptal/chewy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_proxy.rb
257 lines (254 loc) · 13.4 KB
/
query_proxy.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
module Chewy
module Search
# This specialized proxy class is used to provide an ability
# of `query`, `filter`, `post_filter` parameters additional
# modification.
#
# @see Chewy::Search::Parameters::Query
# @see Chewy::Search::Parameters::Filter
# @see Chewy::Search::Parameters::PostFilter
# @see Chewy::Search::Parameters::QueryStorage
class QueryProxy
# @param parameter_name [Symbol] modified parameter name
# @param request [Chewy::Search::Request] request instance for modification
def initialize(parameter_name, request)
@parameter_name = parameter_name
@request = request
end
# @!method must(query_hash = nil, &block)
# Executes {Chewy::Search::Parameters::QueryStorage#must} in the scope
# of newly created request object.
#
# @see Chewy::Search::Parameters::QueryStorage#must
# @return [Chewy::Search::Request]
#
# @overload must(query_hash)
# If pure hash is passed it is added to `must` array of the bool query.
#
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html
# @example
# PlacesIndex.query.must(match: {name: 'Moscow'}).query.must(match: {name: 'London'})
# # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
# # :must=>[{:match=>{:name=>"Moscow"}}, {:match=>{:name=>"London"}}]}}}}>
# @param query_hash [Hash] pure query hash
#
# @overload must
# If block is passed instead of a pure hash, `elasticsearch-dsl"
# gem will be used to process it.
#
# @see https://github.com/elastic/elasticsearch-ruby/tree/master/elasticsearch-dsl
# @example
# PlacesIndex.query.must { match name: 'Moscow' }.query.must { match name: 'London' }
# # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
# # :must=>[{:match=>{:name=>"Moscow"}}, {:match=>{:name=>"London"}}]}}}}>
# @yield the block is processed by `elasticsearch-dsl` gem
#
# @!method should(query_hash = nil, &block)
# Executes {Chewy::Search::Parameters::QueryStorage#should} in the scope
# of newly created request object.
#
# @see Chewy::Search::Parameters::QueryStorage#should
# @return [Chewy::Search::Request]
#
# @overload should(query_hash)
# If pure hash is passed it is added to `should` array of the bool query.
#
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html
# @example
# PlacesIndex.query.should(match: {name: 'Moscow'}).query.should(match: {name: 'London'})
# # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
# # :should=>[{:match=>{:name=>"Moscow"}}, {:match=>{:name=>"London"}}]}}}}>
# @param query_hash [Hash] pure query hash
#
# @overload should
# If block is passed instead of a pure hash, `elasticsearch-dsl"
# gem will be used to process it.
#
# @see https://github.com/elastic/elasticsearch-ruby/tree/master/elasticsearch-dsl
# @example
# PlacesIndex.query.should { match name: 'Moscow' }.query.should { match name: 'London' }
# # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
# # :should=>[{:match=>{:name=>"Moscow"}}, {:match=>{:name=>"London"}}]}}}}>
# @yield the block is processed by `elasticsearch-dsl` gem
#
# @!method must_not(query_hash = nil, &block)
# Executes {Chewy::Search::Parameters::QueryStorage#must_not} in the scope
# of newly created request object.
#
# @see Chewy::Search::Parameters::QueryStorage#must_not
# @return [Chewy::Search::Request]
#
# @overload must_not(query_hash)
# If pure hash is passed it is added to `must_not` array of the bool query.
#
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html
# @example
# PlacesIndex.query.must_not(match: {name: 'Moscow'}).query.must_not(match: {name: 'London'})
# # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
# # :must_not=>[{:match=>{:name=>"Moscow"}}, {:match=>{:name=>"London"}}]}}}}>
# @param query_hash [Hash] pure query hash
#
# @overload must_not
# If block is passed instead of a pure hash, `elasticsearch-dsl"
# gem will be used to process it.
#
# @see https://github.com/elastic/elasticsearch-ruby/tree/master/elasticsearch-dsl
# @example
# PlacesIndex.query.must_not { match name: 'Moscow' }.query.must_not { match name: 'London' }
# # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
# # :must_not=>[{:match=>{:name=>"Moscow"}}, {:match=>{:name=>"London"}}]}}}}>
# @yield the block is processed by `elasticsearch-dsl` gem
%i[must should must_not].each do |method|
define_method method do |query_hash = nil, &block|
raise ArgumentError, "Please provide a parameter or a block to `#{method}`" unless query_hash || block
@request.send(:modify, @parameter_name) { send(method, block || query_hash) }
end
end
# @!method and(query_hash_or_scope = nil, &block)
# Executes {Chewy::Search::Parameters::QueryStorage#and} in the scope
# of newly created request object.
#
# @see Chewy::Search::Parameters::QueryStorage#and
# @return [Chewy::Search::Request]
#
# @overload and(query_hash)
# If pure hash is passed, the current root `bool` query and
# the passed one are joined into a single `must` array of the
# new root query.
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html
# @example
# scope = PlacesIndex.query.must_not(match: {name: 'Moscow'})
# scope.query.and(match: {name: 'London'})
# # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
# # :must=>[{:bool=>{:must_not=>{:match=>{:name=>"Moscow"}}}}, {:match=>{:name=>"London"}}]}}}}>
# scope = PlacesIndex.query(match: {name: 'Moscow'})
# scope.query.and(match: {name: 'London'})
# # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
# # :must=>[{:match=>{:name=>"Moscow"}}, {:match=>{:name=>"London"}}]}}}}>
# @param query_hash [Hash] pure query hash
#
# @overload and(scope)
# If a scope is passed, the appropriate parameter storage value
# will be extracted from it and used as a second query.
# @example
# scope1 = PlacesIndex.query.must_not(match: {name: 'Moscow'})
# scope2 = PlacesIndex.query(match: {name: 'London'})
# scope1.query.and(scope2)
# # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
# # :must=>[{:bool=>{:must_not=>{:match=>{:name=>"Moscow"}}}}, {:match=>{:name=>"London"}}]}}}}>
# @param scope [Chewy::Search::Request] other scope
#
# @overload and
# If block is passed instead of a pure hash, `elasticsearch-dsl"
# gem will be used to process it.
# @see https://github.com/elastic/elasticsearch-ruby/tree/master/elasticsearch-dsl
# @example
# scope = PlacesIndex.query.must_not(match: {name: 'Moscow'})
# scope.query.and { match name: 'London' }
# # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
# # :must=>[{:bool=>{:must_not=>{:match=>{:name=>"Moscow"}}}}, {:match=>{:name=>"London"}}]}}}}>
# @yield the block is processed by `elasticsearch-dsl` gem
#
# @!method or(query_hash_or_scope = nil, &block)
# Executes {Chewy::Search::Parameters::QueryStorage#or} in the scope
# of newly created request object.
#
# @see Chewy::Search::Parameters::QueryStorage#or
# @return [Chewy::Search::Request]
#
# @overload or(query_hash)
# If pure hash is passed, the current root `bool` query and
# the passed one are joined into a single `should` array of the
# new root query.
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html
# @example
# scope = PlacesIndex.query.must_not(match: {name: 'Moscow'})
# scope.query.or(match: {name: 'London'})
# # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
# # :should=>[{:bool=>{:must_not=>{:match=>{:name=>"Moscow"}}}}, {:match=>{:name=>"London"}}]}}}}>
# scope = PlacesIndex.query(match: {name: 'Moscow'})
# scope.query.or(match: {name: 'London'})
# # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
# # :should=>[{:match=>{:name=>"Moscow"}}, {:match=>{:name=>"London"}}]}}}}>
# @param query_hash [Hash] pure query hash
#
# @overload or(scope)
# If a scope is passed, the appropriate parameter storage value
# will be extracted from it and used as a second query.
# @example
# scope1 = PlacesIndex.query.must_not(match: {name: 'Moscow'})
# scope2 = PlacesIndex.query(match: {name: 'London'})
# scope1.query.or(scope2)
# # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
# # :should=>[{:bool=>{:must_not=>{:match=>{:name=>"Moscow"}}}}, {:match=>{:name=>"London"}}]}}}}>
# @param scope [Chewy::Search::Request] other scope
#
# @overload or
# If block is passed instead of a pure hash, `elasticsearch-dsl"
# gem will be used to process it.
# @see https://github.com/elastic/elasticsearch-ruby/tree/master/elasticsearch-dsl
# @example
# scope = PlacesIndex.query.must_not(match: {name: 'Moscow'})
# scope.query.or { match name: 'London' }
# # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
# # :should=>[{:bool=>{:must_not=>{:match=>{:name=>"Moscow"}}}}, {:match=>{:name=>"London"}}]}}}}>
# @yield the block is processed by `elasticsearch-dsl` gem
#
# @!method not(query_hash_or_scope = nil, &block)
# Executes {Chewy::Search::Parameters::QueryStorage#not} in the scope
# of newly created request object.
# The only difference from {#must_not} is that is accepts another scope additionally.
#
# @see Chewy::Search::Parameters::QueryStorage#not
# @return [Chewy::Search::Request]
#
# @overload not(query_hash)
# If pure hash is passed it is added to `must_not` array of the bool query.
# @see https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html
# @example
# scope = PlacesIndex.query.must_not(match: {name: 'Moscow'})
# scope.query.not(match: {name: 'London'})
# # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
# # :must_not=>[{:match=>{:name=>"Moscow"}}, {:match=>{:name=>"London"}}]}}}}>
# @param query_hash [Hash] pure query hash
#
# @overload not(scope)
# If a scope is passed, the appropriate parameter storage value
# will be extracted from it and used as a second query.
# @example
# scope1 = PlacesIndex.query.must_not(match: {name: 'Moscow'})
# scope2 = PlacesIndex.query(match: {name: 'London'})
# scope1.query.not(scope2)
# # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
# # :must_not=>[{:match=>{:name=>"Moscow"}}, {:match=>{:name=>"London"}}]}}}}>
# @param scope [Chewy::Search::Request] other scope
#
# @overload not
# If block is passed instead of a pure hash, `elasticsearch-dsl"
# gem will be used to process it.
# @see https://github.com/elastic/elasticsearch-ruby/tree/master/elasticsearch-dsl
# @example
# scope = PlacesIndex.query.must_not(match: {name: 'Moscow'})
# scope.query.not { match name: 'London' }
# # => <PlacesIndex::Query {..., :body=>{:query=>{:bool=>{
# # :must_not=>[{:match=>{:name=>"Moscow"}}, {:match=>{:name=>"London"}}]}}}}>
# @yield the block is processed by `elasticsearch-dsl` gem
%i[and or not].each do |method|
define_method method do |query_hash_or_scope = nil, &block|
raise ArgumentError, "Please provide a parameter or a block to `#{method}`" unless query_hash_or_scope || block
query_hash_or_scope = query_hash_or_scope.parameters[@parameter_name].value if !block && query_hash_or_scope.is_a?(Chewy::Search::Request)
@request.send(:modify, @parameter_name) { send(method, block || query_hash_or_scope) }
end
end
# Executes {Chewy::Search::Parameters::QueryStorage#minimum_should_match} in the scope
# of newly created request object.
#
# @see Chewy::Search::Parameters::QueryStorage#minimum_should_match
# @param value [String, Integer, nil]
# @return [Chewy::Search::Request]
def minimum_should_match(value)
@request.send(:modify, @parameter_name) { minimum_should_match(value) }
end
end
end
end