class Elasticsearch::API::Response::ExplainTrimmer

Public Class Methods

new() click to toggle source
# File lib/elasticsearch/api/response/explain_trimmer.rb, line 5
def initialize
end

Public Instance Methods

trim(tree) click to toggle source
# File lib/elasticsearch/api/response/explain_trimmer.rb, line 8
def trim(tree)
  trim_node(tree)
end

Private Instance Methods

merge_function_score_node(current, boost, target) click to toggle source
# File lib/elasticsearch/api/response/explain_trimmer.rb, line 96
def merge_function_score_node(current, boost, target)
  entity = boost.field == "*" || boost.field.nil? ? target : boost

  ExplainNode.new(
    score: current.score,
    level: current.level,
    details: current.details,
    description: Description.new(
      raw: current.description.raw,
      type: entity.type,
      operator: entity.operator,
      operation: entity.operation,
      field: entity.field,
      value: entity.value
    )
  )
end
trim_children_node(node) click to toggle source
# File lib/elasticsearch/api/response/explain_trimmer.rb, line 40
def trim_children_node(node)
  case node.children.size
  when 1
    node.children = []
    trim_node(node)
  else
    trim_default_node(node)
  end
end
trim_default_node(node) click to toggle source
# File lib/elasticsearch/api/response/explain_trimmer.rb, line 83
def trim_default_node(node)
  if node.has_children?
    node.children = node.children.map(&method(:trim_node)).compact
  end
  node
end
trim_func_score_node(node) click to toggle source
# File lib/elasticsearch/api/response/explain_trimmer.rb, line 65
def trim_func_score_node(node)
  case node.children.size
  when 2
    boost = node.children.find { |n| n.match? || n.query_boost? }
    if boost
      other = (node.children - [boost])[0]
      if boost.score_one? && other.score == node.score
        other = trim_node(other) if other.has_children?
        new_node = merge_function_score_node(node, boost, other)
        new_node.children = other.children
        return trim_node(new_node)
      end
    end
  end

  trim_default_node(node)
end
trim_min_score_node(node) click to toggle source

@note show only the node with a minimum score

# File lib/elasticsearch/api/response/explain_trimmer.rb, line 91
def trim_min_score_node(node)
  child = node.children.find {|n| n.score == node.score }
  trim_node(child)
end
trim_node(node) click to toggle source
# File lib/elasticsearch/api/response/explain_trimmer.rb, line 14
def trim_node(node)
  case
  when node.product?
    trim_product_node(node)
  when node.score?
    trim_score_node(node)
  when node.func?, node.boost?
    trim_children_node(node)
  when node.func_score?
    trim_func_score_node(node)
  when node.min?
    trim_min_score_node(node)
  else
    trim_default_node(node)
  end
end
trim_product_node(node) click to toggle source
# File lib/elasticsearch/api/response/explain_trimmer.rb, line 50
def trim_product_node(node)
  case node.children.size
  when 2
    constant = node.children.find { |n| n.constant? }
    if constant
      other = (node.children - [constant])[0]
      if constant.score_one? && other.score == node.score
        return trim_node(other)
      end
    end
  end

  trim_default_node(node)
end
trim_score_node(node) click to toggle source
# File lib/elasticsearch/api/response/explain_trimmer.rb, line 31
def trim_score_node(node)
  case node.children.size
  when 1
    return trim_node(node.children.first)
  else
    trim_default_node(node)
  end
end