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, #extract_options!, #is_empty?, #require_params!

Constructor Details

#initialize(options = {}) ⇒ API

Returns a new instance of API.



14
15
16
17
18
# File 'lib/stretchy/api.rb', line 14

def initialize(options = {})
  @collector  = AndCollector.new(options[:nodes] || [], query: true)
  @root       = options[:root]     || {}
  @context    = options[:context]  || {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



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

def method_missing(method, *args, &block)
  if results_obj.respond_to?(method)
    results_obj.send(method, *args, &block)
  elsif collector.respond_to?(method)
    collector.send(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#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

#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



57
58
59
# File 'lib/stretchy/api.rb', line 57

def aggs(params = {})
  add_root aggs: params
end

#boost(params = {}, options = {}) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/stretchy/api.rb', line 110

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

Returns:

  • (Boolean)


20
21
22
# File 'lib/stretchy/api.rb', line 20

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

#current_pageObject



45
46
47
# File 'lib/stretchy/api.rb', line 45

def current_page
  Utils.current_page(offset, limit)
end

#explainObject



49
50
51
# File 'lib/stretchy/api.rb', line 49

def explain
  add_root explain: true
end

#field_value(params = {}) ⇒ Object



122
123
124
# File 'lib/stretchy/api.rb', line 122

def field_value(params = {})
  add_params params, :boost, :field_value_function_node
end

#fields(*list) ⇒ Object



53
54
55
# File 'lib/stretchy/api.rb', line 53

def fields(*list)
  add_root fields: list
end

#filter(params = {}) ⇒ Object



89
90
91
# File 'lib/stretchy/api.rb', line 89

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

#fulltext(params = '') ⇒ Object



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

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



106
107
108
# File 'lib/stretchy/api.rb', line 106

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

#limit(size = nil) ⇒ Object Also known as: limit_value



24
25
26
27
# File 'lib/stretchy/api.rb', line 24

def limit(size = nil)
  return @root[:size] || DEFAULT_PER_PAGE unless size
  add_root size: size.to_i
end

#match(params = {}) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/stretchy/api.rb', line 65

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



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

def more_like(params = {})
  params[:ids] = Array(params[:ids]) if params[:ids]
  add_params params, :query, :more_like_node
end

#near(params = {}) ⇒ Object



134
135
136
# File 'lib/stretchy/api.rb', line 134

def near(params = {})
  add_params params, :boost, :decay_function_node
end

#not(params = {}) ⇒ Object



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

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

#offset(from = nil) ⇒ Object



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

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



37
38
39
40
41
42
43
# File 'lib/stretchy/api.rb', line 37

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

#query(params = {}) ⇒ Object



85
86
87
# File 'lib/stretchy/api.rb', line 85

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

#random(params) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/stretchy/api.rb', line 126

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



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

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

#requestObject



138
139
140
141
142
143
144
145
146
147
# File 'lib/stretchy/api.rb', line 138

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

#responseObject



149
150
151
# File 'lib/stretchy/api.rb', line 149

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

#results_objObject



153
154
155
# File 'lib/stretchy/api.rb', line 153

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

#should(params = {}) ⇒ Object



93
94
95
# File 'lib/stretchy/api.rb', line 93

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

#where(params = {}) ⇒ Object



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

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