英文:
Neo4j add property to relationship when using apoc.merge.relationship()
问题
以下是翻译好的部分:
我有以下的导入:
// 没有附件或链接
USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM ("file:///sessions/Hourly_Parsed/2019-12-10_00_hourly_parsed_mail_logs.csv") AS row
MERGE (a:Sender { name: row.From, domain: row.Sender_Sub_Fld, datetime: datetime(replace(row.DateTime, ' ', 'T'))})
MERGE (b:Recipient { name: row.To, datetime: datetime(replace(row.DateTime, ' ', 'T'))})
WITH a,b,row
WHERE row.Url = "false" AND row.FileHash = "false"
CALL apoc.merge.relationship(a, row.Outcome2, {}, {}, b) YIELD rel as rel1
RETURN a,b
请注意,代码部分没有进行翻译。
英文:
I have the following import:
// NO ATTACHMENT OR LINK
USING PERIODIC COMMIT 1000
LOAD CSV WITH HEADERS FROM ("file:///sessions/Hourly_Parsed/2019-12-10_00_hourly_parsed_mail_logs.csv") AS row
MERGE (a:Sender { name: row.From, domain: row.Sender_Sub_Fld, datetime: datetime(replace(row.DateTime, ' ', 'T'))})
MERGE (b:Recipient { name: row.To, datetime: datetime(replace(row.DateTime, ' ', 'T'))})
WITH a,b,row
WHERE row.Url = "false" AND row.FileHash = "false"
CALL apoc.merge.relationship(a, row.Outcome2, {}, {}, b) YIELD rel as rel1
RETURN a,b
As you can see I have added the datetime property to both the Sender
and Recipient
nodes. I would like to add this property to the relationship. The problem that I am running into is that my property is created using apoc.merge.relationship()
so that I can create the name based on the outcome in the rows column.
Can I add something below the CALL
portion to add the datetime: datetime(replace(row.DateTime, ' ', 'T'))
as a property to the relationship?
答案1
得分: 4
在相同的`apoc.merge.relationship`调用中,您可以为关系添加属性。以下是该apoc调用的签名:
```apoc.merge.relationship(startNode :: NODE?, relationshipType :: STRING?, identProps :: MAP?, props :: MAP?, endNode :: NODE?, onMatchProps = {} :: MAP?) :: (rel :: RELATIONSHIP?)```
所以对您来说应该适用:
```CALL apoc.merge.relationship(a, row.Outcome2, {}, {datetime:datetime(replace(row.DateTime, '' ', 'T''))}, b, {})```
请记住,这只会在创建新边时添加`datatime`属性。如果您在`apoc.merge.relationship`的最后一个参数中设置属性,那么该属性将在`MATCH`时添加。如果您想将属性包括在`MERGE`的`MATCH`部分中,可以将其设置为`apoc.merge.relationship`的第3个参数中。例如,如果您运行以下代码
CALL apoc.merge.relationship(a, "CONNECTS_TO", {time: "today"}, {}, b, {})
CALL apoc.merge.relationship(a, "CONNECTS_TO", {time: "tomorrow"}, {}, b, {})
您将得到两个`CONNECTS_TO`边,分别连接`a`和`b`,一个带有`time: "today"`,另一个带有`time:"tomorrow"`。但如果您运行以下代码
CALL apoc.merge.relationship(a, "CONNECTS_TO", {}, {time: "today"}, b, {})
CALL apoc.merge.relationship(a, "CONNECTS_TO", {}, {time: "tomorrow"}, b, {})
您将只得到一个`CONNECTS_TO`边,其属性为`time: "today"`。
英文:
You can add properties to relationships in the same apoc.merge.relationship
call. Here is the signature of that apoc call:
apoc.merge.relationship(startNode :: NODE?, relationshipType :: STRING?, identProps :: MAP?, props :: MAP?, endNode :: NODE?, onMatchProps = {} :: MAP?) :: (rel :: RELATIONSHIP?)
So this should work for you:
CALL apoc.merge.relationship(a, row.Outcome2, {}, {datetime:datetime(replace(row.DateTime, ' ', 'T'))}, b, {})
Remember this will add the datatime
property only when a new edge gets created. If you set a property in the last argument of apoc.merge.relationship
, then the property will be added on MATCH
. If you want to include the property in the MATCH
part of the MERGE
, you can set it in the 3rd argument of apoc.merge.relationship
. For example, if you run
CALL apoc.merge.relationship(a, "CONNECTS_TO", {time: "today"}, {}, b, {})
CALL apoc.merge.relationship(a, "CONNECTS_TO", {time: "tomorrow"}, {}, b, {})
you will end up with two CONNECTS_TO
edges between a
and b
, one with time: "today"
, and one with time:"tomorrow"
. But if you run the following instead
CALL apoc.merge.relationship(a, "CONNECTS_TO", {}, {time: "today"}, b, {})
CALL apoc.merge.relationship(a, "CONNECTS_TO", {}, {time: "tomorrow"}, b, {})
You will end up with only one CONNECTS_TO
edge that has time: "today"
as its property.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论