Neo4j如何从某个起始节点递归删除节点

时间:2015-04-18 08:35:43

标签: neo4j cypher spring-data-neo4j graph-databases

在我的Neo4j数据库中,我有以下实体:

@NodeEntity
public class Product {

    private final static String CONTAINS = "CONTAINS";
    private final static String DEFINED_BY = "DEFINED_BY";
    private final static String VOTED_FOR = "VOTED_FOR";
    private final static String PARENT = "PARENT";
    private final static String CREATED_BY = "CREATED_BY";

    @GraphId
    private Long id;

    @RelatedTo(type = PARENT, direction = Direction.INCOMING)
    private Product parent;

    @RelatedTo(type = CONTAINS, direction = Direction.OUTGOING)
    private Set<Product> childProducts = new HashSet<>();

    @RelatedTo(type = DEFINED_BY, direction = Direction.INCOMING)
    private Set<Criterion> criterias = new HashSet<>();

    @RelatedTo(type = VOTED_FOR, direction = Direction.INCOMING)
    private Set<Vote> votes = new HashSet<>();

    @RelatedTo(type = CREATED_BY, direction = Direction.OUTGOING)
    private User user;

}    

@NodeEntity
public class Criterion {

    private final static String CREATED_BY = "CREATED_BY";
    private final static String DEFINED_BY = "DEFINED_BY";

    @GraphId
    private Long id;

    @RelatedTo(type = DEFINED_BY, direction = Direction.OUTGOING)
    private Product owner;

    @RelatedTo(type = CREATED_BY, direction = Direction.OUTGOING)
    private User user;

}

@NodeEntity
public class Vote {

    private static final String VOTED_ON = "VOTED_ON";
    private final static String VOTED_FOR = "VOTED_FOR";
    private static final String CREATED_BY = "CREATED_BY";

    @GraphId
    private Long id;

    @RelatedTo(type = VOTED_FOR, direction = Direction.OUTGOING)
    private Product product;

    @RelatedTo(type = VOTED_ON, direction = Direction.OUTGOING)
    private Criterion criterion;

    @RelatedTo(type = CREATED_BY, direction = Direction.OUTGOING)
    private User user;

}

Product是一个复合实体,可以包含子Products。 从层次结构中的某个Product节点开始,我需要删除此Votes上的所有Product,然后我需要递归删除定义的所有子ProductsCriteria通过这些节点和Votes。不得删除 User个节点

我尝试过这个Cypher查询:

MATCH (p:Product)-[r:CONTAINS*]-(e) WHERE id(p) = {productId} FOREACH (rel IN r| DELETE rel) DELETE e 

但它只删除产品,并且不会删除开始节点上的投票以及所有子标准和投票。请帮我正确的Cypher查询。感谢。

1 个答案:

答案 0 :(得分:2)

我将其分成两个查询。第一个递归收集产品层次结构,第二个删除一个产品节点及其直接环境。

获取产品层次结构非常简单:

MATCH (p:Product)-[:CONTAINS*]->(childProduct)
WHERE id(p) = {productId}
RETURN id(childProduct) as id

要删除一个产品,我们需要删除该产品节点的所有关系。此外,如果所有相关节点既不是用户(您希望保留它们)也不是产品(想想父产品 - 它们也应该保留),也需要删除它们。此外,我们需要确保目标节点没有连接,这就是第二个可选匹配的原因:

MATCH (p:Product) 
OPTIONAL MATCH (p)-[r]-(t)
WHERE id(p) = {productId}
DELETE r,p
WITH t
OPTIONAL MATCH (t)-[r2:VOTE_ON|:CREATED_BY]->()
WHERE none(x in labels(t) WHERE x in ["User", "Product"])
DELETE t,r2

我没有自己测试此查询,因为您没有提供测试图。所以把它作为一个想法并修改它直到它起作用。

更新

在聊天中我们发现此cypher语句解决了该问题,请注意Product已被模型中的Decision标签取代:

MATCH (p:Decision) 
WHERE p.name = "NoSQL" 
WITH p 
OPTIONAL MATCH (p)-[r]-(t) 
DELETE p,r 
WITH t,r 
OPTIONAL MATCH (t)-[r2:VOTED_ON|:CREATED_BY|:VOTED_FOR]-() 
WITH t, r2,r 
WHERE none(x in labels(t) WHERE x in ["User", "Decision"]) 
DELETE t,r2