“Neo4j在使用apoc.merge.relationship()时添加属性到关系”

huangapple go评论54阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年1月3日 23:35:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581296.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定