【问题标题】:Neo4j: Join existing nodes in neo4j graphNeo4j:加入 Neo4j 图中的现有节点
【发布时间】:2016-03-07 06:12:03
【问题描述】:

问题陈述: 添加两个输入节点 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

我已经在上面解释了我的问题。我怎样才能实现它。

【问题讨论】:

  • 这是作业/面试题吗?另外:您还没有展示任何作品。这里不是发布“问题陈述”并让人们为您编写代码的地方。
  • 编辑您的问题。不要在评论中发布密码块。不可读。
  • @DavidMakogon 你现在能看到吗
  • 您没有正确使用合并。看我的回答。您需要合并各个部分。

标签: neo4j cypher py2neo


【解决方案1】:

这将涵盖所有四个点,附加点:

如果 n1 和 n2 不存在,则创建 n1 和 n2 并与边 [:rel] 连接。

MERGE (n1:Label {unique_prop: "unique_value"})
MERGE (n2:Label {unique_prop: "unique_value"})
MERGE (n1)-[:rel]->(n2);

【讨论】:

    猜你喜欢
    • 2018-09-10
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-23
    • 2016-05-01
    相关资源
    最近更新 更多