hello, I want to get all the nodes and relations that connected to subGraphInstance node, and I'm having troubles with that,

the graph looks like this:

and I want to find it and all it's relations by the name of the subgraph

unfortunately all i'm able to get is this when I tried to run a query that goes like this:

MATCH (n:SubGraphInstance)-[r:FROM_GRAPH]->(c:CellInstance)-[g:FROM_TYPE]->(t:CellType)-[k:CELL_TYPE_FUNCTION]->(f:Function)-[v:FUNCTIONS_VARS]->(va:Variable)

WHERE n.name= "batman"

please help me with this guys!

and have a great day!!

:

You have specified a very specific pattern to match. It looks like two paths matched and where returned. You could try a more generalized pattern to obtain all the paths, such as:

```
MATCH p=(n:SubGraphInstance)-[*]-()
WHERE n.name= "batman"
RETURN p
```

I see from the data, that there looks to be four paths originating from the Batman node and terminating on a ‘Variable’ node that has only an incoming FUNCTIONS_VARS relationship type. We can use that to get more targeted and get just those paths. The following looks for paths starting from Batman and terminating on a ‘Variable’ node, I.e, no outgoing relationships.

```
MATCH p=(n:SubGraphInstance)-[*]-(va:Variable)
WHERE n.name= "batman
AND not exists ( (va)-[]->() )
RETURN p
```

There are also APOC methods to retrieve subgraphs originating from a node. Maybe one of the APOC experts will provide such a solution.

1 Like

thank you so much!! looks like it works :)!!

and can you please explain me about the asterisk in these two cases? I do understand that it's connected to relationships, but how exactly?

Glad to. When you specify a pattern as below, it is requiring the two nodes to be exactly one relationship apart.

```
Match ()-[]-()
```

Sometimes you want to match a pattern, but you don’t want to specify the exact sequence of relationships between two nodes. In such a case, you can use variable length patterns, where you specify the number of relationships instead. The asterisk is the syntax that indicates a variable number relationships between two nodes. The match below specifies paths between two nodes that has any number of relationships between the two nodes.

```
Match ()-[*]-()
```

You can also set upper and lower bounds on the number of hops, as below, where the min is 2 relationships and the max is 6 relationships. Either the min or max is optional, allowing you to set only an upper or lower limit as well.

```
Match ()-[*2…6]-()
```

If you want to read more, try reviewing section 4.3 and the sections around it.

1 Like

Awesome! thank you so much for your clear answers ,I will also review section 4.3 that you added to understand more how everything works.

and if i will have more questions I will ask :)