AJINKYA JAIN
AJINKYA JAIN

Reputation: 33

Multiple match statements in neo4j in a single query which are related to each other

I have a list of match statement which are related to each other

Match(pst:Post) where a.status="ACTIVE"  
WITH PST
MATCH(g: Group {id:pst.parentId}) <-[m:MEMBER]-(u:User {userId:'[email protected]}) ,(b:Business {id:pst.parentId}) 
where (b.status ="ACTIVE" OR g.status="ACTIVE") 
return n;

There are 3 types of Post , the actual Post, GroupPost (member relationship - who has created post in the group and also a member of that group) and businessPost(HAS-A relationship with business node) Basically I want to get Post from actual Post node AND Post from group node of which user is member of that group AND post from the business node. I am clueless what query to form?

Upvotes: 0

Views: 946

Answers (1)

Tomaž Bratanič
Tomaž Bratanič

Reputation: 6534

If I understand you correctly you want to return three different types of post in a single query. The best way to approach this problem is to use the UNION operator. Given that I don't know your graph schema, your query would look along the lines of:

MATCH (u:User{id:$userId})-[:HAS_POST]->(post)
RETURN post
UNION
MATCH (u:User{id:$userId})-[:MEMBER]->(group)-[:HAS_POST]->(post)
RETURN post

This is one way how you could approach your problem. Another way would be to also use OPTIONAL MATCH, but I would need more information before I could help you more.

Upvotes: 1

Related Questions