neo4j按输入的数组顺序匹配节点

时间:2016-09-17 13:15:13

标签: neo4j cypher

我正在尝试实现https://neo4j.com/blog/moving-relationships-neo4j/指针功能,以便将其用作团队订单计算机。请参阅http://imgur.com/a/MViF0了解模型。我正在使用这个密码查询。

 MERGE (list:LIST)
 WITH list
 MATCH (u) WHERE ID(u) IN [421, 419, 420]
 MERGE (team:TEAM{name: u.name})
 MERGE (team)-[:PARTOF]->(list)
 WITH collect(team)as elems,list
      FOREACH (n IN RANGE(0, LENGTH(elems)-2) |
 FOREACH (prec IN [elems[n]] |
 FOREACH (next IN [elems[n+1]] |
 MERGE (prec)-[:NEXT]->(next))))
 with list
 MATCH (elem:TEAM) WHERE NOT (elem)<-[:NEXT]-()
 MERGE (list)-[:POINTER]->(elem)

现在这很好用,但我只有一个问题。这一行:

MATCH (u) WHERE ID(u) IN [421, 419, 420]

返回按ID排序的原始团队,但我想通过[421,419,420]模式中的模式定义我的顺序,就像一个

的函数
return * order by my array input.

请记住,它应该适用于任何数量的团队,这只是一个例子。而我原来的团队节点并没有被标记为团队,而是其他东西,所以我们每次都会复制。感谢任何投入,谢谢。

1 个答案:

答案 0 :(得分:2)

尝试使用语句&#34;展开&#34;:

MERGE (list:LIST)
WITH list
UNWIND [421, 419, 420] as uid
MATCH (u) WHERE id(u) = uid
MERGE (team:TEAM{name: u.name})
...

[更新]当然,可以手动了解每个节点的订单:

MERGE (list:LIST)
WITH list, [3871013, 3871011, 3871012] as ids
MATCH (u) WHERE ID(u) IN ids
WITH list, u, 
     FILTER(x in RANGE(0,size(ids)-1) WHERE ids[x] = id(u)) as orderIndex 
     ORDER BY orderIndex[0] // Sort by node position in the array of identifiers
MERGE (team:TEAM{name: u.name})
...