Class: Stretchy::Builders::MatchBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/stretchy/builders/match_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMatchBuilder

Returns a new instance of MatchBuilder.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/stretchy/builders/match_builder.rb', line 10

def initialize
  @matches           = Hash.new { [] }
  @matchops          = Hash.new { 'and' }
  
  @antimatches       = Hash.new { [] }
  @antimatchops      = Hash.new { 'and' }
  
  @shouldmatches     = Hash.new { [] }
  @shouldmatchops    = Hash.new { 'and' }
  
  @shouldnotmatches  = Hash.new { [] }
  @shouldnotmatchops = Hash.new { 'and' }
end

Instance Attribute Details

#antimatchesObject

Returns the value of attribute antimatches.



5
6
7
# File 'lib/stretchy/builders/match_builder.rb', line 5

def antimatches
  @antimatches
end

#antimatchopsObject

Returns the value of attribute antimatchops.



5
6
7
# File 'lib/stretchy/builders/match_builder.rb', line 5

def antimatchops
  @antimatchops
end

#matchesObject

Returns the value of attribute matches.



5
6
7
# File 'lib/stretchy/builders/match_builder.rb', line 5

def matches
  @matches
end

#matchopsObject

Returns the value of attribute matchops.



5
6
7
# File 'lib/stretchy/builders/match_builder.rb', line 5

def matchops
  @matchops
end

#shouldmatchesObject

Returns the value of attribute shouldmatches.



5
6
7
# File 'lib/stretchy/builders/match_builder.rb', line 5

def shouldmatches
  @shouldmatches
end

#shouldmatchopsObject

Returns the value of attribute shouldmatchops.



5
6
7
# File 'lib/stretchy/builders/match_builder.rb', line 5

def shouldmatchops
  @shouldmatchops
end

#shouldnotmatchesObject

Returns the value of attribute shouldnotmatches.



5
6
7
# File 'lib/stretchy/builders/match_builder.rb', line 5

def shouldnotmatches
  @shouldnotmatches
end

#shouldnotmatchopsObject

Returns the value of attribute shouldnotmatchops.



5
6
7
# File 'lib/stretchy/builders/match_builder.rb', line 5

def shouldnotmatchops
  @shouldnotmatchops
end

Instance Method Details

#any?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/stretchy/builders/match_builder.rb', line 24

def any?
  @matches.any? || @antimatches.any? || @shouldmatches.any? || @shouldnotmatches.any?
end

#bool_queryObject



40
41
42
43
44
45
46
# File 'lib/stretchy/builders/match_builder.rb', line 40

def bool_query
  Stretchy::Queries::BoolQuery.new(
    must:     to_queries(@matches, @matchops),
    must_not: to_queries(@antimatches, @antimatchops),
    should:   build_should
  )
end

#buildObject



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/stretchy/builders/match_builder.rb', line 28

def build
  return Stretchy::Queries::MatchAllQuery.new unless any?

  if @matches.count > 1   || @antimatches.any? || 
     @shouldmatches.any?  || @shouldnotmatches.any?
    
    bool_query
  else
    to_queries(@matches, @matchops).first
  end
end

#build_shouldObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/stretchy/builders/match_builder.rb', line 48

def build_should
  if @shouldnotmatches.any?
    Stretchy::Queries::BoolQuery.new(
      must:     to_queries(@shouldmatches, @shouldmatchops),
      must_not: to_queries(@shouldnotmatches, @shouldnotmatchops)
    )
  else
    to_queries(@shouldmatches, @shouldmatchops)
  end
end