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

天涯浪子 提交于 2019-12-24 01:17:22

问题


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.


回答1:


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.




回答2:


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)


来源:https://stackoverflow.com/questions/37899643/how-to-associate-multiple-value-to-a-property-using-cypher-queries-in-neo4j

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!