1
votes

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

1
I am a newbie and I am waiting for any answer online. :) CheersWang Yufan
Hi, I found 1 query which is available to my problem. But it looks like a stupid and slow solution. Is there any way to improve it? MATCH path =(n)--(neighbor)--(x { ID:'123456' })--(follower)--(m) RETURN path,neighbor, follower,m,n limit 1000Wang Yufan
I am using a Cypher to satisfy my requirement, which can be foundhere.Wang Yufan

1 Answers

1
votes

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 e.g. [*..20].