neo4j - 查找具有强关系的节点

neo4j - find nodes with strong relationship

我的图表中有地点和人物作为标签,还有关系 "knows_the_place"。喜欢:

(person)-[knows_the_place]->(place) 

一个人通常知道多个地方。

现在我想通过地点(有很多 "places" 的共同点)找到具有 "strong" 关系的人,所以例如我想查询所有的人,共享至少 3 个不同的地方,像这样(不工作!)查询:

MATCH
(a:person)-[:knows_the_place]->(x:place)<-[:knows_the_place]-(b:person),
(a:person)-[:knows_the_place]->(y:place)<-[:knows_the_place]-(b:person),
(a:person)-[:knows_the_place]->(z:place)<-[:knows_the_place]-(b:person)
WHERE NOT x=y and y=z
RETURN a, b

如何使用 neo4j 查询执行此操作?

加分题:

与其向我展示与另一个人有 x 个共同点的人,不如让我得到一个订单列表,如:

a 与 b 共享 7 个名额 c 与 b 共享 5 个位置 d 与 e 共享 2 个位置 f 与 a 共享 1 个位置 ...

感谢您的帮助!

给你:

MATCH (a:person)-[:knows_the_place]->(x:place)<-[:knows_the_place]-(b:person)
WITH a, b, count(x) AS count
WHERE count >= 3
RETURN a, b, count

订购:

MATCH (a:person)-[:knows_the_place]->(x:place)<-[:knows_the_place]-(b:person)
RETURN a, b, count(x) AS count
ORDER BY count(x) DESC

您也可以通过在第一个查询的后面添加 ORDER BY 来完成这两项操作。

请记住,此查询是 ab 的笛卡尔积,因此它将检查 person 节点的每个组合,这在性能方面可能不是很好,如果你有很多 person 个节点。 Neo4j 2.3 应该警告您这些类型的查询。