Filter nodes in variable depth query

Hi there,

I'm working on a query that should return a path and also exclude some nodes.

This is the query:

MATCH (excluded:Client) 
WHERE excluded.code IN ["TEST01"] 
WITH collect(excluded) AS excludedC 
MATCH path=(b:BaseHierarchy)-[]->(n:Client:Group)-[:PARENT_OF*]->(c:Client) 
WHERE n.code = 'EMIRG' 
WITH excludedC, path, b, collect(c) as clients, n, collect(path) as paths 
WHERE NONE (c in clients where c in excludedC) 
WITH b, nodes(path) as nodes, relationships(path) as relationships 
RETURN b, 
apoc.coll.toSet(apoc.coll.flatten(collect(relationships))) as relationships, apoc.coll.toSet(apoc.coll.flatten(collect(nodes))) as nodes;

Here you can see an example graph:

image

My query should return only the circled nodes

image

But it is returning the whole graph because of the variable length operator I think?

I've tried a couple of different things like filtering the nodes list but it will still return the CP1 and I don't want it to be returned because it is under the excluded node (TEST01). Does anyone know how would I go about this? Any help is appreciated

Thanks

Hi mrksph,

pattern-matching works on the idee "what is on the left side can't be on the right side".

MATCH (excl:Client) <-[:PARENT_OF]-(n:Client:Group), path=(b:BaseHierarchy)-[]->(n)-[:PARENT_OF*]->(incl:Client) 
WHERE excl.code IN ["TEST01"] and n.code = 'EMIRG'
WITH b, nodes(path) as nodes, relationships(path) as relationships 
RETURN b, 
	apoc.coll.toSet(apoc.coll.flatten(collect(relationships))) as relationships, 
	apoc.coll.toSet(apoc.coll.flatten(collect(nodes))) as nodes;

Defining the "excl" and "incl" in the same matching and "excl" as not being a part of "incl" should insure that you have only the expected "incl".
However the 'path' may disturb this approch. I don't experience on this. Take a chance.

Thank you, @Benoit_d works like a charm!