检索节点A的朋友的朋友

时间:2014-02-15 08:50:45

标签: neo4j cypher

这是一张简单的社交地图。我正在寻找节点'A的朋友们的朋友。

CREATE (pa:Person { name: "A", from: "Sweden", learn: "surfing" }),
(pb:Person { name: "B", from: "London", title: "author" }),
(pc:Person { name: "C", from: "Paris" }),
(pd:Person { name: "D", from: "Pune" }),
(pe:Person { name: "E", from: "Bangalore" }),
(pf:Person { name: "F", from: "Chicago" }),
(pg:Person { name: "G", from: "Sudney" }),
(pa)-[:KNOWS {since: 2001}]->(pb),
(pa)-[:KNOWS {rating: 5}]->(pc),
(pa)-[:KNOWS]->(pd),
(pb)-[:KNOWS]->(pd),
(pb)-[:KNOWS]->(pe),
(pb)-[:KNOWS]->(pf),
(pc)-[:KNOWS]->(pd),
(pd)-[:KNOWS]->(pa),
(pd)-[:KNOWS]->(pg)

我运行了以下查询。

MATCH (m:Person)-[:KNOWS]->(n:Person)-[:KNOWS]->(o:Person)
WHERE m.name="A"
return o;

返回A,D,E,F,G

我期待只看到E,F,G节点。

你能帮忙吗,这里有什么问题?

1 个答案:

答案 0 :(得分:1)

它返回A,因为A - [:知识] - > D - [:知识] - > A
它返回D,因为A - [:知识] - > B - [:知识] - > D

您需要指定m<> n<> o和m不应该知道o。

MATCH (m:Person)-[:KNOWS]->(n:Person)-[:KNOWS]->(o:Person) 
WHERE m.name="A" AND m<>o AND NOT (m)-[:KNOWS]->(o)
RETURN DISTINCT o;

请参阅:http://console.neo4j.org/r/8i2vvu