简体   繁体   English

从密码查询中获取路径长度

[英]Get Path Length from Cypher Query

So, Say that I've got a list of nodes like so: 因此,假设我有一个像这样的节点列表:

A -> B -> C -> D -> ... A-> B-> C-> D-> ...

I want to add node F onto the beginning of this list. 我想将节点F添加到此列表的开头。 To complicate this pattern, I can be given a reference to any node on this list as the "starting point," from which I will need to derive the starting point. 为了使这种模式复杂化,我可以引用此列表上的任何节点作为“起点”,从中我需要从中得出起点。 For example, I could be given a reference to node "C", and need to derive an algorithm that will return a reference to A. 例如,可以为我提供对节点“ C”的引用,并且需要派生一种算法,该算法将返回对A的引用。

I figure this should be able to be done with a query such as 我认为这应该可以通过查询来完成

    START n = node(*), a = node(*)
    MATCH a -[:LINKED*]> n
    WHERE n.id! = <ID>
    RETURN a

If I could sort the relationships by length, I could simply take the longest path as the first node in the relationship, and go along my merry way. 如果我可以按长度对关系进行排序,那么我可以简单地将最长的路径作为关系中的第一个节点,然后沿自己的方式前进。 Trouble is, I can't figure out how to order the results by path length. 麻烦的是,我不知道如何按路径长度排序结果。 I figure that it must be possible, I'm just missing a small query command. 我认为这是有可能的,我只是缺少一个小的查询命令。 Any takers? 有没有人?

-pYr0 -pYr0

Length is function: http://docs.neo4j.org/chunked/stable/query-functions-scalar.html#functions-length 长度就是函数: http : //docs.neo4j.org/chunked/stable/query-functions-scalar.html#functions-length

START n = node(*), a = node(*)
MATCH p=a -[:LINKED*]-> n
WHERE n.id! = <ID>
RETURN a
ORDER BY length(p) desc

If you want the head of the list, you can also match with an optional relationship leading into your candidate node. 如果您想成为列表的头,则还可以将其与指向候选节点的可选关系进行匹配。 If the relation does not exist, you're there. 如果该关系不存在,那么您就在那里。

Assuming that you got the ID of some node in the chain: 假设您已获得链中某个节点的ID

START n = node(<ID>)
MATCH () -[r?:LINKED]-> a -[:LINKED*]-> n
WHERE r = null
RETURN a

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

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