当节点之间没有直接关系时,从Neo4j中的csvfile向关系添加属性

时间:2016-05-14 13:19:24

标签: neo4j cypher

我有这三个节点(User,Tweet,Token),如:

:User with this property {userID}
:Token with this property {word}
:Tweet with this properties {tweetID, userID, tweetTxt}

目前的关系如下:

(:Tweet)<-[:MADE]-(:User) (:Token)<-[:CONTAINS {tweet_score}]-(:Tweet)

现在考虑以下csv文件:

userToken.csv
_________________________________________
token,userID,score
that_danielle,15990804,0.111140564157
foodies,15990804,0.159946268074
soft-launched,15990804,0.132826927255
email,60730027,0.0561669544423
email,60730027,0.105124263028
email,60730027,0.0453705868273
email,60730027,0.0967876752689
email,32785000,0.101566813224
you,60730027,0.0835723672219

我需要在“CONTAINS”关系中添加一个名为“user_score”的新属性,并且应该从userToken.csv文件中检索此分数。在下面的代码中,我尝试这样做:匹配“p”以包含用户拥有的所有:Token节点(不确定它是否真的有用!),然后使用这些“p”从csv添加“user_score”归档为“CONTAINS”关系:

USING PERIODIC COMMIT 500
LOAD CSV WITH HEADERS FROM "file:///..../userToken.csv" AS csvrow
MATCH p=(u:User)-[*2]->(tok:Token)
with p
where u.userID = toInt(csvrow.userID)
FOREACH (n IN nodes(p)| SET n.user_score = toFloat(csvrow.score) )

Here is how my nodes and relations look like!!!

这会返回错误:

"csvrow not defined (line 6, column 52 (offset: 258))
"FOREACH (n IN nodes(p)| SET n.user_score = toFloat(csvrow.score) )""
你能帮我解决一下吗?

P.S:不确定我问题的标题:/

1 个答案:

答案 0 :(得分:0)

USING PERIODIC COMMIT 500
LOAD CSV WITH HEADERS FROM "file:///..../userToken.csv" AS csvrow
MATCH p=(u:User)-[*2]->(tok:Token)
with p
where u.userID = toInt(csvrow.userID)
FOREACH (n IN nodes(p)| SET n.user_score = toFloat(csvrow.score) )

WITH中未传递的所有内容都不可用,因此当您不通过csvrow时,它不会再与查询的其余部分绑定。

然而,你绝对不需要在这里。其次,您的查询会将分数添加到路径中的所有节点,因此在用户节点,推文和令牌上。

如果你想把它放在CONTAINS关系上,你可以这样做:

USING PERIODIC COMMIT 500
LOAD CSV WITH HEADERS FROM "file:///..../userToken.csv" AS csvrow
MATCH (u:User)
where u.userID = toInt(csvrow.userID)
MATCH (u)-[:MADE]->(tweet)-[r:CONTAINS]->(token)
SET r.user_score = csvrow.score

但是,此查询还会将user_score添加到用户节点

评论后编辑(假设令牌节点上的属性键为name

USING PERIODIC COMMIT 500
LOAD CSV WITH HEADERS FROM "file:///..../userToken.csv" AS csvrow
MATCH (u:User)
where u.userID = toInt(csvrow.userID)
MATCH (u)-[:MADE]->(tweet)-[r:CONTAINS]->(token)
WHERE token.name = csvrow.token
SET r.user_score = csvrow.score