Class: Stretchy::Clauses::Base
- Inherits:
-
Object
- Object
- Stretchy::Clauses::Base
- Extended by:
- Forwardable
- Defined in:
- lib/stretchy/clauses/base.rb
Overview
A Clause is the basic unit of Stretchy's chainable query syntax.
Think of it as a state machine, with transitions between states
being handled by methods that return another Clause. When you
call the where method, it stores the params passed as an
internal representation, to be compiled down to the Elastic query
syntax, and returns a WhereClause. The WhereClause reflects the
current state of the query, and gives you access to methods like
range and geo to add more specific filters. It inherits
other methods from the Base class, allowing other transitions.
Attributes are copied when a new Clause is instanciated, so the underlying storage is maintained throughout the chain.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
Instance Method Summary collapse
-
#aggregations(opts = {}) ⇒ self
(also: #aggs)
Allows adding raw aggregation JSON to your query.
-
#boost ⇒ BoostClause
Used for boosting the relevance score of search results.
-
#explain ⇒ self
Tells the search to explain the scoring mechanism for each document.
-
#fields(*args) ⇒ self
Select fields for Elasticsearch to return.
- #get_aggregations ⇒ Object (also: #get_aggs)
- #get_explain ⇒ Object
-
#get_fields ⇒ Array
Accessor for fields Elasticsearch will return.
-
#get_limit ⇒ Integer
(also: #limit_value)
Accessor for
@limit. -
#get_offset ⇒ Integer
Accessor for
@offset. -
#get_page ⇒ Integer
(also: #current_page)
Accessor for current page.
-
#initialize(base = nil, options = {}) ⇒ Base
constructor
Generates a chainable query.
-
#inverse? ⇒ true, false
Accessor for
@inverse. -
#limit(num) ⇒ self
Sets how many results to return, similar to ActiveRecord's limit method.
-
#match(options = {}) ⇒ MatchClause
Used for fulltext searching.
- #not(opts_or_string = {}) ⇒ Base
-
#offset(num) ⇒ self
(also: #per_page)
Sets the offset to start returning results at.
-
#page(num, options = {}) ⇒ self
Allows pagination via Kaminari-like accessor.
-
#query_results ⇒ Results::Base
The Results object for this query, which handles sending the search request and providing convienent accessors for the response.
-
#should(opts_or_string = {}) ⇒ WhereClause
Adds filters in the
shouldcontext. -
#where(options = {}) ⇒ WhereClause
(also: #filter)
Used for filtering results.
Constructor Details
#initialize(base_or_opts, options) ⇒ Base #initialize(base_or_opts) ⇒ Base
Generates a chainable query. The only required option for the
first initialization is :type , which specifies what type
to query on your index.
40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/stretchy/clauses/base.rb', line 40 def initialize(base = nil, = {}) if base.is_a?(Builders::ShellBuilder) @base = base elsif base.nil? @base = Builders::ShellBuilder.new else @base = Builders::ShellBuilder.new(base) end @base.index ||= [:index] || Stretchy.index_name @base.type = [:type] if [:type] end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
19 20 21 |
# File 'lib/stretchy/clauses/base.rb', line 19 def base @base end |
Instance Method Details
#aggregations(opts = {}) ⇒ self Also known as: aggs
Allows adding raw aggregation JSON to your query
268 269 270 271 |
# File 'lib/stretchy/clauses/base.rb', line 268 def aggregations(opts = {}) base.aggregate_builder = base.aggregate_builder.merge(opts) self end |
#boost ⇒ BoostClause
Used for boosting the relevance score of
search results. match and where clauses
added after boost will be applied as
boosting functions instead of filters
209 210 211 |
# File 'lib/stretchy/clauses/base.rb', line 209 def boost BoostClause.new(base) end |
#explain ⇒ self
Tells the search to explain the scoring mechanism for each document.
154 155 156 157 |
# File 'lib/stretchy/clauses/base.rb', line 154 def explain base.explain = true self end |
#fields(*args) ⇒ self
Select fields for Elasticsearch to return
By default, Stretchy will return the entire _source
for each document. If you call .fields with no
arguments or an empty array, Stretchy will pass
an empty array and only the "_type" and "_id"
fields will be returned.
133 134 135 136 137 |
# File 'lib/stretchy/clauses/base.rb', line 133 def fields(*args) base.fields ||= [] base.fields += args.flatten if args.any? self end |
#get_aggregations ⇒ Object Also known as: get_aggs
274 275 276 |
# File 'lib/stretchy/clauses/base.rb', line 274 def get_aggregations base.aggregate_builder end |
#get_explain ⇒ Object
159 160 161 |
# File 'lib/stretchy/clauses/base.rb', line 159 def get_explain !!base.explain end |
#get_fields ⇒ Array
Accessor for fields Elasticsearch will return
143 144 145 |
# File 'lib/stretchy/clauses/base.rb', line 143 def get_fields base.fields end |
#get_limit ⇒ Integer Also known as: limit_value
Accessor for @limit
70 71 72 |
# File 'lib/stretchy/clauses/base.rb', line 70 def get_limit base.limit end |
#get_offset ⇒ Integer
Accessor for @offset
94 95 96 |
# File 'lib/stretchy/clauses/base.rb', line 94 def get_offset base.offset end |
#get_page ⇒ Integer Also known as: current_page
Accessor for current page
114 115 116 |
# File 'lib/stretchy/clauses/base.rb', line 114 def get_page base.page end |
#inverse? ⇒ true, false
Accessor for @inverse
283 284 285 |
# File 'lib/stretchy/clauses/base.rb', line 283 def inverse? !!@inverse end |
#limit(num) ⇒ self
Sets how many results to return, similar to ActiveRecord's limit method.
61 62 63 64 |
# File 'lib/stretchy/clauses/base.rb', line 61 def limit(num) base.limit = num self end |
#match(options = {}) ⇒ MatchClause
Used for fulltext searching. Works similarly to #where .
175 176 177 |
# File 'lib/stretchy/clauses/base.rb', line 175 def match( = {}) MatchClause.new(base, ) end |
#not(string) ⇒ Base #not(opts_or_string) ⇒ Base
228 229 230 231 232 233 234 |
# File 'lib/stretchy/clauses/base.rb', line 228 def not(opts_or_string = {}) if opts_or_string.is_a?(Hash) WhereClause.new(base).not(opts_or_string) else MatchClause.new(base).not(opts_or_string) end end |
#offset(num) ⇒ self Also known as: per_page
Sets the offset to start returning results at. Corresponds to Elastic's "from" parameter
84 85 86 87 |
# File 'lib/stretchy/clauses/base.rb', line 84 def offset(num) base.offset = num self end |
#page(num, options = {}) ⇒ self
Allows pagination via Kaminari-like accessor
104 105 106 107 108 |
# File 'lib/stretchy/clauses/base.rb', line 104 def page(num, = {}) base.limit = [:limit] || [:per_page] || get_limit base.offset = [(num - 1), 0].max.ceil * get_limit self end |
#query_results ⇒ Results::Base
The Results object for this query, which handles sending the search request and providing convienent accessors for the response.
293 294 295 |
# File 'lib/stretchy/clauses/base.rb', line 293 def query_results @query_results ||= Stretchy::Results::Base.new(base) end |
#should(opts_or_string) ⇒ WhereClause #should(opts_or_string) ⇒ WhereClause
Adds filters in the should context. Operates just like
#where, but these filters only serve to add to the
relevance score of the returned documents, rather than
being required to match.
254 255 256 257 258 259 260 |
# File 'lib/stretchy/clauses/base.rb', line 254 def should(opts_or_string = {}) if opts_or_string.is_a?(Hash) WhereClause.new(base).should(opts_or_string) else MatchClause.new(base).should(opts_or_string) end end |
#where(options = {}) ⇒ WhereClause Also known as: filter
Used for filtering results. Works similarly to
ActiveRecord's where method.
189 190 191 |
# File 'lib/stretchy/clauses/base.rb', line 189 def where( = {}) WhereClause.new(base, ) end |