# Nested APOC statements using apoc.refactor.mergeNodes()

**URL:** <https://community.neo4j.com/t/nested-apoc-statements-using-apoc-refactor-mergenodes/21391>\
**Category:** Procedures & APOC\
**Created:** [July 10, 2020, 3:06am UTC](https://community.neo4j.com/t/nested-apoc-statements-using-apoc-refactor-mergenodes/21391 "2020-07-10T03:06:44Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![lavanya\_kannan](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/lavanya_kannan/32/8545_2.png) [@lavanya\_kannan](https://community.neo4j.com/u/lavanya_kannan)\
**Post date:** [July 10, 2020, 3:06am UTC](https://community.neo4j.com/t/nested-apoc-statements-using-apoc-refactor-mergenodes/21391/1 "2020-07-10T03:06:44Z")

</div>

Hi,

I have a need to merge multiple nodes multiple times. The multiple nodes are disjoint and so I could attempt apoc.refactor.mergeNodes() in parallel. In any case, here is what I had by nesting it with apoc.periodic.iterate which worked well in APOC 3.5. With APOC 4.1, I think it is getting stuck (running for a long time with no successful result).

```auto
CALL apoc.periodic.iterate(
	"
	MATCH (p:GraphAlgos_Person) --> (b:alias) 
	WHERE p.person_updated = TRUE 
	WITH 
		b.entity_name AS entity_name, 
		b.GraphProperty_Scientific_Social_Cluster AS component,
		collect(b) AS alias_nodes
        WITH entity_name, 
        component, 
        alias_nodes,
        size(alias_nodes) as size_same_alias_nodes WHERE size_same_alias_nodes>1 
        RETURN entity_name, component, alias_nodes
	",
	"
	CALL apoc.refactor.mergeNodes(alias_nodes, {properties: {
    GraphProperty_triangles_scientific: 'combine',
    GraphProperty_scientific_degree: 'combine',
    GraphProperty_triangle_clustering: 'combine',
    `.*`: 'discard'
	}})
	YIELD node
	WITH node,
	reduce(x = 0, xi in node.GraphProperty_triangles_scientific| CASE (xi > x) WHEN TRUE THEN xi else x END) 
	AS GraphProperty_triangles_scientific,
	reduce(y = 0, yi in node.GraphProperty_scientific_degree| CASE (yi > y) WHEN TRUE THEN yi else y END) 
	AS GraphProperty_scientific_degree,
	reduce(z = 0, zi in node.GraphProperty_triangle_clustering_scientific| CASE (zi > z) WHEN TRUE THEN zi else z END) 
	AS GraphProperty_triangle_clustering_scientific
	SET node.GraphProperty_triangles_scientific = GraphProperty_triangles_scientific,
		node.GraphProperty_scientific_degree = GraphProperty_scientific_degree, 
		node.GraphProperty_triangle_clustering_scientific = GraphProperty_triangle_clustering_scientific,
		node.found_person_via_bulk_update = FALSE
	", {batchSize:1000, iterateList:true})
YIELD batches, total, errorMessages 
RETURN batches, total, errorMessages;

```

I would greatly appreciate what may cause the nested APOC statement to stall; or better yet, suggest alternate methods - perhaps using another APOC statement such as `apoc.cypher.mapParallel` would help? Let me know. Thanks.

Lavanya
