Class: Stretchy::Clauses::WhereClause

Inherits:
Base
  • Object
show all
Defined in:
lib/stretchy/clauses/where_clause.rb

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

Instance Method Summary collapse

Methods inherited from Base

#boost, #get_limit, #get_offset, #inverse?, #limit, #match, #offset, #query_results, #to_search, #where

Constructor Details

#initialize(base, options = {}) ⇒ WhereClause

Returns a new instance of WhereClause.



11
12
13
14
15
16
# File 'lib/stretchy/clauses/where_clause.rb', line 11

def initialize(base, options = {})
  super(base)
  @inverse = options.delete(:inverse)
  @should  = options.delete(:should)
  add_params(options)
end

Class Method Details

.tmp(options = {}) ⇒ Object



7
8
9
# File 'lib/stretchy/clauses/where_clause.rb', line 7

def self.tmp(options = {})
  self.new(Base.new, options)
end

Instance Method Details

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



27
28
29
30
31
32
33
# File 'lib/stretchy/clauses/where_clause.rb', line 27

def geo(field, options = {})
  get_storage(:geos)[field] = {
    distance:  options[:distance],
    geo_point: Stretchy::Types::GeoPoint.new(options)
  }
  self
end

#not(options = {}) ⇒ Object



35
36
37
# File 'lib/stretchy/clauses/where_clause.rb', line 35

def not(options = {})
  self.class.new(self, options.merge(inverse: true, should: should?))
end

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



22
23
24
25
# File 'lib/stretchy/clauses/where_clause.rb', line 22

def range(field, options = {})
  get_storage(:ranges)[field] = Stretchy::Types::Range.new(options)
  self
end

#should(options = {}) ⇒ Object



39
40
41
# File 'lib/stretchy/clauses/where_clause.rb', line 39

def should(options = {})
  self.class.new(self, options.merge(should: true))
end

#should?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/stretchy/clauses/where_clause.rb', line 18

def should?
  !!@should
end

#to_boost(weight = nil) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/stretchy/clauses/where_clause.rb', line 43

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