Neo4j:加入 neo4j 图中的现有节点
Neo4j: Join existing nodes in neo4j graph
问题陈述:
添加两个输入节点 n1 和 n2,边方向为 n1-[:rel]->n2
1. if n1-[:rel]->n2 exists ignore
2. if n1 and n2 exists but not with [:rel]
create a new relationship edge between the existing nodes
3. if n1 doesnt exist
create n1 and join n1 and existing n2 with edge [:rel]
4. if n2 doesnt exist
create n2 and join n2 and existing n1 with edge [:rel]
查询:
INSERT_QUERY = ''' FOREACH(t IN {term_mod_pair_list}|
MERGE(tt:target_Word {type:'target_term',word:t[0]['word'],pos:t[0]['tag']})-
[:MODIFIER]->(mod:mod_Word {type:'a-mod',word:t[1]['word'],pos:t[0]['tag']}) ) '''
我发现合并正在创建重复节点,经过一些发现我发现合并与精确模式匹配。
所以如果 n1-[:rel]->n2
存在,新添加的关系 n1-[:rel]->n3
将创建另一个新节点 n1
上面我已经解释了我的问题。我怎样才能实现它。
这将涵盖所有四点以及附加点:
如果 n1 和 n2 不存在,则创建 n1 和 n2 并用边 [:rel] 连接。
MERGE (n1:Label {unique_prop: "unique_value"})
MERGE (n2:Label {unique_prop: "unique_value"})
MERGE (n1)-[:rel]->(n2);
问题陈述: 添加两个输入节点 n1 和 n2,边方向为 n1-[:rel]->n2
1. if n1-[:rel]->n2 exists ignore
2. if n1 and n2 exists but not with [:rel]
create a new relationship edge between the existing nodes
3. if n1 doesnt exist
create n1 and join n1 and existing n2 with edge [:rel]
4. if n2 doesnt exist
create n2 and join n2 and existing n1 with edge [:rel]
查询:
INSERT_QUERY = ''' FOREACH(t IN {term_mod_pair_list}|
MERGE(tt:target_Word {type:'target_term',word:t[0]['word'],pos:t[0]['tag']})-
[:MODIFIER]->(mod:mod_Word {type:'a-mod',word:t[1]['word'],pos:t[0]['tag']}) ) '''
我发现合并正在创建重复节点,经过一些发现我发现合并与精确模式匹配。
所以如果 n1-[:rel]->n2
存在,新添加的关系 n1-[:rel]->n3
将创建另一个新节点 n1
上面我已经解释了我的问题。我怎样才能实现它。
这将涵盖所有四点以及附加点:
如果 n1 和 n2 不存在,则创建 n1 和 n2 并用边 [:rel] 连接。
MERGE (n1:Label {unique_prop: "unique_value"})
MERGE (n2:Label {unique_prop: "unique_value"})
MERGE (n1)-[:rel]->(n2);