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

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

问题描述

我正在尝试根据此处的建议使用Neo/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中被多次标注,并且子节点可以在多个父节点的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.

推荐答案

[更新]

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

In the following queries, for simplicity I pretend that:

  • 我们通过名称找到了感兴趣的Parent节点.
  • 当前关注的关系类型为Foo.
  • We find the Parent node of interest by name.
  • The relationship type of current interest is 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. 要在Parent节点后紧随Child 123Child :

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;

  • 插入在索引4处具有id123Child节点(即,使其成为第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;
    

  • Child替换为索引4上的Child(即第5个孩子),而Childid123:

  • 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).仅当这是您真正想要发生的情况时才合适,并且仅当cr之外没有其他关系时才会成功.要探索如何满足更具体的需求,请提出一个新问题.

    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 Links LIst:如何取消移位并按索引替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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