Class: Stretchy::API
- Inherits:
-
Object
show all
- Extended by:
- Forwardable
- Includes:
- Enumerable, Utils::Methods
- Defined in:
- lib/stretchy/api.rb
Constant Summary
collapse
- DEFAULT_BOOST =
2.0
- DEFAULT_PER_PAGE =
10
Instance Attribute Summary collapse
Instance Method Summary
collapse
#coerce_id, #extract_options!, #is_empty?, #require_params!
Constructor Details
#initialize(opts = {}) ⇒ API
Returns a new instance of API.
29
30
31
32
33
34
|
# File 'lib/stretchy/api.rb', line 29
def initialize(opts = {})
@opts = opts
@collector = AndCollector.new(opts[:nodes] || [], query: true)
@root = opts[:root] || {}
@context = opts[:context] || {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
183
184
185
186
187
188
189
|
# File 'lib/stretchy/api.rb', line 183
def method_missing(method, *args, &block)
if collector.respond_to?(method)
collector.send(method, *args, &block)
else
super
end
end
|
Instance Attribute Details
#collector ⇒ Object
Returns the value of attribute collector.
12
13
14
|
# File 'lib/stretchy/api.rb', line 12
def collector
@collector
end
|
#context ⇒ Object
Returns the value of attribute context.
12
13
14
|
# File 'lib/stretchy/api.rb', line 12
def context
@context
end
|
#opts ⇒ Object
Returns the value of attribute opts.
12
13
14
|
# File 'lib/stretchy/api.rb', line 12
def opts
@opts
end
|
#root ⇒ Object
Returns the value of attribute root.
12
13
14
|
# File 'lib/stretchy/api.rb', line 12
def root
@root
end
|
Instance Method Details
#aggs(params = {}) ⇒ Object
79
80
81
|
# File 'lib/stretchy/api.rb', line 79
def aggs(params = {})
add_root aggs: params
end
|
#boost(params = {}, options = {}) ⇒ Object
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/stretchy/api.rb', line 132
def boost(params = {}, options = {})
return add_context(:boost) unless params.any?
subcontext = context.merge(boost: true)
if params.is_a? self.class
boost_json = options.merge(filter: params.filter_node.json)
add_nodes Node.new(boost_json, subcontext)
else
add_nodes Factory.raw_boost_node(params, subcontext)
end
end
|
#context?(*args) ⇒ Boolean
36
37
38
|
# File 'lib/stretchy/api.rb', line 36
def context?(*args)
(args - context.keys).empty?
end
|
#count ⇒ Object
179
180
181
|
# File 'lib/stretchy/api.rb', line 179
def count
results_obj.ids.count
end
|
#current_page ⇒ Object
67
68
69
|
# File 'lib/stretchy/api.rb', line 67
def current_page
Utils.current_page(offset, limit)
end
|
#explain ⇒ Object
71
72
73
|
# File 'lib/stretchy/api.rb', line 71
def explain
add_root explain: true
end
|
#field_value(params = {}) ⇒ Object
144
145
146
|
# File 'lib/stretchy/api.rb', line 144
def field_value(params = {})
add_params params, :boost, :field_value_function_node
end
|
#fields(*list) ⇒ Object
75
76
77
|
# File 'lib/stretchy/api.rb', line 75
def fields(*list)
add_root fields: list
end
|
#filter(params = {}) ⇒ Object
111
112
113
|
# File 'lib/stretchy/api.rb', line 111
def filter(params = {})
add_params params, :filter, :raw_node
end
|
#fulltext(params = '') ⇒ Object
100
101
102
103
104
105
|
# File 'lib/stretchy/api.rb', line 100
def fulltext(params = '')
unless params.is_a?(String)
raise Errors::InvalidParamsError.new('.fulltext only takes a string')
end
add_nodes Factory.fulltext_nodes_from_string(params, context)
end
|
#geo_distance(params = {}) ⇒ Object
128
129
130
|
# File 'lib/stretchy/api.rb', line 128
def geo_distance(params = {})
add_params params, :filter, :geo_distance_node
end
|
#limit(size = nil) ⇒ Object
Also known as:
limit_value
40
41
42
43
|
# File 'lib/stretchy/api.rb', line 40
def limit(size = nil)
return @root[:size] || DEFAULT_PER_PAGE unless size
add_root size: size.to_i
end
|
#match(params = {}) ⇒ Object
87
88
89
90
91
92
93
|
# File 'lib/stretchy/api.rb', line 87
def match(params = {})
if params.is_a? Hash
add_params params, :query, :context_nodes
else
add_params Hash[_all: params], :query, :context_nodes
end
end
|
#more_like(params = {}) ⇒ Object
95
96
97
98
|
# File 'lib/stretchy/api.rb', line 95
def more_like(params = {})
params[:ids] = Array(params[:ids]) if params[:ids]
add_params params, :query, :more_like_node
end
|
#near(params = {}) ⇒ Object
156
157
158
|
# File 'lib/stretchy/api.rb', line 156
def near(params = {})
add_params params, :boost, :decay_function_node
end
|
#not(params = {}) ⇒ Object
119
120
121
|
# File 'lib/stretchy/api.rb', line 119
def not(params = {})
add_params params, :must_not, :context_nodes
end
|
#offset(from = nil) ⇒ Object
46
47
48
49
|
# File 'lib/stretchy/api.rb', line 46
def offset(from = nil)
return @root[:from] || 0 unless from
add_root from: from.to_i
end
|
#page(num = nil, params = {}) ⇒ Object
page 1 = from: 0, size: per_page
page 2 = from: per_page, size: per_page
53
54
55
56
57
58
59
|
# File 'lib/stretchy/api.rb', line 53
def page(num = nil, params = {})
return current_page if num.nil?
per = params[:limit] || params[:per_page] || limit
per = per.to_i > 0 ? per.to_i : 1
start = [num.to_i - 1, 0].max
add_root from: start * per, size: per
end
|
#per(num = nil) ⇒ Object
Also known as:
per_page
61
62
63
64
|
# File 'lib/stretchy/api.rb', line 61
def per(num = nil)
return limit if num.nil?
add_root size: [num.to_i, 1].max
end
|
#query(params = {}) ⇒ Object
107
108
109
|
# File 'lib/stretchy/api.rb', line 107
def query(params = {})
add_params params, :query, :raw_node
end
|
#random(params) ⇒ Object
148
149
150
151
152
153
154
|
# File 'lib/stretchy/api.rb', line 148
def random(params)
if params.is_a? Hash
add_params params, :boost, :random_score_function_node
else
add_params Hash[seed: params], :boost, :random_score_function_node
end
end
|
#range(params = {}) ⇒ Object
123
124
125
126
|
# File 'lib/stretchy/api.rb', line 123
def range(params = {})
require_context!
add_params params, nil, :range_node
end
|
#request ⇒ Object
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/stretchy/api.rb', line 160
def request
@request ||= begin
base = root.dup
sub = {query: collector.as_json}
agg = base.delete(:aggs) || {}
sub[:aggs] = agg if agg.any?
base.merge(body: sub)
end
end
|
#response ⇒ Object
171
172
173
|
# File 'lib/stretchy/api.rb', line 171
def response
@response ||= Stretchy.search(request)
end
|
#results_obj ⇒ Object
175
176
177
|
# File 'lib/stretchy/api.rb', line 175
def results_obj
@results ||= Results.new request, response
end
|
#should(params = {}) ⇒ Object
115
116
117
|
# File 'lib/stretchy/api.rb', line 115
def should(params = {})
add_params params, :should, :context_nodes
end
|
#where(params = {}) ⇒ Object
83
84
85
|
# File 'lib/stretchy/api.rb', line 83
def where(params = {})
add_params params, :filter, :context_nodes
end
|