Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

neo4j: how to query subgraph

Tags:

neo4j

cypher

I want to select a subgraph(S) from my neo4j database and use another query on S to find if two given nodes are connected. Is there a way to write a query in neo4j ? I'm using node.js and Cypher. EDIT: I'm doing something similar to this, for example:

Match (u:User)-[:adds]->(y:Paper)-[:consistsOf]->(e:L2)-[]->(m:L3)
where u.username = 'test'
MATCH p=(m:L3)-[r:gives*1..4]->(n:L3)
...

Thanks

like image 450
user2103008 Avatar asked Apr 14 '14 02:04

user2103008


1 Answers

In your example, you could use the WITH clause to connect the 2 MATCH statements, like this (cleaned up a little):

MATCH (u:User {username:'test'})-[:adds]->(y:Paper)-[:consistsOf]->(e:L2)-->(m:L3)
WITH m
MATCH p=(m)-[r:gives*1..4]->(n:L3)
...

The WITH clause is like RETURN, except that its purpose is to pass value(s) from one query to the next. In this case, only 'm' is being passed, and so the second MATCH will not be aware of 'u', 'y', or 'e'.

like image 177
cybersam Avatar answered Oct 03 '22 04:10

cybersam