2

using Neo4j - Graph Database Kernel 2.0.0-M02 and the new merge function, I was trying to merge nodes into a new one (merge does not really merges but binds to the returning identifier according to the documentation) and delete old nodes. I only care at the moment about properties to be transferred to the new node and not relationships. What I have at the moment is the cypher below

merge (n:User {form_id:123})  //I get the nodes with form_id=123 and label User 
with n match p=n  //subject to change to have the in a collection  
create (x) //create a new  node 
foreach(n in nodes(p): set x=n) //properties of n copied over to x
return n,x 

Problems 1. When foreach runs it creates a new x for every n 2. Moving properties from n to x is replacing all properties each time with the new n so if the 1st n node from merge has 2 properties a,b and the second c,d in the and after the set x=n all new nodes end up with c,d properties. I know is stated in the documentation so my question is: Is there a way to merge all properties of N number of nodes (and maybe relationships as well) in a new node with cypher only?

4

2 回答 2

4

我认为 Cypher 语言目前没有一种语法可以非破坏性地将任何和所有属性从一个节点复制到另一个节点。

但是,我将针对可能与您的情况相似的简单情况提出解决方案。假设一些用户节点具有属性 a 和 b,而另一些用户节点具有属性 c 和 d。例如:

  CREATE (:User { id:1,a: 1,b: 2 }),(:User { id:1,c: 3,d: 4 }),
         (:User { id:2,a:10,b:20 }),(:User { id:2,c:30,d:40 });

这就是我们如何将具有相同 id 的所有用户节点“合并”到一个节点中的方式:

  MATCH (x:User), (y:User)
  WHERE x.id=y.id AND has(x.a) AND has(y.c)
  SET x.c = y.c, x.d = y.d
  DELETE y
  RETURN x

您可以在 neo4j 沙箱中试用:http ://console.neo4j.org/

于 2014-02-01T22:59:56.897 回答
3

使用 Neo4j-3.x 还可以使用特定的apoc 过程将两个节点合并为一个。

首先,您需要将 apoc 程序 jar 文件下载到您的$NEO4J_HOME/plugins文件夹中并启动 Neo4j 服务器。

然后你可以这样调用apoc.refactor.mergeNodes

MATCH (x:User), (y:User)
WHERE x.id=y.id
call apoc.refactor.mergeNodes([x,y]) YIELD node
RETURN node

如我所见,生成的节点将具有 x 和 y 的所有属性,如果两者都设置,则选择 y 的值。

于 2016-09-05T17:14:34.140 回答