4

I have a schema like: Topics have posts and users write topics and posts. I would like to find recommended topics. In my example I return count(topics) and count(rec_topics). How is possible to return difference bewteen these sets? Like [1,2] - [1] = [2] Thank you

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);
user1657173
  • 231
  • 1
  • 2
  • 9

1 Answers1

7

You can use collect to build up a collection for topics and rec_topics. In a second step a list comprehension can be used to return only those topics not being part of 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)
jonny
  • 3,022
  • 1
  • 17
  • 30
Stefan Armbruster
  • 39,465
  • 6
  • 87
  • 97