用Cypher查询Neo4j中的递归关系链 [英] Querying Recursive Relationship Chains in Neo4j with Cypher
问题描述
我有一个图形数据库,用于建模消息的元数据以及可以包含在这些消息中的字段.这些字段中的某些字段可以是组",它们是其他字段的组.我试图问Neo的是正在使用该组的消息是什么?".以下是可用于从消息到达组的路径类型的列表:
I have a graph database that is modeling the metadata for messages and the fields that can be contained on those messages. Some of those fields can be "groups" which are groups of other fields. What I am attempting to ask Neo is "what messages are using this group?". The following is a list of the path types that can be used to get from a message to a group:
message-[:INLINE]->group (the fields of a group are used inline on a message)
message-[:FIELDREF]->(fref)-[:FIELD]->(field)-[:DATATYPE]->group (the group is used as a data type by a field on the message)
第二条链是递归的.换句话说,-[:FIELDREF]->(fref)-[:FIELD]->(field)-[:DATATYPE]-(group)段可能会一遍又一遍,直到最终到达我感兴趣的组内.
The second chain is recursive. In other words, the -[:FIELDREF]->(fref)-[:FIELD]->(field)-[:DATATYPE]-(group) segment can happen over and over again before finally reaching the group I'm interested in.
所以,我想知道的是,如何在路径中的每个元素上要求重复的关系链,而不是多个关系链(例如,在关系名称后加*)?
So, what I want to know is, how do I ask for a repeating chain of relationships rather than just a multiple (e.g. * after the relationship name) on each individual element in the path?
回顾一下,您可以通过遍历[:INLINE]关系从一条消息进入一个组,然后可以跟随n个"fieldref-field-datatype-group"链.通过遍历n个"fieldref-field-datatype-group"链从一条消息中进行分组.
To recap, you can either get to a group from a message by traversing an [:INLINE] relationship, which can then follow n number of "fieldref-field-datatype-group" chains.. OR you can get to a group from a message by traversing n number of "fieldref-field-datatype-group" chains.
START group=node({sourceGroupId})
... ? ? ? ...
所以我想要类似[?:INLINE]-> 0..n的(fieldref-field-datatype-group)链.
So I want something like [?:INLINE]-> 0..n of (fieldref-field-datatype-group) chains.
有什么想法吗?
推荐答案
根据 http://docs.neo4j.org/chunked/milestone/query-match.html ...
12.2.13.可变长度关系 可以使用以下语法找到数量可变的关系→距离节点跳远的节点:-[:TYPE * minHops..maxHops]->. minHops和maxHops是可选的,默认分别为1和infinity.当没有界限时,可以省略点.
12.2.13. Variable length relationships Nodes that are a variable number of relationship→node hops away can be found using the following syntax: -[:TYPE*minHops..maxHops]->. minHops and maxHops are optional and default to 1 and infinity respectively. When no bounds are given the dots may be omitted.
下面是我想寻找的一个例子.我将最小值设置为2.
An example of what I think you seek is below. I set the minimum at two.
start n=node:node_auto_index(name='Neo') match n-[r:KNOWS*2..]-m return n as Neo,r,m
您可以在 http://console.neo4j.org 上逐字测试该查询
You can test this query verbatim at http://console.neo4j.org
这篇关于用Cypher查询Neo4j中的递归关系链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!