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, #dotify, #extract_options!, #is_empty?, #nestify, #require_params!
Constructor Details
#initialize(opts = {}) ⇒ API
Returns a new instance of API.
21
22
23
24
25
26
27
|
# File 'lib/stretchy/api.rb', line 21
def initialize(opts = {})
@opts = opts
@collector = AndCollector.new(opts[:nodes] || [], query: true)
@root = opts[:root] || {}
@body = opts[:body] || {}
@context = opts[:context] || {}
end
|
Instance Attribute Details
#body ⇒ Object
Returns the value of attribute body.
12
13
14
|
# File 'lib/stretchy/api.rb', line 12
def body
@body
end
|
#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
74
75
76
|
# File 'lib/stretchy/api.rb', line 74
def aggs(params = {})
add_body aggs: params
end
|
#boost(params = {}, options = {}) ⇒ Object
134
135
136
137
138
139
140
141
142
143
144
|
# File 'lib/stretchy/api.rb', line 134
def boost(params = {}, options = {})
return add_context(:boost) if Utils.is_empty? params
subcontext = context.merge(boost: true)
if params.is_a? self.class
boost_json = options.merge(filter: params.json)
add_nodes Node.new(boost_json, subcontext)
else
add_nodes Factory.raw_boost_node(params, subcontext)
end
end
|
#context?(*args) ⇒ Boolean
29
30
31
|
# File 'lib/stretchy/api.rb', line 29
def context?(*args)
(args - context.keys).empty?
end
|
#count ⇒ Object
176
177
178
|
# File 'lib/stretchy/api.rb', line 176
def count
results_obj.ids.count
end
|
#current_page ⇒ Object
60
61
62
|
# File 'lib/stretchy/api.rb', line 60
def current_page
Utils.current_page(offset, limit)
end
|
#explain ⇒ Object
64
65
66
|
# File 'lib/stretchy/api.rb', line 64
def explain
add_root explain: true
end
|
#field_value(params = {}) ⇒ Object
146
147
148
|
# File 'lib/stretchy/api.rb', line 146
def field_value(params = {})
add_params params, :boost, :field_value_function_node
end
|
#fields(*list) ⇒ Object
Also known as:
source
68
69
70
|
# File 'lib/stretchy/api.rb', line 68
def fields(*list)
add_root _source: list
end
|
#filter(params = {}) ⇒ Object
114
115
116
|
# File 'lib/stretchy/api.rb', line 114
def filter(params = {})
add_params params, :filter, :raw_node
end
|
#fulltext(params = '') ⇒ Object
#geo_distance(params = {}) ⇒ Object
130
131
132
|
# File 'lib/stretchy/api.rb', line 130
def geo_distance(params = {})
add_params params, nil, :geo_distance_node
end
|
#highlight(params = {}) ⇒ Object
78
79
80
|
# File 'lib/stretchy/api.rb', line 78
def highlight(params = {})
add_body highlight: params
end
|
#limit(size = nil) ⇒ Object
Also known as:
limit_value
33
34
35
36
|
# File 'lib/stretchy/api.rb', line 33
def limit(size = nil)
return @root[:size] || DEFAULT_PER_PAGE unless size
add_root size: size.to_i
end
|
#match(params = {}) ⇒ Object
88
89
90
91
92
93
94
95
96
|
# File 'lib/stretchy/api.rb', line 88
def match(params = {})
if params.is_a? Hash
subcontext = {query: true}
subcontext[:nested] = true if params.delete(:nested)
add_params params, subcontext, :context_nodes
else
add_params Hash[_all: params], :query, :context_nodes
end
end
|
#more_like(params = {}) ⇒ Object
98
99
100
101
|
# File 'lib/stretchy/api.rb', line 98
def more_like(params = {})
params[:ids] = Array(params[:ids]) if params[:ids]
add_params params, :query, :more_like_node
end
|
#near(params = {}) ⇒ Object
158
159
160
|
# File 'lib/stretchy/api.rb', line 158
def near(params = {})
add_params params, :boost, :decay_function_node
end
|
#not(params = {}) ⇒ Object
122
123
124
|
# File 'lib/stretchy/api.rb', line 122
def not(params = {})
add_params params, :must_not, :context_nodes
end
|
#offset(from = nil) ⇒ Object
39
40
41
42
|
# File 'lib/stretchy/api.rb', line 39
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
46
47
48
49
50
51
52
|
# File 'lib/stretchy/api.rb', line 46
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
54
55
56
57
|
# File 'lib/stretchy/api.rb', line 54
def per(num = nil)
return limit if num.nil?
add_root size: [num.to_i, 1].max
end
|
#query(params = {}) ⇒ Object
110
111
112
|
# File 'lib/stretchy/api.rb', line 110
def query(params = {})
add_params params, :query, :raw_node
end
|
#random(params) ⇒ Object
150
151
152
153
154
155
156
|
# File 'lib/stretchy/api.rb', line 150
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
126
127
128
|
# File 'lib/stretchy/api.rb', line 126
def range(params = {})
add_params params, nil, :range_node
end
|
#request ⇒ Object
162
163
164
165
166
|
# File 'lib/stretchy/api.rb', line 162
def request
@request ||= begin
root.merge(body: body.merge(query: collector.as_json))
end
end
|
#response ⇒ Object
168
169
170
|
# File 'lib/stretchy/api.rb', line 168
def response
@response ||= Stretchy.search(request)
end
|
#results_obj ⇒ Object
172
173
174
|
# File 'lib/stretchy/api.rb', line 172
def results_obj
@results ||= Results.new request, response
end
|
#should(params = {}) ⇒ Object
118
119
120
|
# File 'lib/stretchy/api.rb', line 118
def should(params = {})
add_params params, :should, :context_nodes
end
|
#where(params = {}) ⇒ Object
82
83
84
85
86
|
# File 'lib/stretchy/api.rb', line 82
def where(params = {})
subcontext = {filter: true}
subcontext[:nested] = params.delete(:nested) if params[:nested]
add_params params, subcontext, :context_nodes
end
|