This repository has been archived by the owner on Jun 30, 2018. It is now read-only.
/
query.rb
219 lines (180 loc) · 5.53 KB
/
query.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
module Tire
module Search
class Query
def initialize(&block)
@value = {}
block.arity < 1 ? self.instance_eval(&block) : block.call(self) if block_given?
end
def term(field, value, options={})
query = if value.is_a?(Hash)
{ field => value.to_hash }
else
{ field => { :term => value }.update(options) }
end
@value = { :term => query }
end
def terms(field, value, options={})
@value = { :terms => { field => value } }
@value[:terms].update( { :minimum_match => options[:minimum_match] } ) if options[:minimum_match]
@value
end
def range(field, value)
@value = { :range => { field => value } }
end
def text(field, value, options={})
query_options = { :query => value }.update(options)
@value = { :text => { field => query_options } }
@value
end
def string(value, options={})
@value = { :query_string => { :query => value } }
@value[:query_string].update(options)
@value
end
def prefix(field, value, options={})
if options[:boost]
@value = { :prefix => { field => { :prefix => value, :boost => options[:boost] } } }
else
@value = { :prefix => { field => value } }
end
end
def custom_score(options={}, &block)
@custom_score ||= Query.new(&block)
@value[:custom_score] = options
@value[:custom_score].update({:query => @custom_score.to_hash})
@value
end
def fuzzy(field, value, options={})
query = { field => { :term => value }.update(options) }
@value = { :fuzzy => query }
end
def boolean(options={}, &block)
@boolean ||= BooleanQuery.new(options)
block.arity < 1 ? @boolean.instance_eval(&block) : block.call(@boolean) if block_given?
@value[:bool] = @boolean.to_hash
@value
end
def filtered(&block)
@filtered = FilteredQuery.new
block.arity < 1 ? @filtered.instance_eval(&block) : block.call(@filtered) if block_given?
@value[:filtered] = @filtered.to_hash
@value
end
def dis_max(options={}, &block)
@dis_max ||= DisMaxQuery.new(options)
block.arity < 1 ? @dis_max.instance_eval(&block) : block.call(@dis_max) if block_given?
@value[:dis_max] = @dis_max.to_hash
@value
end
def all(options = {})
@value = { :match_all => options }
@value
end
def ids(values, type)
@value = { :ids => { :values => values, :type => type } }
end
def boosting(options={}, &block)
@boosting ||= BoostingQuery.new(options)
block.arity < 1 ? @boosting.instance_eval(&block) : block.call(@boosting) if block_given?
@value[:boosting] = @boosting.to_hash
@value
end
def to_hash
@value
end
def to_json(options={})
to_hash.to_json
end
end
class BooleanQuery
# TODO: Try to get rid of multiple `should`, `must`, etc invocations, and wrap queries directly:
#
# boolean do
# should do
# string 'foo'
# string 'bar'
# end
# end
#
# Inherit from Query, implement `encode` method there, and overload it here, so it puts
# queries in an Array instead of hash.
def initialize(options={}, &block)
@options = options
@value = {}
block.arity < 1 ? self.instance_eval(&block) : block.call(self) if block_given?
end
def must(&block)
(@value[:must] ||= []) << Query.new(&block).to_hash
@value
end
def must_not(&block)
(@value[:must_not] ||= []) << Query.new(&block).to_hash
@value
end
def should(&block)
(@value[:should] ||= []) << Query.new(&block).to_hash
@value
end
def to_hash
@value.update(@options)
end
end
class FilteredQuery
def initialize(&block)
@value = {}
block.arity < 1 ? self.instance_eval(&block) : block.call(self) if block_given?
end
def query(options={}, &block)
@value[:query] = Query.new(&block).to_hash
@value
end
def filter(type, *options)
@value[:filter] ||= {}
@value[:filter][:and] ||= []
@value[:filter][:and] << Filter.new(type, *options).to_hash
@value
end
def to_hash
@value
end
def to_json(options={})
to_hash.to_json
end
end
class DisMaxQuery
def initialize(options={}, &block)
@options = options
@value = {}
block.arity < 1 ? self.instance_eval(&block) : block.call(self) if block_given?
end
def query(&block)
(@value[:queries] ||= []) << Query.new(&block).to_hash
@value
end
def to_hash
@value.update(@options)
end
def to_json(options={})
to_hash.to_json
end
end
class BoostingQuery
def initialize(options={}, &block)
@options = options
@value = {}
block.arity < 1 ? self.instance_eval(&block) : block.call(self) if block_given?
end
def positive(&block)
(@value[:positive] ||= []) << Query.new(&block).to_hash
@value
end
def negative(&block)
(@value[:negative] ||= []) << Query.new(&block).to_hash
@value
end
def to_hash
@value.update(@options)
end
end
end
end