SRS
SRS

Reputation: 449

How to associate multiple value to a property using cypher queries in Neo4j

I have 2 nodes created in graph database, origin airport and destination airport. It is related by a property named 'delayed_by'. I need to associate multiple values to this property 'delayed_by'. There are various reasons for delays such as weather delay, carrier delay, nas delay and I need to link all of them to this property in graph database. I am expecting values as that of below tabular format in graph database

ORIGIN    DEST    carr_delay  weather_delay   nas_delay
ABE        ATL      492         56             56   

I used the below code for the relation:

MATCH (origin:origin_airport {name: row.ORIGIN}), 
(destination:dest_airport {name: row.DEST})
CREATE (origin)-[:delayed_by {carr_delay: row.carr_delay}]->(destination)
CREATE (origin)-[:delayed_by {weather_delay: row.weather_delay}]->    
(destination)
CREATE (origin)-[:delayed_by {nas_delay: row.nas_delay}]->(destination)

But the delayed_by relationship is getting mapped 3 times and it produces something like below:

 ORIGIN    DEST    carr_delay  weather_delay   nas_delay
 ABE        ATL      492         NULL             NULL  

 ORIGIN    DEST    carr_delay  weather_delay   nas_delay
 ABE        ATL      NULL         56            NULL 

 ORIGIN    DEST    carr_delay  weather_delay   nas_delay
 ABE        ATL      NULL         NULL            56   

I tried to use the below code as suggested in one other post,

 MATCH (origin:origin_airport {name: row.ORIGIN}), 
  (destination:dest_airport {name: row.DEST})
  CREATE (origin)
      -[:delayed_by {type: carr_delay, value: row.carr_delay}]->
      (destination)
  CREATE (origin)
      -[:delayed_by {type: weather_delay, value: row.weather_delay}]->
      (destination)
  CREATE (origin)
      -[:delayed_by {type: nas_delay, value: row.nas_delay}]->
      (destination)

But it is not accepting type:, value: properties and I am getting an error at type

Error: Client error: (400) Bad Request
Neo.ClientError.Statement.SyntaxError

How to add multiple value to a property using cypher queries in Neo4j?. I am writing these queries in R by connecting R to Neo4j.

Upvotes: 2

Views: 257

Answers (2)

Luanne
Luanne

Reputation: 19373

Here's what the LOAD CSV Cypher statement could look like-

LOAD CSV WITH HEADERS FROM "file:///Users/luanne/temp/so.csv" AS row
MERGE (origin:origin_airport {name: row.ORIGIN})
MERGE (destination:dest_airport {name: row.DEST})
MERGE (origin)-[r:delayed_by]->(destination)
SET  r.carr_delay=row.carr_delay, r.weather_delay=row.weather_delay, r.nas_delay=row.nas_delay

You'll want to MERGE the relationship between two airports to end up with exactly one delayed_by relationship between them. Or if you have a row per set of airports, you can change that MERGE of the relationship to a CREATE.

Upvotes: 2

stdob--
stdob--

Reputation: 29172

You have a typo, you forgot about quotes:

  CREATE (origin)
      -[:delayed_by {type: carr_delay, value: row.carr_delay}]->
      (destination)

=>

  CREATE (origin)
      -[:delayed_by {type: "carr_delay", value: row.carr_delay}]->
      (destination)

Upvotes: 1

Related Questions