Search code examples
neo4jcypherneo4j-apoc

Neo4j: How can I implement apoc.atomic.update in my cypher statement?


I have a Cypher import for the first import and the new_neo_test_1.csv file that has the following columns: From, To, Sender_Sub_Fld, DateTime, Url, and FileHash

LOAD CSV WITH HEADERS FROM ("file:///sessions/new_neo_test_1.csv") AS row
MERGE (a:Sender { name: row.From, domain: row.Sender_Sub_Fld, datetime: datetime(row.DateTime) })
MERGE (b:Recipient { name: row.To, datetime: datetime(row.DateTime) })
WITH a,b,row
WHERE row.Url = "false" AND row.FileHash = "false"
CALL apoc.merge.relationship(a, row.Outcome2, {}, {datetime: datetime(row.DateTime)}, b, {}) YIELD rel as rel1
RETURN a,b

My goal is to use something like the following for the rest of the imports. I would like to update the relationships datetime property if the node already exists. Kindof a last seen property.

I have tried variations of apoc.atomic.update like the following but cant seem to get it to work.

LOAD CSV WITH HEADERS FROM ("file:///sessions/new_neo_test_2.csv") AS row 
MERGE (a:Sender { name: row.From, domain: row.Sender_Sub_Fld, datetime: datetime(row.DateTime) }) 
MERGE (b:Recipient { name: row.To, datetime: datetime(row.DateTime) }) 
WITH a,b,row 
WHERE row.Url = "false" AND row.FileHash = "false" 
CALL apoc.merge.relationship(a, row.Outcome2, {}, {}, b) YIELD rel as rel1 
CALL apoc.atomic.update(rel1, datetime, datetime(row.DateTime)) YIELD datetime as newdatetime 
RETURN a,b

Or if there is another suggestion to solve my problem that would be greatly appreciated.


Solution

  • You should only use values that uniquely identify a node in the MERGE clauses of your non-first imports. Otherwise, you can cause new nodes to be created. For example, your MERGE clauses should omit the datetime properties (since their values might change between import files). After each MERGE, you can use SET to update the datetime.

    This might work for you:

    LOAD CSV WITH HEADERS FROM ("file:///sessions/new_neo_test_2.csv") AS row
    WITH row, datetime(row.DateTime) AS dt
    MERGE (a:Sender {name: row.From, domain: row.Sender_Sub_Fld})
    SET a.datetime = dt
    MERGE (b:Recipient {name: row.To})
    SET b.datetime = dt
    WITH a, b, row, dt
    WHERE row.Url = "false" AND row.FileHash = "false" 
    CALL apoc.merge.relationship(a, row.Outcome2, {}, {}, b) YIELD rel
    SET rel.datetime = dt
    RETURN a, b
    

    NOTE: There is no apparent need for using apoc.atomic.update, so this query does not.