Class: Stretchy::Clauses::BoostWhereClause

Inherits:
BoostClause show all
Defined in:
lib/stretchy/clauses/boost_where_clause.rb

Overview

Boosts documents that match certain filters. Most filters will be passed into Stretchy::Clauses::Base#initialize, but you can also use .range and .geo .

Author:

  • [atevans]

Instance Attribute Summary

Attributes inherited from Base

#base

Instance Method Summary collapse

Methods inherited from BoostClause

#all, #boost_mode, #field, #max, #near, #random, #score_mode

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

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



14
15
16
17
18
19
20
21
# File 'lib/stretchy/clauses/boost_where_clause.rb', line 14

def boost_where(params = {}, options = {})
  weight = params.delete(:weight) || options[:weight]
  options[:inverse] = true if inverse?
  clause = WhereClause.new.where(params, options)
  boost  = clause.to_boost(weight)
  base.boost_builder.add_boost(boost) if boost
  self
end

#geo(field, options = {}) ⇒ Base

Boosts a document if it matches a geo filter. This is different than Stretchy::Clauses::BoostClause#near - while .near applies a decay function that boosts based on how close a field is to a geo point, .geo applies a filter that either boosts or doesn't boost the document.

See Also:

  • Stretchy::Clauses::BoostWhereClause.{WhereFunction{WhereFunction#geo}
  • Stretchy::Clauses::BoostWhereClause.{Filters{Filters::GeoFilter}
  • Elastic Docs - Geo Distance Filter


99
100
101
102
103
104
105
106
107
# File 'lib/stretchy/clauses/boost_where_clause.rb', line 99

def geo(field, options = {})
  weight = options[:weight]
  options[:inverse] = true if inverse?
  
  clause = WhereClause.new.geo(field, options)
  boost  = clause.to_boost(weight)
  base.boost_builder.add_boost(boost) if boost
  Base.new(base)
end

#match(*args) ⇒ MatchClause

Returns to the base context. Queries passed here will be used to filter documents.

Examples:

Returning to base context

query.boost.where(number_field: 89).match('username')

Staying in boost context

query.boost.where(number_field: 89).boost.match('love')

See Also:

  • Stretchy::Clauses::BoostWhereClause.{MatchClause{MatchClause#initialize}


58
59
60
# File 'lib/stretchy/clauses/boost_where_clause.rb', line 58

def match(*args)
  MatchClause.new(base).match(*args)
end

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



40
41
42
43
# File 'lib/stretchy/clauses/boost_where_clause.rb', line 40

def not(params = {}, options = {})
  @inverse = true
  boost_where(params, options)
end

#range(field, options = {}) ⇒ Base

Applies a range filter with a min or max as a boost.

See Also:

  • Stretchy::Clauses::BoostWhereClause.{WhereClause{WhereClause#range}
  • Stretchy::Clauses::BoostWhereClause.{Filters{Filters::RangeFilter}
  • Elastic Guides - Ranges


73
74
75
76
77
78
79
80
81
82
# File 'lib/stretchy/clauses/boost_where_clause.rb', line 73

def range(field, options = {})
  weight = options[:weight]
  options[:inverse] = true if inverse?
  
  clause = WhereClause.new.range(field, options)
  boost  = clause.to_boost(weight)
  base.boost_builder.add_boost(boost) if boost
  
  Base.new(base)
end

#where(*args) ⇒ WhereClause

Returns to the base context; filters passed here will be used to filter documents.

Examples:

Returning to base context

query.boost.where(number_field: 33).where(other_field: 64)

Staying in boost context

query.boost.where(number_field: 33).boost.where(other_field: 99)

See Also:

  • Stretchy::Clauses::BoostWhereClause.{WhereClause{WhereClause#initialize}


36
37
38
# File 'lib/stretchy/clauses/boost_where_clause.rb', line 36

def where(*args)
  WhereClause.new(base).where(*args)
end