创建时,相同标签的节点关系的CYPHER存储顺序 [英] CYPHER store order of node relationships of the same label when I create

查看:69
本文介绍了创建时,相同标签的节点关系的CYPHER存储顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个源于一个节点的关系.这些关系中的每一个都具有相同的标签.关系指向子节点(不一定是唯一的).在通过此关系标签获取链接到父级的所有子节点之后,我通过称为trueindex的关系属性对其进行排序.然后,我有一个节点数组,我的客户可以按照正确的排序顺序对其进行遍历.

I have multiple relationships that stem from a node. Each of these relationships are of the same label. The relationships point to a child node (not neccesarily unique). After I fetch all child nodes that link to the parent by this relationship label, I order them by a relationship property called trueindex. I then have an array of nodes that my client can iterate through in the correct sort order.

当我尝试推,弹出,松开等"到此阵列上时,问题就来了.如果要在订单的前面添加新的关系,则必须创建一个新的关系,使用它将父级链接到子节点,然后将0值添加到关系trueindex属性.问题在于,已经有一个trueindex值为零的关系,并且我需要执行某种casecading函数,以增加所有其他关系(所有类型都源自同一父节点的相同类型)的trueindex.我正在尝试找到一种免费获取此类似数组"索引号功能的方法

The problem comes when I try to "push, pop, unshift, etc..." onto this array. If I want to add a new relationship to the front of the order, I have to create a new relationship, use it to link the parent to the child node, then add a 0 value to the relationships trueindex property. The problem is that there already is a relationship with a trueindex value of zero and I need to perform somesort of casecading function that increments the trueindex of all other relationships (of the same type that all stem from that same parent node). I am trying to find a way to get this "array-like" index number functionality for free

我认为这样做的唯一方法是首先删除该特定标签源自父级的所有关系.然后重写整个数组(具有将其trueindex增加1的所有现有关系),以反映正确的顺序.这在小情况下很好,但是如果我计划父节点具有大量关系,那么每次我要添加,按索引,弹出等方式删除整个数组(关系集)时,都会遇到问题并且仍然保持源于父节点的关系顺序.

The only way I can think to do so is to first delete all relationships of that particular label that stem from the parent. And then rewrite the entire array (with all the prexisting relationship that increment their trueindex by one) in order to reflect the correct order. This is fine for small cases but if I plan for a parent node to have a significant amount of relationships, then its a problem to rewrite the entire array (set of relationships) every time I want to add, remove by index, pop, etc and yet still maintain an order of relationships that stem from the parent node.

Neo4j是否具有某种关系功能,可以在创建新关系时以正确的顺序写入?

Does Neo4j have some sort of relationship feature to write to the correct order when creating new relationships?

非常感谢您提供的建议.

I am very grateful for advise you can offer.

推荐答案

尝试将子节点保留在链接列表中.结构看起来像

Try keeping the child nodes in linked lists. The structure will look something like

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

这维护了子节点的顺序,并允许您通过两种方式改善与结构的交互:

This maintains the order of your child nodes and allows you to improve your interaction with the structure in two ways:

  1. 将新节点插入此结构仅涉及更改节点要移动的位置之前"和之后"的关系,而不是整个结构.例如,要在c1c2之间插入newc,请删除r2,创建newc并创建从c1newcc2:NEXT关系.与其他操作类似:您的所有更改现在都位于结构内.

  1. Inserting a new node into this structure only involves changes to the relationships 'before' and 'after' the place that node is going, as opposed to the entire structure. For example, to insert newc between c1 and c2, you delete r2, create newc and create :NEXT relationships from c1 to newc to c2. Analogous for other operations: all your alterations are now local within the structure.

您使用关系及其类型来构造数据,而不是关系属性.这样更灵活,几乎总是表现更好(有时表现更好).

You use relationships and their types to structure your data, instead of relationship properties. This is more flexible and almost always more performant (sometimes much more performant).

要从此结构中读取一个孩子,您现在可以使用trueindex在链接列表中声明要查找节点的深度,即

To read a single child from this structure you now use your trueindex to declare the depth in the linked list at which to find the node, i.e.

MATCH (parent:Parent {parentId: 1234})-[:CHILDREN]->()-[:NEXT*3]->(child)
RETURN child

并检索包含所有子项的父项

and to retrieve a parent with all its children

MATCH (parent:Parent {parentId: 1234})-[:CHILDREN|NEXT*]->(child)
RETURN parent, COLLECT(child) as children

这篇关于创建时,相同标签的节点关系的CYPHER存储顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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