简体   繁体   English

Neo4j:以特定的关系顺序查询两个节点之间的路径,并且仅知道最后一个节点的标签

[英]Neo4j: Query for a path between two nodes in a specific order of relationships and knowing only label of the last node

For example if I have a complex graph having the following labels and relationships: 例如,如果我有一个具有以下标签和关系的复杂图形:

N1-a->N2-b->N3-c->N4 N1-a-> N2-b-> N3-c-> N4

N2-d-N5-e->N3 N2-d-N5-e-> N3

etc. 等等

Now, I want to find a path from (:N1{id:'xyz'}) to any node of type N4 using Cypher but I want the relationships to be in the same order ie a,b,c. 现在,我想使用Cypher查找从(:N1 {id:'xyz'})到N4类型的任何节点的路径,但我希望关系的顺序相同,即a,b,c。

Also, if there are no nodes of type N3 connecting to a node of type N4, I would like to return the path until N3 另外,如果没有N3类型的节点连接到N4类型的节点,我想返回路径直到N3

I was wondering if there is a way to do this. 我想知道是否有办法做到这一点。 Can someone please help? 有人可以帮忙吗? I am new to Neo4j 我是Neo4j的新手

If you explicitly know the relationships to traverse, then you should be able to do this with Cypher, though your condition to return N3 if there is no N4 may be tricky. 如果您明确知道要遍历的关系,那么您应该可以使用Cypher进行此操作,尽管如果没有N4,则返回N3的条件可能很棘手。

Is the label of N3 known, or do you just want the path as far as it can go? N3的标签是否已知,还是您只想尽可能地走这条路? Also if nodes of label N4 are encountered along the path instead of just at the end, do you want those as well, or are you only interested in N4 at the end of the relationship chain? 另外,如果沿着路径而不是仅在路径末尾遇到标签N4的节点,您是否也想要这些节点,还是只对关系链末端的N4感兴趣?

Also, are you interested in all possible paths found, or do you just need a single path, if it exists? 另外,您是否对找到的所有可能路径感兴趣,或者仅需要一条路径(如果存在)?

This query should work if you explicitly know (and can define in the query) the relationships to traverse, and if you only need a single path if it exists, and if you're only interested in a node of label N4 at the end (or the node just before, if there is no N4 at the end): 如果您明确知道(并可以在查询中定义)要遍历的关系,并且如果只需要一个路径(如果存在),并且只对结尾处的标签N4感兴趣,则此查询应该可以工作(或之前的节点(如果末尾没有N4):

MATCH p=(:N1{id:'xyz'})-[:a]->()-[:b]->()-[:c*0..1]->(last)
WHERE length(p) = 3 and last:N4 OR length(p) = 2
WITH p
ORDER BY length(p) DESC
LIMIT 1
RETURN p

If the criteria is more complex, you may need APOC path expander procedures for this. 如果条件更复杂,则可能需要APOC路径扩展程序。

For using APOC path expander, provided you're using one of the APOC releases from Winter 2018 or newer, you can take advantage of the new sequences feature, which lets you define a repeating sequence of node labels and relationship types. 对于使用APOC路径扩展器,如果您使用的是2018年冬季或更早版本的APOC版本,则可以利用新的序列功能,该功能可让您定义节点标签和关系类型的重复序列。 In this case we'll limit the repetition with the maxLevel config parameter. 在这种情况下,我们将使用maxLevel config参数限制重复。

MATCH (start:N1{id:'xyz'})
CALL apoc.path.expandConfig(start, {sequence:'N1, a>, N2, b>, N3, c>, N4', maxLevel:3}) YIELD path
RETURN path
ORDER BY length(path) DESC
LIMIT 1

If you don't care (or don't know) the labels in the path (with the exception of the last label N4), you can use * for the labels, like so: 如果您不关心(或不知道)路径中的标签(最后一个标签N4除外),则可以将*用作标签,如下所示:

sequence:'*, a>, *, b>, *, c>, N4'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM