Neo4j - 更改关系类型在 Web 界面数据浏览器中不起作用 [英] Neo4j - changing relationship type not working in web interface data browser

查看:14
本文介绍了Neo4j - 更改关系类型在 Web 界面数据浏览器中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些节点 (n1) 和 (n2) 具有相同的名称但具有独特的属性并共享节点对关系 (s1)-[r]->(e1) 如下:

I have some nodes (n1) and (n2) that have the same name but unique properties and share a node-pair relationship (s1)-[r]->(e1) as follows:

我正在尝试一次性创建 s1 和 e1 之间的新关系,以便新关系基于 n1 和 n2 的 id 是唯一的,但这些关系的类型与之前的关系相同,并且然后删除旧的关系:

I am trying to do a one-off where I create new relationships between s1 and e1 so that the new relationships are unique, based on id of n1 and n2, but the relationships have the same type as the previous relationship, and then delete the old relationship:

我发现了以下内容:neo4j cypher:如何改变关系的类型,引导我进行以下查询:

I found the following: neo4j cypher: how to change the type of a relationship which guided me toward the following query:

MATCH (e1)<-[:ENDS_WITH]-(n {name:"same"})-[:STARTS_WITH]->(s1),
(s1)-[r]->(e1)
CREATE (s1)-[r2:NEWREL]->(e1)
// copy properties and set n_id unique
SET r2 = r, r2.n_id = n.n_id
WITH r
DELETE r

完全有道理.但是,当我在 Neo4j Web 界面数据浏览器中尝试此操作时,n_id 属性设置正确,但 type(r2) 设置为字符串NEWREL"而不是预期的REL"

Makes perfect sense. However, when I attempt this in the Neo4j web interface data browser, the n_id property gets set properly, but the type(r2) gets set as string "NEWREL" rather than the expected "REL"

我尝试在 NEWREL 周围回勾并从 type(r) 传递一个临时变量.不工作.

I tried back ticks around the NEWREL and passing a temporary variable from type(r). Not working.

推荐答案

[UPDATED]

Cypher 只允许您使用 硬编码 类型创建关系,因此通常无法实现您想要的.

Cypher only allows you to create a relationship using a hardcoded type, so in general there is no way for you to achieve what you want.

但是,如果您事先知道所有可能的关系类型,则有一种解决方法.在以下示例中,假设可能的关系类型为 REL1REL2REL3REL4:>

However, if you know beforehand all the possible relationship types, there is a workaround. In the following example, suppose the possible relationship types are REL1, REL2, REL3, and REL4:

MATCH (e1)<-[:ENDS_WITH]-(n {name:"same"})-[:STARTS_WITH]->(s1)-[r]->(e1)
WITH s1, e1, r, n,
  CASE TYPE(r)
    WHEN 'REL1' THEN {rel1:[1]}
    WHEN 'REL2' THEN {rel2:[1]}
    WHEN 'REL3' THEN {rel3:[1]}
    WHEN 'REL4' THEN {rel4:[1]}
  END AS todo
FOREACH(x IN todo.rel1 | CREATE (s1)-[rr:REL1]->(e1) SET rr = r, rr.n_id = n.n_id)
FOREACH(x IN todo.rel2 | CREATE (s1)-[rr:REL2]->(e1) SET rr = r, rr.n_id = n.n_id)
FOREACH(x IN todo.rel3 | CREATE (s1)-[rr:REL3]->(e1) SET rr = r, rr.n_id = n.n_id)
FOREACH(x IN todo.rel4 | CREATE (s1)-[rr:REL4]->(e1) SET rr = r, rr.n_id = n.n_id)
DELETE r

只有 FOREACH 子句中的一个会实际创建一个新关系(正确类型)并复制现有属性.

Only one of the FOREACH clauses will actually create a new relationship (of the correct type) and copy the existing properties.

上述解决方案适用于neo4j 3.0,但neo4j 2.3 似乎有一个导致查询失败的错误.如果 r 关系中没有任何值得复制的属性,请将所有 SET rr = r, rr.n_id = n.n_id 子句更改为 SET rr.n_id = n.n_id 应该在 2.3 上避免这个问题.

The above solution works in neo4j 3.0, but neo4j 2.3 seems to have a bug that makes the query fail. If you do not have any properties on the r relationship worth copying, changing all the SET rr = r, rr.n_id = n.n_id clauses to SET rr.n_id = n.n_id should avoid that issue on 2.3.

这篇关于Neo4j - 更改关系类型在 Web 界面数据浏览器中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆