Neo4j Cypher match () in java. Find connected nodes

蹲街弑〆低调 提交于 2019-12-11 10:29:07

问题


I have the follwing structure

        firstNode = graphDb.createNode();
        firstNode.setProperty( "person", "Andy " ); 
        Label myLabel = DynamicLabel.label("A");
        firstNode.addLabel(myLabel);
        secondNode = graphDb.createNode();
        secondNode.setProperty( "person", "Bobby" );
        Label myLabel1 = DynamicLabel.label("B");
        secondNode.addLabel(myLabel1);
        ThirdNode = graphDb.createNode();
        ThirdNode.setProperty( "person", "Chris " );
        Label myLabel2 = DynamicLabel.label("C");
        ThirdNode.addLabel(myLabel2);....

        relationship = firstNode.createRelationshipTo( secondNode, RelTypes.emails );
        relationship.setProperty( "relationship", "email " );
        relationship = firstNode.createRelationshipTo( ThirdNode, RelTypes.emails );
        relationship.setProperty( "relationship", "email " );
        relationship = secondNode.createRelationshipTo( ThirdNode, RelTypes.emails );
        relationship.setProperty( "relationship", "email " );
        relationship = secondNode.createRelationshipTo( FourthNode, RelTypes.emails );
        relationship.setProperty( "relationship", "email " );

firstNode is linked to second and third by the relation "emails". Similarly, second node is connected to third, fourth, first.

I want for each node output somethinglike this: secondNode=[firstNode, FouthNode, ThirdNode], firstNode=[second, third], third=...

I tried something like this:

try{
        ExecutionEngine engine = new ExecutionEngine(graphDb);
        ExecutionResult result = engine.execute("MATCH (secondNode{person:'Bobby'})<-[:emails]-(node)RETURN node");

        System.out.println(result.dumpToString());
        tx1.success();
    } 

I got the output :Node[0]{person:"Andy "}

Im am very new to cypher. How to write match statement for this? Is this possible?


回答1:


  • Your label should be something like :Person not :A, :B, :C
  • You want to aggregate by your first node.
  • You should use uppercase re-types

try something like this:

MATCH (sender:Person)-[:EMAILS]->(receiver) 
RETURN sender,collect(receiver) as receivers


来源:https://stackoverflow.com/questions/34677104/neo4j-cypher-match-in-java-find-connected-nodes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!