简体   繁体   English

neo4j Cypher Query沿路径返回关系

[英]neo4j Cypher Query to return relationships along a path

I'm working with a Neo4j data set that looks like this: 我正在使用如下所示的Neo4j数据集:

样本数据

I'm trying to write a query that returns all paths from the green node 84 to node 88 that also returns/includes the blue nodes that have a Files relationship to the gray nodes along the paths 我正在尝试编写一个查询,该查询返回从绿色节点84到节点88所有路径,该查询还返回/包括沿路径与灰色节点具有Files关系的蓝色节点

So far, my query looks like this: 到目前为止,我的查询看起来像这样:

MATCH (startingstatus:green {id: 84}),
(endingstatus:green {id: 88}),
path = (startingstatus) - [StatusOut*0..5] -> (endingstatus),
(scenario:gray) - [Files] -> (form:blue) 
RETURN *;

The problem is that right now, the query returns every gray - [Files] -> blue relationship in the database (eg the image above), I only want the blue nodes with the relationship to the gray node with the ids 4 , 20 , and 21 since those are the nodes along the path. 的问题是,现在,该查询返回的每个gray - [Files] -> blue在数据库关系(例如上述的图像), 我只要与该ID的关系的灰色节点蓝色节点420 ,和21因为它们是路径上的节点。

I hope this makes sense. 我希望这是有道理的。 I apologize for having to rename some of the nodes. 对于必须重命名某些节点,我深表歉意。

Its hard to test the query without a sample data set, but you can try this solution using filter() , nodes() and UNWIND : 没有样本数据集很难测试查询,但是您可以使用filter()nodes()UNWIND尝试以下解决方案:

MATCH path = (startingstatus:green {id: 84}) - [:StatusOut|:StatusIn*0..5] -> (endingstatus:green {id: 88})
WITH path, filter(node in nodes(path) where node:gray) as grayNodes
UNWIND grayNodes as grayNode
MATCH (grayNode)-[:Files]->(blueNode:blue)
RETURN path, grayNode, blueNode

To explain: I'm extracting the gray nodes from the path. 解释一下:我正在从路径中提取灰色节点。 Then I match the blue nodes related to these gray nodes. 然后我匹配与这些灰色节点相关的蓝色节点。

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

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