Class: Stretchy::Clauses::Base

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/stretchy/clauses/base.rb

Direct Known Subclasses

BoostClause, MatchClause, WhereClause

Constant Summary collapse

DEFAULT_LIMIT =
30
DEFAULT_OFFSET =
0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_or_opts = nil, options = {}) ⇒ Base

Returns a new instance of Base.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/stretchy/clauses/base.rb', line 17

def initialize(base_or_opts = nil, options = {})
  if base_or_opts && !base_or_opts.is_a?(Hash)
    base                = base_or_opts
    @index_name         = base.index_name
    @type               = base.type
    @match_builder      = base.match_builder
    @where_builder      = base.where_builder
    @boost_builder      = base.boost_builder
    @aggregate_builder  = base.aggregate_builder
    @inverse            = options[:inverse] || base.inverse
    @limit              = base.get_limit
    @offset             = base.get_offset
  else
    options = Hash(base_or_opts).merge(options)
    @index_name         = options[:index] || Stretchy.index_name
    @type               = options[:type]
    @match_builder      = Stretchy::Builders::MatchBuilder.new
    @where_builder      = Stretchy::Builders::WhereBuilder.new
    @boost_builder      = Stretchy::Builders::BoostBuilder.new
    @aggregate_builder  = nil
    @inverse            = options[:inverse]
    @limit              = DEFAULT_LIMIT
    @offset             = DEFAULT_OFFSET
  end
end

Instance Attribute Details

#aggregate_builderObject

Returns the value of attribute aggregate_builder.



10
11
12
# File 'lib/stretchy/clauses/base.rb', line 10

def aggregate_builder
  @aggregate_builder
end

#boost_builderObject

Returns the value of attribute boost_builder.



10
11
12
# File 'lib/stretchy/clauses/base.rb', line 10

def boost_builder
  @boost_builder
end

#index_nameObject

Returns the value of attribute index_name.



10
11
12
# File 'lib/stretchy/clauses/base.rb', line 10

def index_name
  @index_name
end

#inverseObject

Returns the value of attribute inverse.



10
11
12
# File 'lib/stretchy/clauses/base.rb', line 10

def inverse
  @inverse
end

#match_builderObject

Returns the value of attribute match_builder.



10
11
12
# File 'lib/stretchy/clauses/base.rb', line 10

def match_builder
  @match_builder
end

#typeObject

Returns the value of attribute type.



10
11
12
# File 'lib/stretchy/clauses/base.rb', line 10

def type
  @type
end

#where_builderObject

Returns the value of attribute where_builder.



10
11
12
# File 'lib/stretchy/clauses/base.rb', line 10

def where_builder
  @where_builder
end

Instance Method Details

#boost(options = {}) ⇒ Object



71
72
73
# File 'lib/stretchy/clauses/base.rb', line 71

def boost(options = {})
  BoostClause.new(self, options)
end

#get_limitObject



48
49
50
# File 'lib/stretchy/clauses/base.rb', line 48

def get_limit
  @limit
end

#get_offsetObject



57
58
59
# File 'lib/stretchy/clauses/base.rb', line 57

def get_offset
  @offset
end

#inverse?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/stretchy/clauses/base.rb', line 91

def inverse?
  !!@inverse
end

#limit(num) ⇒ Object



43
44
45
46
# File 'lib/stretchy/clauses/base.rb', line 43

def limit(num)
  @limit = num
  self
end

#match(options = {}) ⇒ Object Also known as: fulltext



61
62
63
# File 'lib/stretchy/clauses/base.rb', line 61

def match(options = {})
  MatchClause.new(self, options)
end

#not(opts_or_string = {}, opts = {}) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/stretchy/clauses/base.rb', line 75

def not(opts_or_string = {}, opts = {})
  if opts_or_string.is_a?(Hash)
    WhereClause.new(self, opts_or_string.merge(inverse: true))
  else
    MatchClause.new(self, opts_or_string, opts.merge(inverse: true))
  end
end

#offset(num) ⇒ Object



52
53
54
55
# File 'lib/stretchy/clauses/base.rb', line 52

def offset(num)
  @offset = num
  self
end

#query_resultsObject



111
112
113
# File 'lib/stretchy/clauses/base.rb', line 111

def query_results
  @query_results ||= Stretchy::Results::Base.new(self)
end

#should(opts_or_string = {}, opts = {}) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/stretchy/clauses/base.rb', line 83

def should(opts_or_string = {}, opts = {})
  if opts_or_string.is_a?(Hash)
    WhereClause.new(self, opts_or_string.merge(should: true))
  else
    MatchClause.new(self, opts_or_string, opts.merge(should: true))
  end
end

#to_searchObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/stretchy/clauses/base.rb', line 95

def to_search
  return @to_search if @to_search
  
  @to_search = if @where_builder.any?
    Stretchy::Queries::FilteredQuery.new(
      query:  @match_builder.build,
      filter: @where_builder.build
    )
  else
    @match_builder.build
  end

  @to_search = @boost_builder.build(@to_search) if @boost_builder.any?
  @to_search = @to_search.to_search
end

#where(options = {}) ⇒ Object Also known as: filter



66
67
68
# File 'lib/stretchy/clauses/base.rb', line 66

def where(options = {})
  WhereClause.new(self, options)
end