Neo4j Cypher RETURN request a different set of nodes

I have a simple db model of a social network model. Users can follow other users and send messages. I am trying to get a list of all messages sent by the user along with what the user sent by the user sent.

START a=node:node_auto_index(UserIdentifier = "USER0") MATCH (a)-[:POSTED]->(b), (a)-[:FOLLOWS]->(c)-[:POSTED]->(d) RETURN b, d; 

It returns a cross product of two, a tuple of all values ​​in b connected to all values ​​in d. (bxd) I would just like to make a list of messages. How can I do it? Do I need to make two separate requests?

+4
source share
2 answers

Summarized at https://groups.google.com/forum/?fromgroups=#!topic/neo4j/SdM7bKNRDEA :

 START a=node:node_auto_index(UserIdentifier = "USER0") MATCH (a)-[:POSTED]->(b) WITH a, collect(b) as posts MATCH (a)-[:FOLLOWS]->(c)-[:POSTED]->(d) RETURN posts, collect(d) as followersPosts; 
+1
source

Another way you can do this now (and IMHO is a cleaner way) is to use variable-length relationships.

 START user=node... MATCH (user) -[:FOLLOWS*0..1]-> (following) -[:POSTED]-> (post) RETURN post 

The advantage of this method is that it allows you to combine both your own requests and the requests of your friends / followers. For instance. sorting, restriction, pagination, etc.

+1
source

Source: https://habr.com/ru/post/1442943/


All Articles