Class: Elasticfusion::Search::Query::Visitors::PolyadicTree

Inherits:
Elasticfusion::Search::Query::Visitor show all
Defined in:
lib/elasticfusion/search/query/visitors/polyadic_tree.rb

Overview

PolyadicTree is a base class for visitors that accept more than two operands for logical expressions. It converts a binary tree into multi-way tree, replacing all Expression nodes with PolyadicExpression nodes.

Given an AST:

  and
/     \

A and

 /     \
B      and
     /     \
    C       or
          /   \
         D     E

A polyadic representation would be:

    and
/  /   \   \

A B C or

 /  \
D    E

Direct Known Subclasses

Elasticsearch

Instance Method Summary collapse

Methods inherited from Elasticfusion::Search::Query::Visitor

#visit, visitor_method

Instance Method Details

#accept(node) ⇒ Object



35
36
37
# File 'lib/elasticfusion/search/query/visitors/polyadic_tree.rb', line 35

def accept(node)
  super(rewrite(node))
end

#rewrite(node, parent: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/elasticfusion/search/query/visitors/polyadic_tree.rb', line 39

def rewrite(node, parent: nil)
  case node
  when NegatedClause
    node.body = rewrite(node.body)
  when Expression
    flattened = [rewrite(node.left, parent: node),
                 rewrite(node.right, parent: node)].flatten

    if parent && node.op && node.op == parent.op
      return flattened
    else
      return PolyadicExpression.new(node.op, flattened)
    end
  end
  node
end