How to retrieve a node based on its Top most node that appears before a few parent nodes


I have a hierarchy of node relationships like -

Organisation -> Department -> System -> Function -> Port - > Request -> Response -> Parameter

Now i retrieve the Parameter node like -

  MATCH p=()-[r:Parameter]->(b:checkoutby) RETURN p;

However, checkoutby is present in two Organisations -Century & IndiaBulls and i want to retrieve the Parameter checkoutby from Organisation Century. What will be my cypher ?

This is definitely not working -

 MATCH p=(n: Century)-[r:Parameter]->(b:checkoutby) RETURN p;

your cypher would be like below.

MATCH (p:Organisations )-[*]->(b:checkoutby) RETURN b;

you can apply filter according to your Organisations here using Where.

Can you elaborate more on your model? So far you've given us a hierarchy of node relationships, but it would help to know the node labels of nodes in that pattern.

The problem you've provided suggests a modeling problem.

Keep in mind that given the longer pattern, if you have the same :checkoutby node connected (at multiple hops) from multiple organizations, you need something to preserve the context of which organization is connected, usually with a direct relationship to the :Organization node in question (provided you have one).

1 Like

Thanks Kunal.

The query -

       MATCH q=(p)-[*]->(b:checkoutby) WHERE ="william" RETURN q

gives the entire network. Like this -

This is ofcourse useful. But can you help me with the retaining only the immediate parent node.

Like the entire hierarchy is like -

william - [Department ]-> sucheta - [System] -> geeta - [Function]-> wfWorkItemService - [Port] -> wfWorkSoapService - [Parameter] -> wfFetchWorkList -[Parameter] .........->documents - [Parameter] ->checkoutby

But i want the query like --> I mention the organisation which is the top most node .. but i get the response with only the required two nodes. As -

documents - [Parameter ] -> checkoutby

To Kunal and Andrew,

I tried the query -

  MATCH (n:william) WHERE n is null RETURN n UNION MATCH     n=(p)-[:Parameter]->(b)  WHERE    ="checkoutBy"   RETURN n

But here the effect of william node i.e. the first parent node is nullified and we get the output irrespective of the parent node.

I even tried this query -

MATCH (n) WHERE none(node in nodes(n) WHERE node:william) RETURN n UNION MATCH     n=(p)--        
()-[:Parameter]->(b)  WHERE ="cabinet"   RETURN n

but i get error -

Neo.ClientError.Statement.SyntaxError: Type mismatch: expected Path but was Node (line 1, column 36 (offset: 35))
"MATCH (n) WHERE none(node in nodes(n) WHERE node: william ) RETURN n UNION MATCH n=(p)--()-[:Parameter]->(b) WHERE ="cabinet" RETURN n"

I even tried the intersection query but to no avail .

MATCH (n1:william), (n2),(q:cabinet)
WHERE (n1)<-[:Department]-() AND (n2)<-[:Parameter]-(q)
 RETURN count(q), collect(

Warning Error-

This query builds a cartesian product between disconnected patterns.

If a part of a query contains multiple disconnected patterns, this will build a cartesian product between all those parts. This may produce a large amount of data and slow down query processing. While occasionally intended, it may often be possible to reformulate the query that avoids the use of this cross product, perhaps by adding a relationship between the different parts or by using OPTIONAL MATCH (identifier is: (n2))

  EXPLAIN MATCH (n1:william), (n2),(ego:cabinet)

Even this query doesn't work -

  MATCH (n:william)  RETURN n UNION MATCH  n=(p)-[:Parameter]->(b)  WHERE ="checkoutBy" 
   call apoc.path.expandConfig(n, {labelFilter:'-william'}) yield path
    return path 

Please help . I want to retrieve the checkoutby / cabinet node only if it is from the topmost parent node - william