Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

neo4j - difference between two sets in return statement

Tags:

neo4j

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);
like image 457
user1657173 Avatar asked Sep 03 '25 08:09

user1657173


1 Answers

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)
like image 123
Stefan Armbruster Avatar answered Sep 05 '25 00:09

Stefan Armbruster