I have a following Neo4j Cypher query for Decision
entity deleting:
MATCH (d:Decision)
WHERE id(d) IN {decisionsIds}
OPTIONAL MATCH (d)-[r]-(t)
DELETE d, r WITH t, r
WHERE NOT (id(t) IN {decisionsIds})
OPTIONAL MATCH (t)-[r2:VOTED_ON|:CREATED_BY|:VOTED_FOR]-()
WHERE r2 <> r WITH t, r2
WHERE none(x in labels(t) WHERE x in ['User', 'Decision']) DELETE t, r2
Previously I had a Vote
entity with relationships VOTED_ON
and VOTED_FOR
to entities Criterion
and Decision
. Also, Vote
has relationship CREATED_BY
to User
entity.
Everything worked fine.
Today, I have changed this schema. I have introduced new VoteGroup
entity.
Now, VoteGroup
instead of Vote
contains relationships VOTED_ON
and VOTED_FOR
to entities Criterion
and Decision
:
@NodeEntity
public class VoteGroup extends BaseEntity {
private static final String VOTED_ON = "VOTED_ON";
private final static String VOTED_FOR = "VOTED_FOR";
private final static String CONTAINS = "CONTAINS";
@GraphId
private Long id;
@RelatedTo(type = VOTED_FOR, direction = Direction.OUTGOING)
private Decision decision;
@RelatedTo(type = VOTED_ON, direction = Direction.OUTGOING)
private Criterion criterion;
@RelatedTo(type = CONTAINS, direction = Direction.OUTGOING)
private Set<Vote> votes = new HashSet<>();
private double avgVotesWeight;
private long totalVotesCount;
.....
}
Vote
entity now looks like:
@NodeEntity
public class Vote extends BaseEntity {
private final static String CONTAINS = "CONTAINS";
private final static String CREATED_BY = "CREATED_BY";
@GraphId
private Long id;
@RelatedTo(type = CONTAINS, direction = Direction.INCOMING)
private VoteGroup group;
@RelatedTo(type = CREATED_BY, direction = Direction.OUTGOING)
private User author;
private double weight;
....
}
Please help me to change the mentioned Cypher query in order to delete Votes
. After my schema changes it now deletes VoteGroups
(it's okay) but doesn't deletes Votes
. I need to delete Votes
and relationships between Votes
and User
also.
UPDATED
The new following query working now(at least all my tests passed):
MATCH (d:Decision)
WHERE id(d) IN {decisionsIds}
OPTIONAL MATCH (d)-[r]-(t)
DELETE d, r
WITH t, r
WHERE NOT (id(t) IN {decisionsIds})
OPTIONAL MATCH (t)-[r2:VOTED_ON|:CREATED_BY|:VOTED_FOR]-()-[r3:CONTAINS]-(t2)
WHERE r2 <> r
WITH t, r2, t2, r3
WHERE none(x in labels(t)
WHERE x in ['User', 'Decision'])
DELETE t, r2, t2, r3
but I'm still not sure that this query is 100% correct... Anyway, I'll add a bunch of tests in order to check everything.
Could you please validate this query also? Especially I'm not sure that deleted all the relationships and did not leave the garbage in the database.