简体   繁体   English

用于查找图根节点的 Cypher 查询

[英]Cypher query for finding the root node of the graph

I am trying to find out the specific node type root in the graph database.我试图找出图形数据库中的特定节点类型根。 In Gremlin query I can find count the root node.在 Gremlin 查询中,我可以找到计数根节点。

 query = """g.V().hasLabel("A")
    .filter(
        out().hasLabel("A").count().is(gt(1))
        .and()
        .in().hasLabel("A").count().is(eq(0))
    )
    .values("title")"""

This above query will return the root node title.上述查询将返回根节点标题。 How can we find the root node of the node type in the cypher query.我们如何在密码查询中找到节点类型的根节点。

You can use a pattern in the WHERE clause to look for nodes with no incoming relationships.您可以在 WHERE 子句中使用模式来查找没有传入关系的节点。

If you have to consider labels of the nodes, then include that in the pattern:如果必须考虑节点的标签,请将其包含在模式中:

MATCH (root:A)
WHERE NOT (:A)-->(root) AND size((root)-->(:A)) > 1
...

If you don't need to know anything about the connecting nodes, and you want to make sure there is no relationship at all coming in, but there is at least one going out, you can leave out the label of the other node, and the query becomes more efficient, as the relationship type/direction degree information is on the node itself, so no need to expand:如果你不需要知道连接节点的任何事情,并且你想确保没有任何关系进来,但至少有一个出去,你可以省略另一个节点的标签,并且查询变得更高效,因为关系类型/方向度信息在节点本身,所以不需要扩展:

MATCH (root:A)
WHERE NOT ()-->(root) AND size((root)-->()) > 1
...

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

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