Class: Elastic::Nodes::Boolean

Inherits:
Base
  • Object
show all
Includes:
Concerns::Boostable
Defined in:
lib/elastic/nodes/boolean.rb

Instance Attribute Summary collapse

Attributes included from Concerns::Boostable

#boost

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==, #handle_result

Methods included from Support::Traversable

#pick_nodes

Constructor Details

#initializeBoolean

Returns a new instance of Boolean.



15
16
17
18
19
20
21
22
# File 'lib/elastic/nodes/boolean.rb', line 15

def initialize
  super
  @musts = []
  @must_nots = []
  @shoulds = []
  @filters = []
  @disable_coord = !Elastic.config.coord_similarity
end

Instance Attribute Details

#disable_coordObject

Returns the value of attribute disable_coord.



13
14
15
# File 'lib/elastic/nodes/boolean.rb', line 13

def disable_coord
  @disable_coord
end

#minimum_should_matchObject

Returns the value of attribute minimum_should_match.



13
14
15
# File 'lib/elastic/nodes/boolean.rb', line 13

def minimum_should_match
  @minimum_should_match
end

Class Method Details

.build_and(_nodes) ⇒ Object



5
6
7
# File 'lib/elastic/nodes/boolean.rb', line 5

def self.build_and(_nodes)
  new.tap { |n| n.musts = _nodes }
end

.build_or(_nodes) ⇒ Object



9
10
11
# File 'lib/elastic/nodes/boolean.rb', line 9

def self.build_or(_nodes)
  new.tap { |n| n.shoulds = _nodes }
end

Instance Method Details

#cloneObject



91
92
93
94
95
96
97
98
99
# File 'lib/elastic/nodes/boolean.rb', line 91

def clone
  prepare_clone(
    super,
    @musts.map(&:clone),
    @must_nots.map(&:clone),
    @shoulds.map(&:clone),
    @filters.map(&:clone)
  )
end

#filter(_node) ⇒ Object



36
37
38
# File 'lib/elastic/nodes/boolean.rb', line 36

def filter(_node)
  @filters << _node
end

#filtersObject



68
69
70
# File 'lib/elastic/nodes/boolean.rb', line 68

def filters
  @filters.each
end

#filters=(_nodes) ⇒ Object



64
65
66
# File 'lib/elastic/nodes/boolean.rb', line 64

def filters=(_nodes)
  @filters = _nodes.dup.to_a
end

#must(_node) ⇒ Object



24
25
26
# File 'lib/elastic/nodes/boolean.rb', line 24

def must(_node)
  @musts << _node
end

#must_not(_node) ⇒ Object



28
29
30
# File 'lib/elastic/nodes/boolean.rb', line 28

def must_not(_node)
  @must_nots << _node
end

#must_notsObject



52
53
54
# File 'lib/elastic/nodes/boolean.rb', line 52

def must_nots
  @must_nots.each
end

#must_nots=(_nodes) ⇒ Object



48
49
50
# File 'lib/elastic/nodes/boolean.rb', line 48

def must_nots=(_nodes)
  @must_nots = _nodes.dup.to_a
end

#mustsObject



44
45
46
# File 'lib/elastic/nodes/boolean.rb', line 44

def musts
  @musts.each
end

#musts=(_nodes) ⇒ Object



40
41
42
# File 'lib/elastic/nodes/boolean.rb', line 40

def musts=(_nodes)
  @musts = _nodes.dup.to_a
end

#render(_options = {}) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/elastic/nodes/boolean.rb', line 78

def render(_options = {})
  hash = {}
  hash['must'] = @musts.map { |n| n.render(_options) } if !@musts.empty?
  hash['must_not'] = @must_nots.map { |n| n.render(_options) } if !@must_nots.empty?
  hash['should'] = @shoulds.map { |n| n.render(_options) } if !@shoulds.empty?
  hash['filters'] = @filters.map { |n| n.render(_options) } if !@filters.empty?
  hash['minimum_should_match'] = minimum_should_match unless minimum_should_match.nil?
  hash['disable_coord'] = true if disable_coord
  render_boost(hash)

  { "bool" => hash }
end

#should(_node) ⇒ Object



32
33
34
# File 'lib/elastic/nodes/boolean.rb', line 32

def should(_node)
  @shoulds << _node
end

#shouldsObject



60
61
62
# File 'lib/elastic/nodes/boolean.rb', line 60

def shoulds
  @shoulds.each
end

#shoulds=(_nodes) ⇒ Object



56
57
58
# File 'lib/elastic/nodes/boolean.rb', line 56

def shoulds=(_nodes)
  @shoulds = _nodes.dup.to_a
end

#simplifyObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/elastic/nodes/boolean.rb', line 101

def simplify
  new_must = @musts.map(&:simplify)
  new_must_not = @must_nots.map(&:simplify)
  new_should = @shoulds.map(&:simplify)
  new_filter = @filters.map(&:simplify)

  # TODO: detect must elements with boost = 0 and move them to "filter"

  total_nodes = new_must.length + new_must_not.length + new_should.length + new_filter.length
  if boost.nil? && total_nodes == 1
    return new_must.first if !new_must.empty?
    return new_should.first if !new_should.empty? # at least 1 should match
  end

  prepare_clone(super, new_must, new_must_not, new_should, new_filter)
end

#traverse(&_block) ⇒ Object



72
73
74
75
76
# File 'lib/elastic/nodes/boolean.rb', line 72

def traverse(&_block)
  super
  @shoulds.each { |c| c.traverse(&_block) }
  @musts.each { |c| c.traverse(&_block) }
end