2

我有下图。联赛 (l1) 以LEVEL r1 开始,并且NEXT级别使用NEXT关系相关,如图所示。

league-[:LEVEL]->r1-[:NEXT]->r2-[:NEXT]->r3-[:NEXT]->r4

我正在寻找的是按顺序查找给定联赛的所有级别。因此,对于上图,预期输出为r1, r2, r3, r4。我有以下查询,但它正在返回所有路径。

start l1=node(9)  match l1-[:level]->(level) with level match p=level-[:next*]->(remainingLevels) return p;

用于创建此图的密码。这已经在console.neo4j.org上设置(确保更改 id)

CREATE (l1 {name: 'l1'}) return l1;
CREATE (r1 {name: 'r1'}) return r1;
START l1=node(9), r1=node(10) CREATE l1-[r:level]->r1;
CREATE (r2 {name: 'r2'}) return r2;
START r1=node(10), r2=node(11) CREATE r1-[r:next]->r2;
CREATE (r3 {name: 'r3'}) return r3;
START r2=node(11), r3=node(12) CREATE r2-[r:next]->r3;
CREATE (r4 {name: 'r4'}) return r4;
START r3=node(12), r4=node(13) CREATE r3-[r:next]->r4;
4

3 回答 3

4

I cleaned up your data set and attempted an answer here:

MATCH league-[r:level|next*]->(level) 
WHERE league.name?="l1" 
RETURN level, length(r) AS Dist, r 
ORDER BY length(r)

The query starts at a league (l1) and traces out across all :level and :next routes and then returns the nodes found sorted by the distance of the route taken to to arrive at it from the starting node.

Note this finds all possible routes, so if there are muliple ways to get to to the level, it won't work very well. That said, it appears your graph is quite tree-like (no cycles) and this will work fine.

This a 2.0 query because it relies on the WHERE query to utilize the indicies to get the starting node in the graph (l1).

于 2013-08-13T19:03:22.060 回答
1

这个查询怎么样,

匹配 p=league1-[:level]->(level)-[:next*]->endLevel

在哪里 League1.name ?= 'l1' AND NOT (endLevel-[:next]->())

返回尾部(节点(p))

于 2013-08-13T19:11:21.820 回答
1

我设法使用下面的查询来解决这个问题。

start l1=node(9) 
match l1-[:level]->(level) 
with level 
match p = level-[:next*]->(remainingLevels) 
with level, max(length(p)) as maxlen 
match p = level-[:next*]->(remainingLevels) 
where length(p)=maxlen
return nodes(p);

输出

+-----------------------------------------------------------------------------------+
==> | nodes(p)                                                                          |
==> +-----------------------------------------------------------------------------------+
==> | [Node[10]{name:"r1"},Node[11]{name:"r2"},Node[12]{name:"r3"},Node[13]{name:"r4"}] |
==> +-----------------------------------------------------------------------------------+

欢迎任何简化或优化。

于 2013-08-13T17:22:32.637 回答