Eizy
Eizy

Reputation: 351

how to create relationship using cypher

I have been learning neo4j/cypher for the last week. I have finally been able to upload two csv files and create a relationship,"captured". However, I am not fully confident in my understanding of the code as I was following the tutorial on the neo4j site. Could you please help me confirm what I did is correct.

I have two csv files, a "cap.csv" and a "survey.csv". The survey table contains data of each unique survey conducted at the survey sites. the cap table contains data of each unique organisms captured. In the cap table I have a foreign key, "survey_id", which in the Postgres db you would join to the p.key in the survey table.

I want to create a relationship, "captured", showing each unique organsism that was captured based on the "date" column in the survey table.

Survey table | lake_id | date |survey_id | duration | | -------- | -------------- | --| -- | 1 | 05/27/14 |1 | 7 | | 2 | 03/28/13 | 2|10 | | 2 | 06/29/19 | 3|23 | | 3 | 08/21/21 | 4|54 | | 1 | 07/23/18 | 5|23 | | 2 | 07/22/23 | 6|12 |

Capture table | cap_id | species |capture_life_stage | weight | survey_id | | -------- | -------------- | --| -----|---| | 1 | a |adult | 10 | 1| | 2 | a | adult|10 | 2 | | 3 | b | juv|23 | 3 | | 4 | a | adult|54 | 4 | | 5 | b | juv|23 | 5 | | 6 | c | juv |12 | 6 |

LOAD CSV WITH HEADERS FROM 'file:///cap.csv' AS row
WITH
row.id as id, 
row.species as species,
row.capture_life_stage as capture_life_stage,
toInteger(row.weight) as weight,
row.survey_id as survey_id
MATCH (c:cap {id: id})
MERGE (s) - [rel:captured {survey_id: survey_id}] ->(c)
return count(rel)

I am struggling to understand the code I wrote above. I followed the neo4j tutorial exactly but used my data (https://neo4j.com/developer/desktop-csv-import/).

  1. I am fairly confident from data checks, but did the above code create the "captured" relationship showing each unique organism captured on that unique survey date? Based on the visual I can see I believe it did but I don't fully understand each step in the code.

  2. What is the purpose of the MATCH (c:cap {id: id}) in the code?

Upvotes: 1

Views: 193

Answers (1)

jose_bacoy
jose_bacoy

Reputation: 12684

The code below

 MATCH (c:cap {id: id})

is the same as

 MATCH (c:cap)
 Where c.id = id

It is a shorter way of finding Captured node based on id and then you are creating a relationship with Survey node.

Question: s is not defined in your query. Where is it?

Upvotes: 1

Related Questions