2016-10-10 108 views
0

我表示由人員發送的SMS文本消息。爲了簡化問題,我只有2個節點(一個人,一個電話)和3個關係(該人有一部電話並向他自己發送了兩條短信)。該圖被創建如下:Cypher查詢不起作用

try (Transaction tx = graphDb.beginTx()) 
{ 
Node aNode1 = graphDb.createNode(); 
aNode1.addLabel(DynamicLabel.label("Person")); 
aNode1.setProperty("Name", "Juana"); 
tx.success(); 

Node aNode2 = graphDb.createNode(); 
aNode2.addLabel(DynamicLabel.label("Phone")); 
aNode2.setProperty("Number", "1111-1111"); 
tx.success(); 

// (:Person) -[:has]->(:Phone) 
aNode1.createRelationshipTo(aNode2, RelationshipType.withName("has")); 
tx.success(); 


// finally SMS text sent at different moments 
// (:Phone) -[:sms]-> (:Phone) 
Relationship rel1 = aNode2.createRelationshipTo(aNode2, RelationshipType.withName("sms")); 
rel1.setProperty("Length", 100); 
tx.success(); 

Relationship rel2 = aNode2.createRelationshipTo(aNode2, RelationshipType.withName("sms")); 
rel2.setProperty("Length", 50); 
tx.success(); 

} 

當我執行下面的Cypher查詢:

MATCH (p1 :Person)-[:has]-> (n1 :Phone) -[r :sms]-(n2: Phone)<-[:has]-(p2 :Person) 
RETURN p1, p2 

我獲得零元組。我不明白結果集,因爲我必須在p1和p2之間短信(在這種情況下是同一個人)。

出人意料的是,如果我消除了查詢節點P2:

MATCH (p1 :Person)-[:has]-> (n1 :Phone) -[r :sms]-(n2: Phone) 
RETURN p1 

我獲得胡安娜,符合市場預期。我不明白我的第一個查詢的結果集(零元組)。

回答

0

當試圖遍歷路徑時,Cypher將只遍歷一個特定的關係,以避免無限循環。您在查詢中已經遍歷了(p1:Person) - [:HAS] - > (n1:Phone)關係,因此您無法從手機中找回Juana。但是,這不適用於節點,這就是爲什麼您可以從環形[:sms]關係中獲得兩次電話的原因。