Querying Recursive Relationship Chains in Neo4j with Cypher

孤街浪徒 提交于 2019-12-11 01:25:52

问题


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)

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?

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})
... ? ? ? ...

So I want something like [?:INLINE]-> 0..n of (fieldref-field-datatype-group) chains.

Any thoughts?


回答1:


According to the Cypher reference at http://docs.neo4j.org/chunked/milestone/query-match.html ...

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.

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

You can test this query verbatim at http://console.neo4j.org



来源:https://stackoverflow.com/questions/16718784/querying-recursive-relationship-chains-in-neo4j-with-cypher

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!