问题
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