Neo4j,根据节点属性值的顺序创建关系
Neo4j, create relationships according to the order of the node property values
我有一个图形数据库。图中的节点假设根据给定 属性 值的顺序连接。例如,图节点可以是:
(a{p:1}) (b{p:3}) (c{p:6}) (d{p:8})
那么连接应该是:
a--b--c--d
按照p值排序
问题在于 p 值是随机给出的。
那么,如何通过 Cypher 创建节点之间的关系?
你们能给我一些建议吗?非常感谢!
听起来目标是通过p
组织节点,然后在它们之间建立关系以建立有序列表。
// find all of the nodes with a p value
MATCH (n:Node)
WHERE EXISTS (n.p)
// put them in a collection ordered by p
WITH n
ORDER BY n.p
WITH COLLECT(n) AS nodes
// iterate over the collection in pairs starting at the beginning and create the relationship
UNWIND RANGE(1,size(nodes)-1) AS idx
WITH nodes[idx-1] AS a, nodes[idx] AS b
MERGE (a)-[:NEXT]->(b)
我有一个图形数据库。图中的节点假设根据给定 属性 值的顺序连接。例如,图节点可以是:
(a{p:1}) (b{p:3}) (c{p:6}) (d{p:8})
那么连接应该是:
a--b--c--d
按照p值排序
问题在于 p 值是随机给出的。
那么,如何通过 Cypher 创建节点之间的关系?
你们能给我一些建议吗?非常感谢!
听起来目标是通过p
组织节点,然后在它们之间建立关系以建立有序列表。
// find all of the nodes with a p value
MATCH (n:Node)
WHERE EXISTS (n.p)
// put them in a collection ordered by p
WITH n
ORDER BY n.p
WITH COLLECT(n) AS nodes
// iterate over the collection in pairs starting at the beginning and create the relationship
UNWIND RANGE(1,size(nodes)-1) AS idx
WITH nodes[idx-1] AS a, nodes[idx] AS b
MERGE (a)-[:NEXT]->(b)