我尝试在可变长度路径上执行聚合查询,其中我要聚合的节点不在原始路径中,而是与它们相关。例如,我的路径看起来像
MATCH p = (:Visit)-[:NEXT*]->(:Visit)
RETURN p
但每个(:Visit)节点都与(:Destination)
相关(:Visit)-[:LOCATION]->(:Destination)
我想要的聚合是根据Destination节点的id属性而不是Visits计算公共路径。我想出了一种使用Union来组合许多固定长度路径结果的方法
MATCH (d1:Destination)--(v1:Visit), (d2:Destination)--(v2:Visit)
WHERE (v1:Visit)-[:NEXT]->(v2:Visit)
RETURN [d1.id,d2.id] AS Path, count(*) AS PathCount
UNION
MATCH (d1:Destination)--(v1:Visit), (d2:Destination)--(v2:Visit), (d3:Destination)--(v3:Visit)
WHERE (v1:Visit)-[:NEXT]->(v2:Visit)-[:NEXT]->(v3:Visit)
RETURN [d1.id,d2.id,d3.id] AS Path, count(*) AS PathCount
UNION ...
但如果路径长度为200,那么这不是一个很好的解决方案,而且我担心使用多个联盟的表现。
我在这里用样本数据创建了一个Neo4j Gist: http://gist.neo4j.org/?a8ab894c5c9740a94747
样本数据
CREATE
// Destinations.
(d1:Destination {id:'A'}),
(d2:Destination {id:'B'}),
(d3:Destination {id:'C'}),
(d4:Destination {id:'D'}),
(d5:Destination {id:'E'}),
(d6:Destination {id:'F'}),
// First Route
(v1:Visit {time:1}),
(v2:Visit {time:2}),
(v3:Visit {time:3}),
(v4:Visit {time:4}),
(v5:Visit {time:5}),
(v1)-[:LOCATION]->(d1),
(v2)-[:LOCATION]->(d2),
(v3)-[:LOCATION]->(d3),
(v4)-[:LOCATION]->(d4),
(v5)-[:LOCATION]->(d6),
(v1)-[:NEXT]->(v2)-[:NEXT]->(v3)-[:NEXT]->(v4)-[:NEXT]->(v5),
// Second Route
(v6:Visit {time:10}),
(v7:Visit {time:21}),
(v8:Visit {time:23}),
(v10:Visit {time:45}),
(v6)-[:LOCATION]->(d1),
(v7)-[:LOCATION]->(d2),
(v8)-[:LOCATION]->(d4),
(v9)-[:LOCATION]->(d6),
(v10)-[:LOCATION]->(d5),
(v11)-[:LOCATION]->(d3),
(v6)-[:NEXT]->(v7)-[:NEXT]->(v8)-[:NEXT]->(v9)-[:NEXT]->(v10)-[:NEXT]->(v11);
预期产出
Path PathCount
[A, B] 2
[D, F] 1
[B, D] 1
[B, C] 1
[C, D] 1
[B, C, D] 1
[C, D, F] 1
[A, B, C] 1
[A, B, D] 1
... many more
答案 0 :(得分:0)
以下是否适合您?它将每个路径的头部返回为PathHead
,路径中所有其他节点的有序集合为PathTail
,路径中的步骤数为PathCount
。
MATCH (d1:Destination)<-[:LOCATION]-(v1:Visit)-[:NEXT*]->(:Visit)-[:LOCATION]->(d2:Destination)
RETURN d1.id as PathHead, COLLECT(d2.id) AS PathTail, COUNT(*) AS PathCount