Class: Stretchy::Clauses::BoostClause
- Extended by:
- Forwardable
- Defined in:
- lib/stretchy/clauses/boost_clause.rb
Overview
A Boost clause encapsulates the boost query state. It basically says "the next where / range / match filter will be used to boost a document's score instead of selecting documents to return."
Calling .boost
by itself doesn't do anything, but
the next method (.near
, .match
, etc) will specify
a boost using the same syntax as other clauses. These
methods take a :weight
parameter specifying the weight
to assign that boost.
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#all(num) ⇒ self
Defines a global boost for all documents in the query.
-
#boost_mode(mode) ⇒ self
Set boost mode for when a document matches multiple boost functions.
-
#field(*args) ⇒ self
Adds a boost based on the value in the specified field.
-
#match(params = {}, options = {}) ⇒ BoostMatchClause
Changes query state to "match" in the context of boosting.
-
#max(num) ⇒ self
The maximum boost that any document can have.
-
#near(params = {}, options = {}) ⇒ Base
(also: #geo)
Adds a Boosts::FieldDecayBoost, which boosts a search result based on how close it is to a specified value.
- #not(*args) ⇒ Object
-
#random(*args) ⇒ Base
Adds a Boosts::RandomBoost to the query, for slightly randomizing search results.
-
#score_mode(mode) ⇒ self
Set scoring mode for when a document matches multiple boost functions.
-
#where(params = {}, options = {}) ⇒ BoostWhereClause
(also: #filter)
Changes query state to "where" in the context of boosting.
Methods inherited from Base
#aggregations, #boost, #explain, #fields, #get_aggregations, #get_explain, #get_fields, #get_limit, #get_offset, #get_page, #initialize, #inverse?, #limit, #offset, #page, #query_results, #root, #should
Constructor Details
This class inherits a constructor from Stretchy::Clauses::Base
Instance Method Details
#all(num) ⇒ self
Defines a global boost for all documents in the query
178 179 180 181 |
# File 'lib/stretchy/clauses/boost_clause.rb', line 178 def all(num) base.boost_builder.overall_boost = num self end |
#boost_mode(mode) ⇒ self
Set boost mode for when a document matches multiple boost functions.
213 214 215 216 |
# File 'lib/stretchy/clauses/boost_clause.rb', line 213 def boost_mode(mode) base.boost_builder.boost_mode = mode self end |
#field(*args) ⇒ self
Adds a boost based on the value in the specified field.
You can pass more than one field as arguments, and
you can also pass the factor
and modifier
options
as an options hash.
CAUTION: All documents in the index must have a numeric value for any fields specified here, or the query will fail.
79 80 81 82 83 84 85 |
# File 'lib/stretchy/clauses/boost_clause.rb', line 79 def field(*args) = args.last.is_a?(Hash) ? args.pop : {} args.each do |field| base.boost_builder.add_boost(Boosts::FieldValueBoost.new(field, )) end Base.new(base) end |
#match(params = {}, options = {}) ⇒ BoostMatchClause
Changes query state to "match" in the context of boosting. Options here work the same way as Stretchy::Clauses::Base#initialize, but the combined query will be applied as a boost function.
35 36 37 38 39 |
# File 'lib/stretchy/clauses/boost_clause.rb', line 35 def match(params = {}, = {}) clause = BoostMatchClause.new(base) clause = clause.boost_match(params, ) unless params.empty? && .empty? clause end |
#max(num) ⇒ self
The maximum boost that any document can have
189 190 191 192 |
# File 'lib/stretchy/clauses/boost_clause.rb', line 189 def max(num) base.boost_builder.max_boost = num self end |
#near(params = {}, options = {}) ⇒ Base Also known as: geo
Adds a Boosts::FieldDecayBoost, which boosts a search result based on how close it is to a specified value. That value can be a date, time, number, or Types::GeoPoint
Required:
:field
:origin
or:lat
&:lng
combo:scale
146 147 148 149 150 151 152 153 154 |
# File 'lib/stretchy/clauses/boost_clause.rb', line 146 def near(params = {}, = {}) if params[:lat] || params[:latitude] || params[:lng] || params[:longitude] || params[:lon] params[:origin] = Stretchy::Types::GeoPoint.new(params) end base.boost_builder.add_boost Stretchy::Boosts::FieldDecayBoost.new(params) Base.new(base) end |
#not(*args) ⇒ Object
87 88 89 |
# File 'lib/stretchy/clauses/boost_clause.rb', line 87 def not(*args) raise Errors::InvalidQueryError.new("Cannot call .not directly after boost - use .where.not or .match.not instead") end |
#random(*args) ⇒ Base
Adds a Boosts::RandomBoost to the query, for slightly randomizing search results.
167 168 169 170 |
# File 'lib/stretchy/clauses/boost_clause.rb', line 167 def random(*args) base.boost_builder.functions << Stretchy::Boosts::RandomBoost.new(*args) Base.new(base) end |
#score_mode(mode) ⇒ self
Set scoring mode for when a document matches multiple boost functions.
201 202 203 204 |
# File 'lib/stretchy/clauses/boost_clause.rb', line 201 def score_mode(mode) base.boost_builder.score_mode = mode self end |
#where(params = {}, options = {}) ⇒ BoostWhereClause Also known as: filter
Changes query state to "where" in the context of boosting. Works the same way as WhereClause, but applies the generated filters as a boost function.
51 52 53 |
# File 'lib/stretchy/clauses/boost_clause.rb', line 51 def where(params = {}, = {}) BoostWhereClause.new(base).boost_where(params, ) end |