Neo4j - 过滤节点之间的关系

时间:2017-11-21 18:27:43

标签: twitter neo4j cypher nodes relationship

我有这种关系:

enter image description here

我想计算然后计算:Tweet个节点的所有传入关系的平均值,不包括()-[:POSTS]-()关系。 我该怎么做呢?到目前为止,我可以运行此查询来计算所有传入关系的平均值:

match (t:Tweet)-->(topic:Topic) 
with topic, size((t)<--()) as rel 
where topic.name='politics' 
return avg(rel) as Avg_RelationshipCount

谢谢。

2 个答案:

答案 0 :(得分:2)

感谢用户删除了他的答案,我找到了解决方案(稍微纠正了他的问题):

match ()-[:REPLIES_TO|:RETWEETS]->(t:Tweet)-->(topic:Topic) 
with topic, size((t)<-[:REPLIES_TO|:RETWEETS]-()) as rel 
where topic.name='politics' 
return avg(rel) as Avg_RelationshipCount

答案 1 :(得分:1)

@ sirdan的这个重构形式的答案要快得多:

MATCH ()-[r:REPLIES_TO|:RETWEETS]->(:Tweet)-->(topic:Topic)
WHERE topic.name='politics'
WITH topic, COUNT(r) as rel
RETURN AVG(rel) AS Avg_RelationshipCount;

以上查询在生成关系计数之前按topic.name 过滤,以消除多余的计数。它还避免了两次不必要地执行(t)<-[:REPLIES_TO|:RETWEETS]-()查询。