简体   繁体   English

Neo4j Cypher遍历 - 通过多种关系类型查找路径

[英]Neo4j Cypher traversal - find path by multiple relationship types

I have a schema, where nodes are connected by 2 types of relationship - r:A and r:B. 我有一个模式,其中节点通过两种类型的关系连接 - r:A和r:B。 I'm trying to write a pattern, which will find every path from node N to node M. This can be simply done by following cypher query: 我正在尝试编写一个模式,它将找到从节点N到节点M的每条路径。这可以通过以下cypher查询来完成:

match path = (n)-[:A|:B*]->(m) return path;

Unfortunately this is not what I need exactly. 不幸的是,这不是我所需要的。 I need to find every path from (n) to (m) where depth via relation r:A can be infinite, but along the way only limited number of r:B relations can be use. 我需要找到从(n)到(m)的每条路径,其中深度通过关系r:A可以是无限的,但沿途只能使用有限数量的r:B关系。 In happy day scenario the cypher query would look like this: 在快乐的一天场景中,密码查询将如下所示:

match path = (n)-[:A*|:B*0..3]->(m) return path;

However cypher does not allow this syntax. 但是,cypher不允许这种语法。 I can't solve this problem even with usage of another "helping" node on the way: 即使在路上使用另一个“帮助”节点,我也无法解决这个问题:

match path = (n)-[:A*]->()-[:B*0..3]->(m) return path;

This does not match my need also, because the nodes can be interconnected in any possible way. 这也不符合我的需要,因为节点可以以任何可能的方式互连。 For example: 例如:

(n)-[r:A]-()-[r:A]-()-[r:A]-(m)
(n)-[r:A]-(m)
(n)-[r:A]-()-[r:B]-()-[r:A]-()-[r:B]-()-[r:A]-()-[r:A]-(m)

Is there a way how this can be achieved? 有没有办法如何实现这一目标? If not in cypher, can it be done in gremlin / neo4j traversal api / embedded functions of spring data neo4j project? 如果不是在密码中,可以在gremlin / neo4j遍历api /嵌入式函数的spring数据neo4j项目中完成吗?

Thank's for the answers. 谢谢你的答案。

Try this: 尝试这个:

MATCH path = (n)-[:A|:B*]->(m) MATCH路径=(n) - [:A |:B *] - >(m)
WITH path, relationships(path) AS r, filter(rel in relationships(path) WITH path,relationships(path)AS r,filter(rel in relationships(path)
WHERE type(rel) = 'B') AS Brels WHERE type(rel)='B')AS Brels
WITH path, reduce(Bcount = 0, rel IN Brels | Bcount + 1) AS Bcount WITH path,reduce(Bcount = 0,rel IN Brels | Bcount + 1)AS Bcount
WHERE Bcount <= 3 在哪里Bcount <= 3
RETURN path 返回路径

I don't know if I understand the question completely clear. 我不知道我是否完全理解这个问题。 Just let me know. 请告诉我。

EDIT: 编辑:
I added the second query after comments. 我在评论后添加了第二个查询。 This solution is ugly but it is good workaround. 这个解决方案很难看,但它是一个很好的解决方法。

MATCH path = (n)-[:A|:B*]-(m) MATCH路径=(n) - [:A |:B *] - (m)
WITH path, filter(rel in relationships(path) WHERE type(rel) = 'B') AS Brels WITH path,filter(rel in relationships(path)WHERE type(rel)='B')AS Brels
WITH path, reduce(Bcount = 0, rel IN Brels | Bcount + 1) AS Bcount WITH path,reduce(Bcount = 0,rel IN Brels | Bcount + 1)AS Bcount
WHERE Bcount <= 3 在哪里Bcount <= 3
WITH path, relationships(path) AS rels WITH路径,关系(路径)AS rels
WITH path, rels, reduce(count = 0, rel IN rels | count + 1) AS count WITH path,rels,reduce(count = 0,rel IN rels | count + 1)AS count
WITH path, rels, range(0,count-1) as counter WITH path,rels,range(0,count-1)作为计数器
WITH path, reduce(x = 0, c IN counter | WITH path,reduce(x = 0,c IN counter |
CASE WHEN (type(rels[c])='B' AND type(rels[c+1])='B') THEN x+200000 ELSE x+1 END) AS countX CASE WHEN(类型(rels [c])='B'和类型(rels [c + 1])='B')那么x + 200000 ELSE x + 1 END)AS countX
WHERE countX<200000 其中countX <200000
RETURN path, countX 返回路径,countX

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

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