ekkis
ekkis

Reputation: 10236

Terminating a path on specific node types

I have something like Project->Organisation->Project. so one organization can have many projects. I want to query starting with a specific project, and return all nodes associated with the project (including organisations) except for other projects. so I wrote this malfunctioning query:

match (p:Project {Code: 'XR-1'})-[r*1..3]-(n) where not(n:Project) return p, n, r

which parses (to my understanding) as: find project XR-1 and get all nodes within 3 layers of depth that are not projects. what I'm getting is a bunch of other projects. it finds the organization that runs XR-1 and since that org also runs a bunch of other projects, it returns those.

what is the correct way to write this?

* Update I * I've come to the realisation that the where clause is greedy i.e. it gets applied to the longest matching pattern, instead of the shortest. so if I do something like:

match (p:Project {Code: 'XR-1'})-[r*1..3]-(n) 
where labels(n)[0] in ['Documentation'] return p, n, r

it returns projects run by the organization that runs XR-1 because they have a documentation node. it matches on the documentation node and fails to filter out organisations and projects. it's greedy. so what I need is a way to apply the where clause to every node found on the path

Upvotes: 0

Views: 54

Answers (1)

cybersam
cybersam

Reputation: 67044

Does this query work for you?

MATCH p=(proj:Project { Code: 'XR-1' })-[r*1..3]-(n)
WITH proj, RELATIONSHIPS(p) AS rs, TAIL(NODES(p)) AS ns
WHERE NONE (x IN ns WHERE x:Project)
RETURN proj, ns, rs;

It filters out paths that contain any extra Project nodes.

See this console for an example of this query working.

Upvotes: 1

Related Questions