Class: Stretchy::API

Inherits:
Object
  • 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

Methods included from Utils::Methods

#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

#bodyObject (readonly)

Returns the value of attribute body.



12
13
14
# File 'lib/stretchy/api.rb', line 12

def body
  @body
end

#collectorObject (readonly)

Returns the value of attribute collector.



12
13
14
# File 'lib/stretchy/api.rb', line 12

def collector
  @collector
end

#contextObject (readonly)

Returns the value of attribute context.



12
13
14
# File 'lib/stretchy/api.rb', line 12

def context
  @context
end

#optsObject (readonly)

Returns the value of attribute opts.



12
13
14
# File 'lib/stretchy/api.rb', line 12

def opts
  @opts
end

#rootObject (readonly)

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



72
73
74
# File 'lib/stretchy/api.rb', line 72

def aggs(params = {})
  add_body 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) 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

Returns:

  • (Boolean)


29
30
31
# File 'lib/stretchy/api.rb', line 29

def context?(*args)
  (args - context.keys).empty?
end

#countObject



174
175
176
# File 'lib/stretchy/api.rb', line 174

def count
  results_obj.ids.count
end

#current_pageObject



60
61
62
# File 'lib/stretchy/api.rb', line 60

def current_page
  Utils.current_page(offset, limit)
end

#explainObject



64
65
66
# File 'lib/stretchy/api.rb', line 64

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



68
69
70
# File 'lib/stretchy/api.rb', line 68

def fields(*list)
  add_root fields: list
end

#filter(params = {}) ⇒ Object



112
113
114
# File 'lib/stretchy/api.rb', line 112

def filter(params = {})
  add_params params, :filter, :raw_node
end

#fulltext(params = '') ⇒ Object



101
102
103
104
105
106
# File 'lib/stretchy/api.rb', line 101

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, nil, :geo_distance_node
end

#highlight(params = {}) ⇒ Object



76
77
78
# File 'lib/stretchy/api.rb', line 76

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



86
87
88
89
90
91
92
93
94
# File 'lib/stretchy/api.rb', line 86

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



96
97
98
99
# File 'lib/stretchy/api.rb', line 96

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



120
121
122
# File 'lib/stretchy/api.rb', line 120

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



108
109
110
# File 'lib/stretchy/api.rb', line 108

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



124
125
126
# File 'lib/stretchy/api.rb', line 124

def range(params = {})
  add_params params, nil, :range_node
end

#requestObject



160
161
162
163
164
# File 'lib/stretchy/api.rb', line 160

def request
  @request ||= begin
    root.merge(body: body.merge(query: collector.as_json))
  end
end

#responseObject



166
167
168
# File 'lib/stretchy/api.rb', line 166

def response
  @response ||= Stretchy.search(request)
end

#results_objObject



170
171
172
# File 'lib/stretchy/api.rb', line 170

def results_obj
  @results ||= Results.new request, response
end

#should(params = {}) ⇒ Object



116
117
118
# File 'lib/stretchy/api.rb', line 116

def should(params = {})
  add_params params, :should, :context_nodes
end

#where(params = {}) ⇒ Object



80
81
82
83
84
# File 'lib/stretchy/api.rb', line 80

def where(params = {})
  subcontext = {filter: true}
  subcontext[:nested] = params.delete(:nested) if params[:nested]
  add_params params, subcontext, :context_nodes
end