Neo4j - 提取路径中节点的 属性 数据以及指向这些节点的关系

Neo4j - extract property data of nodes in a path and the relationship pointing to those nodes

我想为以下数据结构编写一个紧凑的查询。

节点 n1 到节点 1000 通过关系连接 'rel1' 每个节点 n 都有 属性 n.name 另一个节点(只存在一个)Q 与每个节点 n1 有关系 'rel2',关系为 属性 q.value

(n1)-[rel1]->(n2)-[rel1]->(n3)--[rel1]-......->(n1000)
(Q)-[rel2]->(n1), (Q)-[rel2]->(n2),(Q)-[rel2]->(n3)....... (Q)-[rel2]->(n1000)

我想提取此数据:按出现顺序列出的 1000 个名称 (n.name) 和指向每个节点 n[=13 的 1000 q.value 的列表=]

我使用集合函数检索了所有名称节点 n

match p=(n)-[r:rel1*..]->(m) where  n.nodeNumber = 1 and m.nodeNumber=1000 RETURN extract(n IN nodes(p)| n.name) AS name 
ORDER BY length(p) DESC
LIMIT 1

我可以轻松提取所有指向这些节点的 q.value 吗?最好是在一个声明中。

确保使用标签+索引快速找到你的开始和结束节点。 如果您使用最短路径,它只会找到一条路径。

match p=shortestPath((n:Label)-[r:rel1*..]->(m:Label) 
where  n.nodeNumber = 1 and m.nodeNumber=1000 
RETURN extract(n IN nodes(p) | {name: n.name, value: head([p in (n)<-[:rel2]-() | last(nodes(p)).value]) ) AS data
ORDER BY length(p) DESC
LIMIT 1

更优雅:

match p=shortestPath((n:Label)-[r:rel1*..]->(m:Label) 
where  n.nodeNumber = 1 and m.nodeNumber=1000 
UNWIND nodes(p) as n
MATCH (n)<-[:rel2]-(q:Q)
RETURN n.name, q.value;

只是为了更正之前优雅的命令。 a ) 丢失,您需要指定要搜索的路径的长度。默认情况下它只有 15 个左右。我在命令中添加了 1100,因为给出了 1000 个节点。我还将 n 更改为 nlist,因为 Neo4j 抱怨 n 被声明了两次。

更新的命令在这里:

match p=shortestPath((n:Label)-[r:rel1*..1500]->(m:Label))
where  n.nodeNumber = 1 and m.nodeNumber=1000 
UNWIND nodes(p) as nlist
MATCH (nlist)<-[:rel2]-(q:Q)
RETURN nlist.name, q.value;