简体   繁体   English

Neo4j密码查询链接列表

[英]Neo4j cypher query for linked list

I have the below graph. 我有下图。 A league (l1) starts with LEVEL r1 and the NEXT levels are related using NEXT relationship as shown. 联赛(l1)从LEVEL r1开始,并且使用所示的NEXT关系来关联NEXT级别。

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

What I am looking for is to find all the levels for a given league and in the order. 我正在寻找的是找到给定联赛的所有级别和顺序。 So, for the above graph the expected output is r1, r2, r3, r4 . 因此,对于上图,预期输出为r1, r2, r3, r4 I've the below query but it is returning all the paths. 我有以下查询,但它返回所有路径。

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

Cyphers for creating this graph. 用于创建此图的密码。 This is already setup on console.neo4j.org (make sure to change the id's) 这已在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;

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. 该查询从联赛(l1)开始,并在所有:level和:next路由中进行跟踪,然后返回找到的节点,这些节点按照从起始节点到达该路由所经过的路由距离进行排序。

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). 这是一个2.0查询,因为它依赖WHERE查询来利用索引来获取图形中的起始节点(l1)。

I managed to get this working with the below query. 我设法与下面的查询一起工作。

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);

Output 产量

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

Any simplification or optimizations are welcome. 欢迎进行任何简化或优化。

How about this query, 这个查询怎么样

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

WHERE league1.name ?= 'l1' AND NOT (endLevel-[:next]->()) 在哪里League1.name?='l1'并且不是(endLevel-[:next]->())

RETURN tail(nodes(p)) 返回尾(节点(p))

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

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