简体   繁体   English

AQL-图遍历-复杂条件路径上的过滤

[英]AQL - graph traversal - filtering on path with complex condition

I would like to ask how to best traverse graph and return only subgraph based on complex condition which must be satisfied by all nodes from root to leaves. 我想问一问如何最好地遍历图,并根据复杂的条件仅返回子图,这是从根到叶的所有节点都必须满足的条件。 In other words, I need some mechanism such that when the condition on any intermediate level is not met, traversal is stopped (none nested node is processed and returned to output) 换句话说,我需要某种机制,以便在不满足任何中间级别的条件时,停止遍历(不处理任何嵌套节点并返回到输出)

Let's say I have the following graph: 假设我有以下图表:

A -> B -> C (active=false) -> D

where I deactivated node C (note the flag active=false means that all subgraph is deactivated including C and D). 在这里我停用了节点C(请注意,标记active = false表示所有子图都已停用,包括C和D)。

According to documentation I can easily construct such filter via filtering on path, wildcard [*] and ALL keyword, which also stops traversing when condition on C is not met. 根据文档,我可以通过对路径,通配符[*]和ALL关键字进行过滤来轻松构造此类过滤器,当不满足C的条件时,它们也将停止遍历。 With simple condition this works great: 在简单的条件下,这很好用:

for v,e,p in 1..100 outbound 'test/A' graph 'testGraph'
  filter p.vertices[*].active ALL != false return v
  // returns A, B

Now I have another graph where each node is either fixed or has some validity timespan (from, to) attributes: 现在,我有另一个图,其中每个节点都是固定的,或者具有一些有效时间跨度(从,到)属性:

A (type="fixed") -> B (from=2,to=3) -> C (from=1, to=5) -> D (type="fixed")

Now I would like to return only subgraph where all (intermediate) nodes are either fixed or satisfy time condition from>=2 and to<=3. 现在,我只想返回子图,其中所有(中间)节点都是固定的,或者满足从> = 2到<= 3的时间条件。 I need that A,B are returned. 我需要退还A,B。

for v,e,p in 0..100 outbound 'test/A' graph 'testGraph'
  filter p.vertices[*].type ALL == 'fixed' or
    (p.vertices[*].from ALL >= 2 and p.vertices[*].from ALL <= 3)
  return v

However this is obviously wrong (and returns only A), logically I need to add ALL keyword at the beginning of the condition (I need that the condition is applied on each level and when the condition is not met, traversing is stopped), however this is not supported: 但是,这显然是错误的(并且仅返回A),从逻辑上讲,我需要在条件的开头添加ALL关键字(我需要将条件应用于每个级别,并且当不满足条件时,将停止遍历)不支持:

filter ALL(p.vertices[*].type == 'fixed' or
  (p.vertices[*].from >= 2 and p.vertices[*].from <= 3)

Classical approach via filtering on vertices does not meet my needs, because it doesn't stop traversing when the condition is not met, ie the following returns A,B,D (C is skipped but I also need to prune subtree of C such that D is not on output): 通过对顶点进行过滤的经典方法无法满足我的需求,因为在不满足条件时它不会停止遍历,即以下返回A,B,D(C被跳过,但我还需要修剪C的子树,以便D不在输出上):

for v,e,p in 0..100 outbound 'test/A' graph 'testGraph'
  filter  v.type == 'fixed' or 
    (v.from >= 2 and v.from <= 3)
  return v

Any ideas? 有任何想法吗? Thank you. 谢谢。

The AQL PRUNE feature was introduced in ArangoDB versions 3.4.5 and 3.5.0. AQL PRUNE功能是在ArangoDB版本3.4.5和3.5.0中引入的。 Using the AQL keyword PRUNE the traversing is stopped when a condition on the vertex, the edge, the path or any variable defined before before is met. 使用AQL关键字PRUNE时,如果满足顶点,边,路径或之前之前定义的任何变量的条件,则停止运行。

Pruning is the easiest variant to formulate conditions to reduce the amount of data to be checked during a search. 修剪是制定条件以减少搜索过程中要检查的数据量的最简单变体。 So it allows to improve query performance and reduces the amount of overhead generated by the query. 因此,它可以提高查询性能并减少查询所产生的开销。 Pruning can be executed on the vertex, the edge and the path and any variable defined before. 修剪可以在顶点,边和路径以及之前定义的任何变量上执行。

This video tutorial shows the difference between FILTER and the new PRUNE with a hands-on example. 视频教程通过一个动手示例演示了FILTER和新PRUNE之间的区别。 You can find more details in the documentation . 您可以在文档中找到更多详细信息。

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

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