cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Return all nodes/paths with a where clause

Hi, I am struggling with a specific cypher query.

My Structure:

Procedure
---> name
---> status
---> ProcedureVersions
---------> version
---------> ProcedureNodes
--------------> nodeName
--------------> Widget
---------------------> name
---------------------> Bean
---------------------------> name
---------------------------> fields
---------------> Form
---------------------> type
---------------------> Api
---------------------------> name
---------------------------> location

I have this query below right.
eg.

MATCH (p:Procedure) - [hv:HAS_VERION] -> (pv:ProcedureVersions) - [hn:HAS_NODE] -> (pn:ProcedureNodes)
WHERE p.name CONTAINS 'someName' AND pn.nodeName CONTAINS 'someNodeName'
RETURN *

But the problem is that it only returns the p, pv and pn. And not all nodes below the ProcedureNode as well.

Not All nodes have forms and not all nodes have Widgets. So cannot go deeper and specify
-[hf:HAS_FORM]->(f:Form) because it will only return the nodes that has forms, and it also will not return the (w:Widget) part as well.

How would I include everything related with that query as a base query?

Thank you.
I hope I make sense.

3 REPLIES 3

You can add a "subquery" like this:

MATCH (p:Procedure) - [hv:HAS_VERION] -> (pv:ProcedureVersions) - [hn:HAS_NODE] -> (pn:ProcedureNodes)
WHERE p.name CONTAINS 'someName' AND pn.nodeName CONTAINS 'someNodeName'
WITH p,hv,pv,hn,pn
MATCH (pn)-[r*1..3]-(x)
RETURN p,hv,pv,hn,pn,r,x

So matching all relationships in 3 levels from pn and returning those as well.

Thank you very much. I did that and it does indeed return what I need. Although it says that that way is deprecated, the query takes forever to run on our data set.

Regarding the deprecation notice: Binding relationships to a list in a variable length pattern is deprecated. (Binding a variable length relationship pattern to a variable ('r') is deprecated and will be unsupported in a future version. The recommended way is to bind the whole path to a variable, then extract the relationships: MATCH p = (...)-[...]-(...) WITH *, relationships(p) AS r)

As for query time, the more exact your query is, the faster it is. E.g. in your schema you don't need a 3 level depth, but only 2. And the query is bidirectional ()-[]-() – it may improve it to add an arrow: MATCH (pn)-[r*1..2]->(x).