Class: Stretchy::AndCollector

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/stretchy/and_collector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(nodes, context = {}) ⇒ AndCollector



11
12
13
14
# File 'lib/stretchy/and_collector.rb', line 11

def initialize(nodes, context = {})
  @nodes    = nodes
  @context  = context
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



9
10
11
# File 'lib/stretchy/and_collector.rb', line 9

def context
  @context
end

#nodesObject (readonly)

Returns the value of attribute nodes.



9
10
11
# File 'lib/stretchy/and_collector.rb', line 9

def nodes
  @nodes
end

Instance Method Details

#boost_nodesObject



72
73
74
75
76
# File 'lib/stretchy/and_collector.rb', line 72

def boost_nodes
  @boost_nodes ||= collect_nodes nodes do |n|
    n.context?(:boost)
  end
end

#context?(*args) ⇒ Boolean



20
21
22
# File 'lib/stretchy/and_collector.rb', line 20

def context?(*args)
  args.all? {|c| !!context[c] }
end

#filter_nodeObject



44
45
46
47
48
49
50
# File 'lib/stretchy/and_collector.rb', line 44

def filter_node
  @filter_node ||= if query_nodes.any? || boost_nodes.any?
    Node.new({query: filtered_query_node.json}, context)
  else
    Node.new(compile_nodes(filter_nodes).json, context)
  end
end

#filter_nodesObject



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/stretchy/and_collector.rb', line 52

def filter_nodes
  @filter_nodes ||= begin
    node_arr = collect_nodes nodes do |n|
      n.context?(:filter) &&
      !n.context?(:query) &&
      !n.context?(:boost)
    end
    node_arr += Array(compile_query_filter_node)
    node_arr.compact
  end
end

#nodeObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/stretchy/and_collector.rb', line 24

def node
  @node ||= if boost_nodes.any?
    function_score_node
  elsif filter_nodes.any?
    filtered_query_node
  elsif query_nodes.any?
    single_query_node
  else
    Node.new({match_all: {}}, context)
  end
end

#query_filter_nodesObject



64
65
66
67
68
69
70
# File 'lib/stretchy/and_collector.rb', line 64

def query_filter_nodes
  @query_filter_nodes ||= collect_nodes nodes do |n|
    n.context?(:filter) &&
    n.context?(:query)  &&
    !n.context?(:boost)
  end
end

#query_nodesObject



36
37
38
39
40
41
42
# File 'lib/stretchy/and_collector.rb', line 36

def query_nodes
  @query_nodes ||= collect_nodes nodes do |n|
    n.context?(:query)  &&
    !n.context?(:boost) &&
    !n.context?(:filter)
  end
end

#with_context(new_context) ⇒ Object



16
17
18
# File 'lib/stretchy/and_collector.rb', line 16

def with_context(new_context)
  self.class.new nodes, new_context
end