I want to use Cypher to extract this subgraph (below image).
Subgraph structure:
User -- WATCHED --> Resource
Resource -- CONTAINS --> Video
Course -- HAS_RESOURCE --> Resource
This code that I used to extract User
, Resource
and Video
. Now I don't know how to extract Course
more.
MATCH (u:User)-[w:WATCHED]->(r:Resource)-[:CONTAINS]->(v:Video)
RETURN * LIMIT 50
Example subgraph:
Please help me, I want to extract all 4 node labels: User, Resource, Video and Course
It depends what you mean by extracting "all 4 node labels".
If you just want all nodes with those labels, you could do this:
MATCH (n:User|Resource|Video|Course)
RETURN n
If you want all tuples as shown in your subgraph structure (so not including nodes with those labels that aren't connected as shown) you could use a UNION
to map the three types of tuple to the same three columns:
MATCH (source:User)-[rel:WATCHED]->(target:Resource)
RETURN *
UNION
MATCH (source:Resource)-[rel:CONTAINS]->(target:Video)
RETURN *
UNION
MATCH (source:Course)-[rel:HAS_RESOURCE]->(target:Resource)
RETURN *
If you want all Users that have watched a Resource, plus any Video or Course connected to those Resources, you can use OPTIONAL MATCH
:
MATCH (u:User)-[w:WATCHED]->(r:Resource)
OPTIONAL MATCH (r)-[cn:CONTAINS]->(v:Video)
OPTIONAL MATCH (r)<-[h:HAS_RESOURCE]-(cr:Course)
RETURN *
If you want only want Users that have watched a Resource that contains a Video, and that Resource is part of a course, then you can use a graph pattern:
MATCH (u:User)-[w:WATCHED]->(r:Resource),
(r)-[cn:CONTAINS]->(v:Video),
(r)<-[h:HAS_RESOURCE]-(cr:Course)
RETURN *