Class: Stretchy::Clauses::WhereClause
- Defined in:
- lib/stretchy/clauses/where_clause.rb
Overview
A Where clause inherits the same state as any clause,
but has a few possible states to transition to. You
can call #range and #geo to add their respective
filters, or you can transition to the inverted state
via #not, or the should state via #should
STATES:
- inverted: any filters added in this state will be inverted, ie the document must NOT match said filters.
- should: any filters added in this state will be
applied to a
shouldblock. Documents which do not match these filters will be returned, but documents which do match will have a higher relevance score.
Constant Summary
Constants inherited from Base
Base::DEFAULT_LIMIT, Base::DEFAULT_OFFSET
Instance Attribute Summary
Attributes inherited from Base
#aggregate_builder, #boost_builder, #index_name, #inverse, #match_builder, #type, #where_builder
Class Method Summary collapse
-
.tmp(options = {}) ⇒ WhereClause
Creates a temporary context by initializing a new Base object.
Instance Method Summary collapse
-
#geo(field, options = {}) ⇒ self
Adds a geo distance filter to the current context.
-
#initialize(base, options = {}) ⇒ WhereClause
constructor
Options passed to the initializer will be interpreted as filters to be added to the query.
-
#not(options = {}) ⇒ WhereClause
Switches current state to inverted.
-
#range(field, options = {}) ⇒ self
Add a range filter to the current context.
-
#should(options = {}) ⇒ WhereClause
Switches the current state to
should. -
#should? ⇒ true, false
Accessor for
@should. -
#to_boost(weight = nil) ⇒ Boosts::FilterBoost
Converts the current context into a boost to be passed into a FunctionScoreQuery.
Methods inherited from Base
#boost, #explain, #get_explain, #get_limit, #get_offset, #inverse?, #limit, #match, #offset, #query_results, #to_search, #where
Constructor Details
#initialize(base, options = {}) ⇒ WhereClause
Options passed to the initializer will be interpreted as filters
to be added to the query. This is similar to ActiveRecord's where
method.
61 62 63 64 65 66 |
# File 'lib/stretchy/clauses/where_clause.rb', line 61 def initialize(base, = {}) super(base) @inverse = .delete(:inverse) @should = .delete(:should) add_params() end |
Class Method Details
.tmp(options = {}) ⇒ WhereClause
Creates a temporary context by initializing a new Base object. Used primarily in BoostWhereClause
33 34 35 |
# File 'lib/stretchy/clauses/where_clause.rb', line 33 def self.tmp( = {}) self.new(Base.new, ) end |
Instance Method Details
#geo(field, options = {}) ⇒ self
Adds a geo distance filter to the current context.
Documents must have a geo_point field that is within
the specified distance of the passed parameters.
124 125 126 127 128 129 130 |
# File 'lib/stretchy/clauses/where_clause.rb', line 124 def geo(field, = {}) get_storage(:geos)[field] = { distance: [:distance], geo_point: Stretchy::Types::GeoPoint.new() } self end |
#not(options = {}) ⇒ WhereClause
Switches current state to inverted. Options passed here are equivalent to those passed to #initialize, except documents must not match these filters.
Can be chained with #should to produce inverted should queries
155 156 157 |
# File 'lib/stretchy/clauses/where_clause.rb', line 155 def not( = {}) self.class.new(self, .merge(inverse: true, should: should?)) end |
#range(field, options = {}) ⇒ self
Add a range filter to the current context. While
you can pass a Range object to Base#where, this
allows you to specify an open-ended range, such
as only specifying the minimum or maximum value.
98 99 100 101 |
# File 'lib/stretchy/clauses/where_clause.rb', line 98 def range(field, = {}) get_storage(:ranges)[field] = Stretchy::Types::Range.new() self end |
#should(options = {}) ⇒ WhereClause
Switches the current state to should. Options passed
here are equivalent to those passed to #initialize,
except documents which do not match are still returned
with a lower score than documents which do match.
Can be chained with #not to produce inverted should queries
180 181 182 |
# File 'lib/stretchy/clauses/where_clause.rb', line 180 def should( = {}) self.class.new(self, .merge(should: true)) end |
#should? ⇒ true, false
Accessor for @should
72 73 74 |
# File 'lib/stretchy/clauses/where_clause.rb', line 72 def should? !!@should end |
#to_boost(weight = nil) ⇒ Boosts::FilterBoost
Converts the current context into a boost to be passed into a FunctionScoreQuery.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/stretchy/clauses/where_clause.rb', line 191 def to_boost(weight = nil) weight ||= Stretchy::Boosts::FilterBoost::DEFAULT_WEIGHT if @match_builder.any? && @where_builder.any? Stretchy::Boosts::FilterBoost.new( filter: Stretchy::Filters::QueryFilter.new( Stretchy::Queries::FilteredQuery.new( query: @match_builder.build, filter: @where_builder.build ) ), weight: weight ) elsif @match_builder.any? Stretchy::Boosts::FilterBoost.new( filter: Stretchy::Filters::QueryFilter.new( @match_builder.build ), weight: weight ) elsif @where_builder.any? Stretchy::Boosts::FilterBoost.new( filter: @where_builder.build, weight: weight ) end end |