neo4j increment count when adding relationship

The following Cypher query creates the cnt property and sets all to 0 when I run it the first time. The exact query run a second time updates the cnt property. Is it possible to increment node cnt for each relation that is added upon the load without running twice?

LOAD CSV WITH HEADERS FROM "file:///graph_data.csv" AS row
MERGE (t1:Term {word:row.term1})
MERGE (t2:Term {word:row.term2})
WITH t1, t2, row
MERGE (t1)-[:TOGETHER {id:row.id}]-(t2)
ON MATCH SET
    t1.cnt = t1.cnt+1,
    t2.cnt = t2.cnt+1
ON CREATE SET
    t1.cnt=0,
    t2.cnt=0
RETURN t1,t2

1 answer

  • answered 2018-07-11 17:30 cybersam

    It may not be necessary for you to store and maintain cnt properties at all.

    For instance, to find out how many TOGETHER relationships a specific Term has:

    MATCH (t:Term {word: 'cat'})
    RETURN COUNT((t)-[:TOGETHER]-()) AS cnt;