Search code examples
neo4jcypherneo4j-apoc

Merge nodes with apoc and average property values


When merging nodes with:

apoc.refactor.mergeNodes(nodes, {properties:'combine', mergeRels:true})

The nodes' relationships will be merged as well, and their properties will be concatenated in a list:

(merged_nodes)-[merged_edges]-()

merged_edges.weight = [2,4,6]

Instead of a list I want an average

merged_edges.weight = avg([2,4,6]) = 4

How can I do that?


Solution

  • Set the average after you've done the merge, there's an APOC function to get the average of a list of values:

    ...
    MATCH (merged_nodes)-[merged_edges]-()
    SET merged_edges.weight = apoc.coll.avg(merged_edges.weight)