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)
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

Need to eliminate this relationship: 

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

(sourceFile:JSSourceFile {fileName:"test.js"})-[:Defines]->(:Method)
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