【问题标题】:neo4j - find nodes with strong relationshipneo4j - 查找具有强关系的节点
【发布时间】:2016-01-25 00:52:49
【问题描述】:

我的图表中有地点和人物作为标签,以及关系“knows_the_place”。喜欢:

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

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

现在我想通过地点(有很多共同的“地点”)找到具有“强”关系的人,例如,我想查询共享至少 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 Query 做到这一点?

额外问题:

与其向我展示与另一个人有 x 个地方相同的人,更好的是,如果我能获得如下订单列表:

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

感谢您的帮助!

【问题讨论】:

标签: neo4j querying


【解决方案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 应该警告你这些类型的查询。

【讨论】:

  • 完美,谢谢! :-) 特别有用的是“AS”命令,我不知何故错过了。这对我的这个和其他任务非常有用!但我也注意到这种查询的性能很慢。有没有什么通用的方法来“监控”neo4j-query,知道是什么让它变慢了?还是我只需要知道,什么样的查询是昂贵的?
  • 您可以在查询前添加EXPLAIN 和/或PROFILEPROFILE 子句甚至会在 Web 控制台中提供漂亮的视觉输出
猜你喜欢
  • 2014-05-24
  • 1970-01-01
  • 2017-07-03
  • 2014-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多