简体   繁体   中英

Does neo4j can query all nodes connected with relationship?

I am struggling with a Neo4j problem. My project is using Cypher query to remote Neo4j server.

We have nodes and relationships are connected with others, so there are clusters:

(a)->(b)->(c)->(d), (a)<-(f), (a)<-(g).

Single nodes are not connected with other nodes:

(h), (i), (j).

I want to query a graph contains all nodes connected to node(a) no matter the relationship's direction, (b)->(a) or (a)->(b).

I have read other questions, did google, this is my current query:

MATCH path =(a { ID:'1' })--(neighbor)
RETURN path

But this query only stopped at the first neighbor node, like (a)<-(b), the nodes (c) and (d) are not included.

Please help me and teach me how to include all nodes linked to (a)?

Thank you

Yufan

You can use variable path length matches using the * in the relationship specification, see http://docs.neo4j.org/chunked/stable/introduction-pattern.html , section "variable length".

To get all nodes in a cluster:

MATCH path =(a { ID:'1' })-[*]-(neighbor) RETURN distinct neighbor

We aware that using * queries unlimited depth and can be expensive. It's a good practice to supply a upper limit eg [*..20] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM