neo4j - 返回语句中两组之间的差异

时间:2015-03-06 07:32:57

标签: neo4j

我有一个架构:主题有帖子,用户写主题和帖子。我想找到推荐的主题。在我的例子中,我返回count(主题)和count(rec_topics)。如何在这些集合之间返回差异?像[1,2] - [1] = [2]谢谢

MATCH (user:User {first_name: 'Hlavní' }),
user<-[:wrote_by]-(posts:Post)<-[:TOPIC]-(topics:Topic)-[:TOPIC]->(all_posts:Post)-[:wrote_by]->(friends:User)<-[:wrote_by]-(friends_posts:Post)<-[:TOPIC]-(rec_topics:Topic)
WHERE NOT(topics=rec_topics)
RETURN count(DISTINCT topics),count(DISTINCT rec_topics);

1 个答案:

答案 0 :(得分:3)

您可以使用collect为主题和rec_topics构建集合。在第二步中,列表推导可用于仅返回那些不属于rec_topics的主题:

MATCH (user:User {first_name: 'Hlavní' }),
  user<-[:wrote_by]-(posts:Post)<-[:TOPIC]-(topics:Topic)-[:TOPIC]->       
  (all_posts:Post)-[:wrote_by]->(friends:User)<-[:wrote_by]-  
  (friends_posts:Post)<-[:TOPIC]-(rec_topics:Topic)
WHERE NOT(topics=rec_topics)
WITH collect(DISTINCT topics) as topics, collect(distinct rec_topics) as rec_topics
RETURN [x in topics WHERE not(x in rec_topics)] as delta, 
       length(topics), length(rec_topics)