Search code examples
neo4jcyphergraph-traversal

Translating from Cypher query to traversal framework for neo4j


I have this traversal:

    TraversalDescription td = graph.traversalDescription()
            .depthFirst()
            .relationships(RelationshipTypes.CHILD, Direction.INCOMING)
            .evaluator(new Evaluator() {
                @Override
                public Evaluation evaluate(Path path) {
                    Node node = path.endNode();

                    if (node.hasLabel(NodeTypes.Task) && rootTaskNodeIds.contains(node.getId()))
                        return Evaluation.INCLUDE_AND_PRUNE;
                    else
                        return Evaluation.EXCLUDE_AND_CONTINUE;
                }
            });
    ResourceIterator<Path> it = td.traverse(taskNodes).iterator();
    while (it.hasNext())
            Node resNode = it.next().startNode();

And this Cypher query:

MATCH (c:Task)
    <-[:CHILD*0..]-(p:Task)
WHERE ID(c) in [${taskNodeIds}]
    AND ID(p) in [${rootTaskNodeIds}]
RETURN c

I cannot figure out why do they return different results?


Solution

  • In Cypher one you start at p and return c (not p!), so the relationship is outgoing but in the Traversal you use an incoming relationship. Perhaps you return the wrong node in Cypher?

    Also cypher has rel-uniqueness per path which you haven't configured on the TraversalDescription.

    And you might only want to access the nodes not the path from the traversal description.