Cypher Linked LIst:如何按索引取消移位和替换 [英] Cypher Linked LIst: how to unshift and replace by index

查看:17
本文介绍了Cypher Linked LIst:如何按索引取消移位和替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照此处的建议使用 Neo/Cypher 创建链表结构:CYPHER 存储我创建时相同标签的节点关系的顺序

I am trying to create a Linked List structure with Neo/Cypher as per the recomendation here: CYPHER store order of node relationships of the same label when I create

(p:Parent)-[r1:PARENTID_RELTYPE]->(c1:Child)-[r2:PARENTID_RELTYPE]->(c2:Child)-[r3:PARENTID_RELTYPE]->(c3:Child)

但我无法理解所需事件序列的语法,以便将新节点移到结构上或用不同的节点替换特定索引.

But I am having trouble understanding the syntax for the required sequence of events in order to unshift new nodes onto the structure or replace a particular index with a different node.

我感到困惑的部分是我需要添加新节点并修复使旧节点保持在链表结构内的结构.每种类型(PARENTID_RELTYPE)都应该只有一个源自父节点的关系;但是每个父级可以有多种不同类型的关系.子节点可以在一个 LinkedList 中多次出现,并且一个子节点可以在多个 parent 的 LinkedList 或相同父级但不同关系类型的 LinkedList 中出现.

Where I am confused is the part where I need to add the new node and repair the structure that keeps the old node still inside the linked list sturcture. There should be only one relationship of each type (PARENTID_RELTYPE) that stems from a Parent node; But there can be multiple relationships of different type from each parent. Child nodes can be featured multiple times within a LinkedList and a child node could be featured in a LinkedList of multiple Parents or in a LinkedList of the same parent but a different relationship type.

因此,当我尝试取消换档时,可能会发生以下三种情况之一:

So one of three things could happen when I try to unshift:

  1. 没有通过 PARENTID_RELTYPE 链接到父级的现有子级

  1. There is no existing child that links to the parent by the PARENTID_RELTYPE

已经存在一个通过PARENTID_RELTYPE链接到父节点的子节点

There already exists a child node that links to the parent by PARENTID_RELTYPE

已经存在一个通过 PARENTID_RELTYPE 链接到父节点的子节点,该子节点只是我试图取消转移到链表结构上的子节点的副本(在这种情况下,预期的结果是在链表的零索引和第一索引中具有相同的子节点).

There already exists a child node that links to the parent by PARENTID_RELTYPE and that child node is simply a duplicate of the child node I am attempting to unshift onto the linked list structure (in which case the intended result is to have the same child node in the zero and first indices of the linked list).

上面提到的答案 url 极大地帮助我理解如何在 Neo/Cypher 中读取链表结构,但由于在 cypher 中处理条件的替代方式,我无法理解如何写入结构(并从他的结构中删除).

The answer url mentioned above helps me greatly in understanding how to read a linked list structure in Neo/Cypher but because of the alternative way for which conditionals are handled in cypher, I am having trouble understanding how to write to the structure (and also delete from he structure).

下面是我最初的尝试,但我对语法需要做什么感到有些困惑.

Below is my initial attempt to do so but I am somewhat baffled by what I need the syntax to do.

MATCH (a:%s {id: {_parentnodeid}}), (b:%s {id: {_id}})
MERGE  a-[relold:%s]->b
  ON CREATE
     SET relold.metadata = {_metaData}
  ON MATCH
     ...

非常感谢您提供的帮助.

I am very grateful for help you can provide.

推荐答案

[UPDATED]

在以下查询中,为简单起见,我假设:

In the following queries, for simplicity I pretend that:

  • 我们通过名称找到感兴趣的 Parent 节点.
  • 当前感兴趣的关系类型是Foo.

一般注意事项:

  • OPTIONAL MATCH 子句查找应该在插入的孩子之后的兄弟姐妹(如果有).
  • FOREACH 子句负责将该兄弟节点(如果有)链接到正在插入的子节点,然后删除与该兄弟节点的过时关系.
  • The OPTIONAL MATCH clauses find the sibling, if any, that should follow the child being inserted.
  • The FOREACH clauses take care of linking the that sibling, if any, to the child being inserted, and then deletes the obsolete relationship to that sibling.


  1. unshift id123 紧跟在 Parent 之后的 Child 节点:

  1. To unshift the Child having an id of 123 right after the Parent node:

MATCH (p:Parent {name:"Fred"})
OPTIONAL MATCH (p)-[r:Foo]->(c:Child)
WITH p, r, COLLECT(c) AS cs
MERGE (cNew:Child {id:123})
CREATE (p)-[rNew:Foo]->(cNew)
FOREACH (x IN cs | 
  CREATE (cNew)-[:Foo]->(x)
  DELETE r)
RETURN p, rNew, cNew;

  • 插入 id123Child 节点,位于索引 4(即,让它成为第 5 个孩子):

  • To insert the Child node having an id of 123 at index 4 (i.e, make it the 5th child):

    MATCH (p:Parent {name:"Fred"})
    MATCH (p)-[:Foo*3]->()-[r:Foo]->(c:Child)
    OPTIONAL MATCH (c)-[r1:Foo]->(c1:Child)
    WITH c, r1, COLLECT(c1) AS c1s
    MERGE (cNew:Child {id:123})
    CREATE (c)-[rNew:Foo]->(cNew)
    FOREACH (x IN c1s | 
      CREATE (cNew)-[:Foo]->(x)
      DELETE r1)
    RETURN c, rNew, cNew;
    

  • 用具有 id 的 Child 替换索引 4(即第 5 个子节点)处的 Child123:

  • To replace the Child at index 4 (i.e, the 5th child) with the Child having an id of 123:

    MATCH (p:Parent { name:"Fred" })
    MATCH (p)-[:Foo*4]->(c0)-[r:Foo]->(c:Child)
    OPTIONAL MATCH (c)-[r1:Foo]->(c1:Child)
    WITH c0, r, c, r1, COLLECT(c1) AS c1s
    MERGE (cNew:Child { id:123 })
    CREATE (c0)-[rNew:Foo]->(cNew)
    DELETE r, c
    FOREACH (x IN c1s | 
       CREATE (cNew)-[:Foo]->(x)
       DELETE r1)
    RETURN c0, rNew, cNew;
    

    注意:DELETE r, c 子句总是删除被替换的节点 (c).这仅适用于您真正想要发生的情况,并且只有在 c 没有 r 以外的关系时才会成功.要探索如何满足更具体的需求,请提出一个新问题.

    Note: The DELETE r, c clause always deletes the node being replaced (c). That is only suitable if that is what you actually want to happen, and will only succeed if c does not have relationships other than r. To explore how to address more specific needs, please ask a new question.

    这篇关于Cypher Linked LIst:如何按索引取消移位和替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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