Neo4J two way relationship query with cypher -


i have two-way [:friendship] relationship between user nodes:

(usera)<-[:friendship {approved:true}]->(userb)

here's little test function in java setup relationship:

public void approvefriendship(user requestor, user requestee) {     friendship friendship = new friendship(requestor, requestee);     friendship.setapproved(true);      friendship invertedfriendship = new friendship(requestee, requestor);     invertedfriendship.setapproved(true);      requestor.getfriendships().add(friendship);     requestee.getfriendships().add(invertedfriendship);      userrepository.save(requestor);     userrepository.save(requestee); } 

this cypher query returns friends of usera, , works fine:

start user=node({0}) match (user)-[r?:friendship]->(friends) r.approved = true return friends 

this cypher query returns posts of friend, , not work (returns empty result):

start n=node({0}) match (n)<-[r?:friendship]->(friend)<-[:author]-(friendposts) r.approved = true return friendposts order friendposts.createdat 

when omitting where r.approved = true line or changing where r.approved = false returns posts of friends without approved status in both cases.

can spot if i'm doing wrong here? obliged.

solved it.

dunno why, in one-to-many type relationship (eg. user post) neo4j prefers outgoing relationships on collection, , incoming on single entity. had other way round...

now classes this:

public class user {     @relatedto(type = "author")     private set<post> posts; }  public class post {     @relatedto(type = "author", direction = direction.incoming)     private user author; } 

and of course cypher query has change reflect new relationship direction (note arrow behind author):

start n=node({0}) match (n)<-[r?:friendship]->(friend)-[:author]->(friendposts) r.approved = true return friendposts order friendposts.createdat 

Comments