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

查看:13
本文介绍了创建时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.

当我尝试在这个数组上推送、弹出、取消移动等..."时,问题就出现了.如果我想在订单的前面添加一个新的关系,我必须创建一个新的关系,用它来链接父节点到子节点,然后给关系的 trueindex 属性添加一个 0 值.问题是已经存在一个 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 增加一)以反映正确的顺序.这对于小案例来说很好,但如果我计划让父节点拥有大量关系,那么每次我想添加、删除索引、弹出等时重写整个数组(关系集)都是一个问题并且仍然保持源自父节点的关系顺序.

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天全站免登陆