简体   繁体   English

从节点到叶子的 AQL 路径

[英]AQL Path from Node to Leaf

I am new to Arango and I am trying to understand the 'right' way to write some queries.我是 Arango 的新手,我试图了解编写一些查询的“正确”方式。 I read ( https://www.arangodb.com/docs/stable/graphs-traversals-using-traversal-objects.html ) and ( http://jsteemann.github.io/blog/2015/01/28/using-custom-visitors-in-aql-graph-traversals/ ), since they always popped up when searching for what I am trying to do.我读过( https://www.arangodb.com/docs/stable/graphs-traversals-using-traversal-objects.html )和( http://jsteemann.github.io/blog/2015/01/28/using -custom-visitors-in-aql-graph-traversals/ ),因为它们总是在搜索我想要做的事情时弹出。 In particular, I have a graph where a given node has a single path (via a certain 'type' of edge) from that node to a leaf.特别是,我有一个图表,其中给定的节点有一条从该节点到叶子的路径(通过某种“类型”的边)。 Something like x -a-> y -a-> z.类似于 x -a-> y -a-> z。 Where a is the edge type, and x,y,z are the nodes.其中 a 是边类型,x,y,z 是节点。 These paths can be of arbitrary length.这些路径可以是任意长度。 I would like to write an AQL query that returns the single 'longest' path from the starting node to the leaf node.我想编写一个 AQL 查询,该查询返回从起始节点到叶节点的单个“最长”路径。 I find that I always get every sub-path and would then have to do some post-processing.我发现我总是得到每个子路径,然后必须做一些后处理。 The traversal objects looked like they supplied a solution to this issue, but it seems they are now deprecated.遍历对象似乎为这个问题提供了解决方案,但现在似乎已弃用。 Is there a correct way to do this in AQL?在 AQL 中是否有正确的方法来做到这一点? Is there a document that shows how to do what steemann does in his article, but only using AQL?有没有文档展示了如何做 steemann 在他的文章中所做的,但只使用 AQL? Is there some great AQL documentation on graph queries other than what is on the arangodb site (all of which I have already read, including the graph presentation and the udemy course)?除了arangodb站点上的内容(我已经阅读了所有内容,包括图形演示和udemy课程)之外,是否还有一些关于图形查询的出色AQL文档? If not, I would be happy to write something to share with the community, but I am not sure yet how to do this myself, so I'd need some pointers to material that can get me started.如果没有,我很乐意写一些东西与社区分享,但我自己还不确定如何做到这一点,所以我需要一些可以帮助我入门的材料指针。 Long, short, I'd just like to know how to run my query to find the path from node to leaf.长,短,我只想知道如何运行我的查询来找到从节点到叶子的路径。 However, I'd be happy to contribute once I see how things should be done without traversal-objects.但是,一旦我看到在没有遍历对象的情况下应该如何完成事情,我会很乐意做出贡献。 Thank you for your help感谢您的帮助

Taking a traversal in OUTBOUND direction as example, you can do a second traversal with depth = 1 to check if you reached a leaf node (no more incoming edges).OUTBOUND方向的遍历为例,您可以使用 depth = 1 进行第二次遍历,以检查是否到达叶节点(不再有传入边)。 Based on this information, the “short” paths can be filtered out.基于此信息,可以过滤掉“短”路径。

Note that a second condition is required: it is possible that the last node in a traversal is not a leaf node if the maximum traversal depth is exceeded.请注意,需要第二个条件:如果超过最大遍历深度,则遍历中的最后一个节点可能不是叶节点。 Thus, you need to also let paths through, which contain as many edges as hops you do in the traversal (here: 5).因此,您还需要让路径通过,其中包含的边数与您在遍历中所做的跳数一样多(此处为:5)。

LET maxDepth = 5
FOR v, e, p IN 1..maxDepth OUTBOUND "verts/s" edges
  LET next = (
    FOR vv, ee IN OUTBOUND v edges
      //FILTER ee != e // needed if traversing edges in ANY direction
      LIMIT 1 // optimization, no need to check for more than a single neighbor
      RETURN true // using a constant here, which is not actually used
  )
  FILTER LENGTH(next) == 0 || LENGTH(p.edges) == maxDepth
  RETURN CONCAT_SEPARATOR(" -> ", p.vertices[*]._key)

Cross-post from https://groups.google.com/g/arangodb/c/VAo_i_1UHbo/m/ByIUTqIfBAAJ来自https://groups.google.com/g/arangodb/c/VAo_i_1UHbo/m/ByIUTqIfBAAJ 的交叉帖子

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

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