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

neo4j - difference between two sets in return statement

我的架构如下:主题有帖子,用户编写主题和帖子。我想找到推荐的主题。在我的示例中,我 return count(topics) 和 count(rec_topics)。这些集合之间怎么可能 return 不同?点赞[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);

您可以使用 collect 为主题和 rec_topics 建立一个集合。在第二步中,列表理解可以用于 return 只有那些不属于 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)