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

Returns a new instance of 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



82
83
84
85
86
# File 'lib/stretchy/and_collector.rb', line 82

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

#context?(*args) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#filter_jsonObject



58
59
60
# File 'lib/stretchy/and_collector.rb', line 58

def filter_json
  filter_node.json
end

#filter_nodeObject



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/stretchy/and_collector.rb', line 44

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

#filter_nodesObject



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

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



74
75
76
77
78
79
80
# File 'lib/stretchy/and_collector.rb', line 74

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