如何在neo4j中存储这种图以便快速遍历?
这是一个图,其节点同时存在于许多连接的组件中,因为节点的关系是边组的集合,使得每个边组一次只能存在一条边。我需要能够找到节点所在的所有连接组件。在 neo4j 中存储此图以快速找到节点存在的所有连接组件的最佳方法是什么?有没有办法使用内置的遍历来做到这一点?
另外:这种图有名字吗?我将不胜感激任何帮助/想法。
更新:
抱歉,没有说清楚。所有节点都属于同一类型。节点具有可变数量的边组。需要为特定的连接组件选择每个边组中的一条边。我将尝试通过示例进行解释:
Node x1 is related to: (x2 or x3 or x4) AND (x5 or x6) AND (x7)
Node x2 is related to: (x8) AND (x9 or x10)
所以 x1 的第一个边组是 (x2, x3, x4)
,它的第二个边组是 (x5, x6)
,它的第三个边组是(x7)
。
因此,这里有一些 x1
存在的连接组件:
CC1:
x1 is related to: x2, x5, x7
x2 is related to: x8 x9
CC2:
x1 is related to: x2, x6, x7
x2 is related to: x8, x9
CC3:
x1 is related to: x3, x5, x7
CC4:
x1 is related to: x3, x6, x7
等。
我很感谢您对此的帮助。
更新2:
我想如果这个问题有答案的话我就能做到这一点: 如何在 Neo4j 遍历的每一步中指定使用哪种关系类型作为当前节点的函数?
This is a graph whose nodes exist in many connected components at once because a node's relationships are a collection of edge groups such that only one edge per edge group can be present at once. I need to be able to find all of the connected components that a node exists in. What would be the best way to store this graph in neo4j to quickly find all of the connected components that a node exists in? Is there a way to use the built in traversals to do this?
Also: is there a name for this kind of graph? I'd appreciate any help/ideas.
Update:
Sorry for not being clear. All nodes are of the same type. Nodes have a variable number of edge groups. Exactly one edge from each edge group needs to be chosen for a particular connected component. I'm going to try to explain through example:
Node x1 is related to: (x2 or x3 or x4) AND (x5 or x6) AND (x7)
Node x2 is related to: (x8) AND (x9 or x10)
So x1's first edge group is (x2, x3, x4)
, its second edge group is (x5, x6)
, and its third edge group is (x7)
.
So here are a few connected components that x1
exists in:
CC1:
x1 is related to: x2, x5, x7
x2 is related to: x8 x9
CC2:
x1 is related to: x2, x6, x7
x2 is related to: x8, x9
CC3:
x1 is related to: x3, x5, x7
CC4:
x1 is related to: x3, x6, x7
etc.
I'm grateful for your help in this.
Update2:
I think I'll be able to do this if there's an answer to this question:
How can I specify which relationship type to use as a function of the current node at every step of a traversal with neo4j?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我理解你的问题的方式是你有许多节点,让我们称它们为 X 节点,它们连接到许多类型节点(或类似的东西),让我们称这些节点为 T 节点。一个 X 节点可以与多个 T 节点有连接,但与每个 T 节点只有一个连接,或者可能与每种 T 节点只有一个连接(这里的描述有点模糊)。
我对此进行建模的方法是为每个(某种)T 节点使用一个RelationshipType。然后您可以使用 x_node.getRelationships(T_TYPE_ONE, T_TYPE_TWO, ...etc...) 从 X 节点获取所有 T 节点。当您改变 X 节点时,您需要维护不变量,即它与每种(一种)T 节点最多只能有一种关系。您可以使用 x_node.getSingleRelationship(THE_T_TYPE_YOURE_MUTATING) 来完成此操作,如果返回 null,则可以安全地添加该类型的新关系,如果它返回关系,则必须先将其删除,然后才能添加新关系。
该模型的 ASCII-art 示例(按照我的解释):
在上面的示例中,两个 X 节点都是 T_ONE 组件的一部分,但 x1 是 T_ONE 组件 t1a 的一部分,x2 是 t1b 的一部分。它们都是 T_THREE 组件 t3a 的一部分,那么 x1 是 T_TWO 组件 t2a 的一部分,x2 是 T_FOUR 组件 t4a 的一部分。在这个例子中查询看起来像这样:
更新看起来像这样:
如果我误解了您的要求,请告诉我,我很乐意为您更新的描述提供帮助。
They way I understand your question you have a number of nodes, let's call them X nodes, that are connected to a number of type nodes (or something similar), let's call these nodes T nodes. An X node can have connections to multiple T nodes, but only one connection to each T node, or possibly only one connection to each kind of T node (your description is a bit fuzzy here).
The way I would model this is by using one RelationshipType for each (kind of) T node. Then you can use x_node.getRelationships(T_TYPE_ONE, T_TYPE_TWO, ...etc...) to get all the T nodes from an X node. When you mutate an X node is where you need to maintain your invariant that it can only have at most one relationship to each (kind of) T node. You do this by using x_node.getSingleRelationship(THE_T_TYPE_YOURE_MUTATING), if that returns null, it's safe to add a new relationship of that type, if it returns a relationship, you will have to remove it before you can add the new one.
ASCII-art example of this model (as I interpret it):
In the example above both X nodes are part of T_ONE components, but x1 is part of T_ONE component t1a and x2 is part of t1b. They are both part of T_THREE component t3a, then x1 is part of T_TWO component t2a, and x2 is part of T_FOUR component t4a. Querying in this example would look something like:
And updating would look like this:
Please let me know if I've misinterpreted your requirements, and I'll be happy to give your updated description a stab.
关于另一个查询,我在 如何在 Neo4j 遍历的每一步中指定使用哪种关系类型作为当前节点的函数?
基本上,不要使用遍历器,而是使用更直接的 node.getRelationship* API 并构建您自己的迭代以进行细粒度控制。
这能解决你的问题吗?
/彼得·纽鲍尔
Regarding the other query, I pointed out some possibilities for fine grained functions at How can I specify which relationship type to use as a function of the current node at every step of a traversal with neo4j?
Basically, don't use a traverser but the more direct node.getRelationship* API and build your own iteration for fine grained control.
Does that solve your problem?
/peter neubauer