【发布时间】:2015-11-01 14:45:44
【问题描述】:
我对@987654321@ 实体删除有以下 Neo4j Cypher 查询:
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
以前我有一个Vote 实体,它与VOTED_ON 和VOTED_FOR 与实体Criterion 和Decision 有关系。此外,Vote 与 CREATED_BY 有关系 User 实体。
一切正常。
今天,我更改了此架构。我引入了新的VoteGroup 实体。
现在,VoteGroup 而不是 Vote 包含 VOTED_ON 和 VOTED_FOR 与实体 Criterion 和 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 实体现在看起来像:
@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;
....
}
请帮助我更改提到的 Cypher 查询以删除 Votes。在我的架构更改后,它现在会删除 VoteGroups(没关系),但不会删除 Votes。我还需要删除Votes 以及Votes 和User 之间的关系。
更新
下面的新查询现在可以工作了(至少我所有的测试都通过了):
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
但我仍然不确定这个查询是否 100% 正确...无论如何,我将添加一堆测试以检查所有内容。
您能否也验证此查询? 尤其是我不确定删除了所有关系并没有在数据库中留下垃圾。
【问题讨论】:
标签: neo4j cypher spring-data-neo4j graph-databases