[英]Cypher merge nodes with same property and collected the other property
I have nodes with this structure 我有这种结构的节点
(g:Giocatore { nome, match, nazionale})
(nome:'Del Piero', match:'45343', nazionale:'ITA')
(nome:'Messi', match:'65324', nazionale:'ARG')
(nome:'Del Piero', match:'18235', nazionale:'ITA')
The property 'match' is unique (ID's of match) while there are several 'nome' with the same name. 属性“ match”是唯一的(匹配的ID),而有多个“ nome”具有相同的名称。 I want to merge all the nodes with the same 'nome' and create a collection of different 'match' like this
我想将所有节点合并为相同的“ nome”,并创建一个不同的“ match”集合,如下所示
(nome:'Del Piero', match:[45343,18235], nazionale:'ITA')
(nome:'Messi', match:'65324', nazionale:'ARG')
I tried with apoc library too but nothing works. 我也尝试过apoc库,但是没有任何效果。 Any idea?
任何想法?
Can you try this query : 您可以尝试以下查询:
MATCH (n:Giocatore)
WITH n.nome AS nome, collect(n) AS node2Merge
WITH node2Merge, extract(x IN node2Merge | x.match) AS matches
CALL apoc.refactor.mergeNodes(node2Merge) YIELD node
SET node.match = matches
Here I'm using APOC to merge the nodes, but then I do a map transformation on the node list to have an array of match
, and I set it on the merged node. 在这里,我使用APOC合并节点,但是随后我在节点列表上进行了映射转换以具有
match
数组,并在合并的节点上进行了设置。
I don't know if you have a lot of Giocatore
nodes, so perhaps this query will do an OutOfMemory exception, so you will have to batch your query. 我不知道您是否有许多
Giocatore
节点,因此此查询可能会执行OutOfMemory异常,因此您将必须对查询进行批处理。 You can for example replace the first line by MATCH (n:Giocatore) WHERE n.nome STARTS WITH 'A'
and repeat it for each letter or you can also use the apoc.periodic.iterate
procedure : 例如,您可以将
MATCH (n:Giocatore) WHERE n.nome STARTS WITH 'A'
替换为第一行,其中MATCH (n:Giocatore) WHERE n.nome STARTS WITH 'A'
开头,然后对每个字母重复此操作,或者也可以使用apoc.periodic.iterate
过程:
CALL apoc.periodic.iterate(
'MATCH (n:Giocatore) WITH n.nome AS nome, collect(n) AS node2Merge RETURN node2Merge, extract(x IN node2Merge | x.match) AS matches',
'CALL apoc.refactor.mergeNodes(node2Merge) YIELD node
SET node.match = matches',
{batchSize:1000,parallel:true,retries:3,iterateList:true}
) YIELD batches, total
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.