Search code examples
neo4jcountcypherrelationshipstore

Update all of my node properties with both the number of relationships to and from that node


So I was thinking of something alone the lines of:

MATCH (a:Entity)<--(x:Entity) SET a.links_to=count(x) MATCH (a:Entity)-->(x:Entity) SET a.links_from=count(x)

How might I write this correctly in Cypher? How might I do this in a fast and preferably parallel fashion, perhaps by using Apoc ?


Solution

  • This can be done very effectively with.

    Match (e:Entity)
    Set e.links_to = size((e)<--()),
          e.links_from = size((e)-->())
    

    If you want to use APOC to speed it up and run in parallel

    Call apoc.periodic.iterate(
    ' match (e:Entity) return e',
    ' set e.links_to = size((e)<--()),
          e.links_from = size((e)-->())',
    {batchSize:10000,parallel: true})