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.

Conditionally removing nodes and edges based on nodes and relationships

Hi all, I have a cypher query that I am struggling to get to work correctly.

Basically I have a graph loaded to my db. I then need to be able to run a query to remove any nodes and edges that are not either directly related to to one or more node names supplied to the query or any descendant nodes in a recurring fashion.

Now my query might receive the name of one or more nodes. 

I have the following query so far;

//Set graph to multiple entities
MATCH (parent:EntityProcess)
WHERE NOT EXISTS {
MATCH (parent)-[:PARENT_OF*0..]->(entity:EntityProcess)
WHERE entity.name IN ["EYU", "Member", "Organisation"]
}
DETACH DELETE parent

Now looking at my query what I am trying to achieve is to remove everything that has no PARENT_OF relationship of either "EYU", "Member" or "Organisation"  or anything that has a PARENT_OF relationship related to any of those nodes. So I want to keep everything related to "EYU" and all of it's relationships and everything related to all of those relationships etc, and the same applies to "Member" and "Organisation".

If I only use a single node name then my query works as expected. 

If I use multiple names, as above, then I am only getting expected results for the first one.

I am clearly missing something and I am assuming it is some sort of iteration issue with the way I think "IN" works in Cypher and maybe confusing it with TSQL, but then I am very confused at the best of times!

Did I explain this well enough? Can you understand what I am trying to achieve?

1 ACCEPTED SOLUTION

Apologies for not being clear, probably due to my having confused myself completely. Anyway, this is no longer an issue as the query is working exactly as expected, the problem was that my original graph was loaded incorrectly. Thank you for looking anyway.

View solution in original post

2 REPLIES 2

I am sorry, I don't understand your explanation. Your first paragraph seems to have a contradiction. From the query, I deduce you want to delete a node if none of its descendant nodes reachable via a PARENT_OF relationship have a name equal to those in your list.  Is that accurate? If not, can you please try again or try explaining with a visual? 

Apologies for not being clear, probably due to my having confused myself completely. Anyway, this is no longer an issue as the query is working exactly as expected, the problem was that my original graph was loaded incorrectly. Thank you for looking anyway.