class Stretchy::AndCollector

Attributes

context[R]
nodes[R]

Public Class Methods

new(nodes, context = {}) click to toggle source
# File lib/stretchy/and_collector.rb, line 11
def initialize(nodes, context = {})
  @nodes    = nodes
  @context  = context
end

Public Instance Methods

boost_nodes() click to toggle source
# File lib/stretchy/and_collector.rb, line 38
def boost_nodes
  @boost_nodes ||= nodes.select {|n| n.context? :boost }
end
context?(*args) click to toggle source
# File lib/stretchy/and_collector.rb, line 20
def context?(*args)
  args.all? {|c| !!context[c] }
end
function_score_node?() click to toggle source
# File lib/stretchy/and_collector.rb, line 42
def function_score_node?
  boost_nodes.reject { |n| n.empty? }.any?
end
node() click to toggle source
# File lib/stretchy/and_collector.rb, line 24
def node
  @node ||= if function_score_node?
    function_score_node
  elsif query_nodes.any?
    query_node
  else
    Node.new({match_all: {}}, context)
  end
end
query_nodes() click to toggle source
# File lib/stretchy/and_collector.rb, line 34
def query_nodes
  @query_nodes ||= nodes.reject {|n| n.context? :boost }
end
with_context(new_context) click to toggle source
# File lib/stretchy/and_collector.rb, line 16
def with_context(new_context)
  self.class.new nodes, new_context
end

Private Instance Methods

bool_ctx() click to toggle source
# File lib/stretchy/and_collector.rb, line 69
def bool_ctx
  [:filter, :must_not, :should]
end
compile_bool(bool_nodes) click to toggle source
# File lib/stretchy/and_collector.rb, line 60
def compile_bool(bool_nodes)
  split_nodes = split_nodes_for_bool(bool_nodes)
  refined = bool_ctx.each_with_object(split_nodes) do |k, hash|
    hash[k] = Array(compile_bool(hash[k])) if multicontext? hash[k]
  end
  bool_json = Hash[refined.map{|k,v| [k, v.map(&:as_json)] }]
  Node.new(bool: bool_json)
end
compile_boost_functions() click to toggle source
# File lib/stretchy/and_collector.rb, line 81
def compile_boost_functions
  boost_nodes.map do |n|
    next if n.empty?
    n.json
  end.compact
end
compile_function_score_options() click to toggle source
# File lib/stretchy/and_collector.rb, line 88
def compile_function_score_options
  boost_nodes.reduce({}) do |options, node|
    options.merge(node.context[:fn_score] || {})
  end
end
function_score_node() click to toggle source
# File lib/stretchy/and_collector.rb, line 94
def function_score_node
  function_score_json = compile_function_score_options
  function_score_json[:functions] = compile_boost_functions
  function_score_json[:query] = query_node.json if query_nodes.any?

  Node.new({function_score: function_score_json}, context)
end
multicontext?(node_arr) click to toggle source
# File lib/stretchy/and_collector.rb, line 56
def multicontext?(node_arr)
  Array(node_arr).any? {|n| n.context?(:must_not) || n.context?(:should) }
end
query_node() click to toggle source
# File lib/stretchy/and_collector.rb, line 48
def query_node
  if query_nodes.size > 1 || multicontext?(query_nodes)
    compile_bool query_nodes
  else
    query_nodes.first
  end
end
split_nodes_for_bool(bool_nodes) click to toggle source
# File lib/stretchy/and_collector.rb, line 73
def split_nodes_for_bool(bool_nodes)
  bool_nodes.each_with_object({}) do |n, hash|
    key = bool_ctx.find{|c| n.context? c } || :must
    hash[key] ||= []
    hash[key] << Node.new(n.json, n.context.merge(key => nil))
  end
end