2016-01-08 75 views
1

我有follwing結構Neo4j Cypher match()in java。查找連接節點

 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被鏈接到第二和第三的關係「電子郵件」。同樣,第二個節點連接到第三,第四,第一個。

我要爲每個節點輸出somethinglike此:secondNode = [firstNode,FouthNode,ThirdNode],firstNode = [第二,第三],第三= ...

我嘗試是這樣的:

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(); 
    } 

我得到了輸出:Node[0]{person:"Andy "}

我對密碼很陌生。如何爲此編寫匹配語句?這可能嗎?

+0

不要創建一個新的執行引擎使用graphDb.execute! –

回答

1
  • 你的標籤應該是這樣的:人不是:A,:B,:C
  • 你想你的第一個節點聚集。
  • 您應該使用大寫字母重新類型

嘗試這樣的事:

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

謝謝。完善!有效。但我沒有指定方向。 – priya

+0

我在此基礎上提出了後續問題。你能檢查一下嗎? [鏈接](http://stackoverflow.com/questions/34693776/collecting-the-result-of-cypher-query-into-a-hash-map-java) – priya