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.

mrksph
Node Clone

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:

3X_d_1_d1b93ba7e8fbbac52ecd73fbf91d7ae71362e044.png

My query should return only the circled nodes

3X_0_c_0c553d22eca88ed65ed48f5dcc14ff53d383ad06.png

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

Comments
Benoit_d
Graph Buddy

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.

mrksph
Node Clone

Thank you, @Benoit_d works like a charm!

Version history
Last update:
‎08-03-2021 06:33 AM
Updated by:
Contributors