Rajat
Rajat

Reputation: 107

Update existing relationship property value in Neo4j using CSV

I already have some data in Neo4j, data is modelled in the below fashion :

:A {ID:"123",Group:"ABC",Family:"XYZ"}
:B {ID:"456",Group:"ABC",Family:"XYZ"})
(:A)-[:SCORE{score:'2'}]-(:B)

Please find the attached image for more clarification how data looks like currently.enter image description here

Now, I am importing some new data through CSV file which has 5 columns

  1. A's ID
  2. B's ID
  3. Score through which A is attached to B
  4. Group
  5. Family

In the new data there can be some new A Ids or some new B Ids

Question :

  1. I want to create those new nodes of type A and B and create a relationship 'Score' and assigning score as the value of relationship type 'Score' between them
  2. there are chances that there already existing scores between A and B might have changed. So i want to just update the previous score with the new one.

How to write cypher to achieve the above problem using CSV as import.

I used the below cypher query to model data for the first time:

using periodic commit LOAD CSV WITH HEADERS FROM "file:///ABC.csv" as line Merge(a:A{ID: line.A,Group:line.Group,Family:line.Family})
Merge(b:B{ID: line.A,Group:line.Group,Family:line.Family})
Merge(a)-[:Score{score:toFloat(line.Score)}]-(b)

Note: Family and Group are same for both type of nodes 'A' and 'B'

Thanks in advance.

Upvotes: 1

Views: 655

Answers (1)

Dave Bennett
Dave Bennett

Reputation: 11216

You can MERGE the relationship and set the score after the fact so it does not create new SCORE relationships for every new value.

USING PERIODIC COMMIT
LOAD CSV WITH HEADERS FROM "file:///ABC.csv" AS line
MERGE (a:A {ID: line.A, Group:line.Group, Family:line.Family})
MERGE (b:B {ID: line.A, Group:line.Group, Family:line.Family})
MERGE (a)-[score:SCORE]-(b)
SET score.score = toFloat(line.Score)

Upvotes: 1

Related Questions