neo4j -return语句中两个集合之间的差异

juzqafwq  于 2022-11-05  发布在  其他
关注(0)|答案(1)|浏览(144)

我有一个模式:主题有帖子,用户写主题和帖子。我想找到推荐的主题。在我的例子中,我返回count(topics)和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);
rsaldnfx

rsaldnfx1#

你可以使用collect为topics和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)

相关问题