2016-10-10 104 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]关系中获得两次电话的原因。