Class: Stretchy::AndCollector
- Inherits:
-
Object
- Object
- Stretchy::AndCollector
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/stretchy/and_collector.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
Instance Method Summary collapse
- #boost_nodes ⇒ Object
- #context?(*args) ⇒ Boolean
- #filter_json ⇒ Object
- #filter_node ⇒ Object
- #filter_nodes ⇒ Object
-
#initialize(nodes, context = {}) ⇒ AndCollector
constructor
A new instance of AndCollector.
- #node ⇒ Object
- #query_filter_nodes ⇒ Object
- #query_nodes ⇒ Object
- #with_context(new_context) ⇒ Object
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
#context ⇒ Object (readonly)
Returns the value of attribute context.
9 10 11 |
# File 'lib/stretchy/and_collector.rb', line 9 def context @context end |
#nodes ⇒ Object (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_nodes ⇒ Object
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
20 21 22 |
# File 'lib/stretchy/and_collector.rb', line 20 def context?(*args) args.all? {|c| !!context[c] } end |
#filter_json ⇒ Object
58 59 60 |
# File 'lib/stretchy/and_collector.rb', line 58 def filter_json filter_node.json end |
#filter_node ⇒ Object
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_nodes ⇒ Object
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 |
#node ⇒ Object
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_nodes ⇒ Object
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_nodes ⇒ Object
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 |