How to filter out specific paths from matched paths

I have a graph db that maps out the relations between source files. I have the following query which finds all files that are calling the methods defined in the given source file.

MATCH (sourceFile:JSSourceFile {filename: 'test.js'})-[:Defines]->(sourceMethod:Method)
<-[:Calls*1..5]-
(callerMethod:Method)<-[:Defines]-(callerFile:TSSourceFile)
RETURN sourceFile.fileName AS sourceFile, COLLECT(DISTINCT callerFile.fileName) AS callerFile
ORDER BY sourceFile

Now I have added another label Utility to some of the existing JSSourceFile nodes. I need to filter out the TSSourceFile whose methods calls the utilityMethods . Doing this to avoid the utility method calls which in turn being called by the TSSoureFiles and end up polluting the results

(:JSSourceFile)-[:Defines]-(sourceMethods:Methods)<-[:Calls*1..5]-(:Method)<-[:Defines]-(callerMethod:TSSourceFile)
(:JSSourceFile:Utility)-[:Defines]->(utilityMethods:Method)
Need to eliminate this relationship: 
(sourceMethods)-[:Calls]->(utilityMethods)<-[:Calls]-(callerMethod)

I have tried the following query but it is running forever on depth 2+ and I need depth in range of 1..5.

MATCH
(sourceFile:JSSourceFile {fileName:"test.js"})-[:Defines]->(:Method)
<-[:Calls*1..3]-
(callerMethod:Method)<-[:Defines]-(callerFile:TSSourceFile)
WHERE NOT EXISTS((callerMethod)-[:Calls]->(sourceFile:Utility))
RETURN sourceFile.fileName, collect(DISTINCT callerFile.fileName)

I'm a beginner in neo4j and any guidance would be really helpful. :blush:

Within your WHERE clause, you can specify a not on a label, like so:

WHERE NOT (sourceFile:Utility)

This will exclude all nodes that have the Utility label assigned to it.

1 Like