简体   繁体   English

Neo4j Cypher检查节点是否为子节点的祖先

[英]Neo4j Cypher check if node is ancestor of child node

I have a following class: 我有以下课程:

@NodeEntity
public class Product {

    private final static String CONTAINS = "CONTAINS";

    @GraphId
    private Long id;

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

}

I'm trying to implement a method: 我正在尝试实现一种方法:

public boolean isProductAncestor(Product productId, Product childProductId) {
    //some Cypher query
}

that will check if productId is an ancestor(any depth up to the root) of childProductId . 它将检查productId是否为childProductId的祖先(直到根的任何深度)。

I need a Cypher query for it. 我需要一个Cypher查询。

This query finds the path between them: 此查询查找它们之间的路径:

MATCH path=(p:Product)-[:CONTAINS*]->(m:Product)
WHERE id(p) = {idp} AND id(m) = {idm}
RETURN path LIMIT 1;

If you get a result, that means that m is an ancestor of p . 如果得到结果,则意味着mp的祖先。 If you get nothing, there's no relationship between them. 如果您什么都没得到,则它们之间没有任何关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM