使用Cypher根据节点属性检索层次结构。

huangapple go评论65阅读模式
英文:

Retrieve hierarchy based on node property with Cypher

问题

可以使用Cypher查询检索从P1开始的整个层次结构。以下是可能的Cypher查询:

MATCH (p1:Node {name: 'P1'})-[:CHILD*]->(child:Node)
RETURN p1, COLLECT(child) AS hierarchy

这个查询会匹配以P1为起点的所有子节点,包括它们之间的所有层次关系,并将它们收集到一个列表中。最终的结果中,p1 将是起点节点,而 hierarchy 列表将包含整个层次结构中的所有节点。

英文:

I have nodes in my graph where they are in a hierarchy. The hierarchy is saved as a node property instead of edges. There are no connecting edges between these nodes in the graph.

eg:

P1 -CHILD-> P2, P3 -CHILD-> P4
P1 has Node property: Child - [P2,P3,P4]
P2 has Node property: Child - [P4]
P3 has Node property: Child - [P4]
P4 has Node property: Child - None

Is it possible to retrieve all the hierarchy starting from P1 in a cypher query?

答案1

得分: 0

为了充分利用图数据库的功能,您的图应该具有关系。请使用以下查询创建它们。

MATCH (n)
UNWIND n.child AS child
MATCH (ch {name: child})
CREATE (n)-[:HAS_CHILD]->(ch)

然后,您可以使用以下查询返回完整的层次结构。

MATCH path = (n {name: "P1"})-[:HAS_CHILD*]->(ch)
RETURN 

AS pathToChild

英文:

To take advantage of the power of a graph database, your graph should have relationships. Create them with this query.

MATCH (n)
UNWIND n.child AS child
MATCH (ch {name:child})
CREATE (n)-[:HAS_CHILD]->(ch)

Then you can return the full hierarchy with this query.

MATCH path = (n {name:"P1"})-[:HAS_CHILD*]->(ch)
RETURN 

AS pathToChild

答案2

得分: 0

你可以考虑使用ORDPATH来按层次排序。有一个Neo4j用户定义的函数可以实现这个功能,详细信息请参考https://www.wai.md/post/ordpath-computing-genealogy-descendancy-trees

在图遍历过程中,您可以收集数字值,然后调用该函数获取连接的位串,以进行层次排序。

英文:

You might consider using ORDPATH for sorting in hierarchical order. There is a Neo4j user defined function for this described at
https://www.wai.md/post/ordpath-computing-genealogy-descendancy-trees

During graph traversals you can collect numeric values and then call the function to get the concatenated bitstring which sorts hierarchically.

huangapple
  • 本文由 发表于 2023年7月3日 16:58:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76603290.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定