Neo4j Cypher删除查询 [英] Neo4j Cypher delete query

查看:229
本文介绍了Neo4j Cypher删除查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下Decision实体删除,我有以下Neo4j Cypher查询:

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

以前,我有一个Vote实体,与实体CriterionDecision的关系为VOTED_ONVOTED_FOR.另外,VoteUser实体具有关系CREATED_BY.

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.

一切正常.

今天,我已经更改了此架构.我介绍了新的VoteGroup实体. 现在,VoteGroup而不是Vote包含与实体CriterionDecision的关系VOTED_ONVOTED_FOR:

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实体现在看起来像:

@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以及VotesUser之间的关系.

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.

已更新

下面的新查询现在可以正常工作(至少我所有的测试都通过了):

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

但是我仍然不确定此查询是否100%正确...无论如何,我将添加一堆测试以检查所有内容.

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.

推荐答案

看起来很复杂.

您不能只指定路径并删除整个路径吗?

Can't you just specify the path and delete the whole path?

MATCH (d:Decision) WHERE id(d) IN {decisionsIds} 
OPTIONAL MATCH path = (d)-[r1:VOTED_ON|:CREATED_BY|:VOTED_FOR]->(t)<-[r2:VOTED_ON|:CREATED_BY|:VOTED_FOR]-()
DELETE path

// or 
DELETE d,t,r1,r2

这篇关于Neo4j Cypher删除查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆