我正在尝试用neo4j
cypher语言编写一个查询,该查询创建一个节点关系链,按特定条件排序。
例如。我有这个问题:
MATCH (n)
WHERE n.some_property = 'something'
WITH n
ORDER BY n.some_other_property
WITH collect(n) as node_list
我需要将node_list
的每个元素与该列表的下一个元素相关联。
示例,如果node_list
包含:
[a, b, c, d, e]
结果将是:
CREATE (a)->[:rel]->(b)->[:rel]->(c)->[:rel]->(d)->[:rel]->(e)
所以最后的查询将是:
MATCH (n)
WHERE n.some_property = 'something'
WITH n
ORDER BY n.some_other_property
WITH collect(n) as node_list
CREATE LINKS OF CONSECUTIVE node_list
有什么办法吗?
答案 0 :(得分:2)
是的!收集完成后,使用UNWIND获取一系列索引:
MATCH (n)
WHERE n.some_property = 'something'
WITH n
ORDER BY n.some_other_property
WITH COLLECT(n) as node_list
UNWIND RANGE(1, SIZE(node_list) - 1) AS idx
WITH node_list[idx - 1] AS from, node_list[idx] AS to
CREATE (from)-[:NEXT]->(to);