有时,在使用repeat步骤检索一幅图时,在给定的检索深度内,需要限定返回结果的数量。要达到这一结果,会比您预料的要复杂一点。limit步骤的作用范围是全局的,如下所示,因此它并不是我们在当前场景下所需要的。
Sometimes, while exploring a graph using repeat steps, it is desirable to limit the amount of results
returned at any given depth of a traversal. Achieving this result is a little more complicated than
you might expect. The limit step has a global scope, as shown below, and is therefore not quite what
we need in this case.

 运行查询,我们在从SFO开始的深度为3的搜索中得到了5个结果。这并不是我们想要找到的结果。

When the query is run, we get back five results at depth three (three hops) from San Francisco
(SFO). While interesting, this is not the result we are looking for in this case.

 乍一看,我们只需要简单地把limit步骤移动到repeat步骤内就可以,但是因为limit步骤的作用域是全局,我们需要采取不同的措施。

At first glance, it may appear that simply moving the limit step inside the body of the repeat step is
all we need to do but because of the global nature of the limit step we need to take a different
approach.

 如您所见,运行查询,我们得到了和limit在repeat步骤外边一样的查询结果。

As you can see, when the query is run we get the same result we got when limit was outside the
repeat step body.

 为了构建这个遍历,在每个深度得到5个结果,我们需要引入额外的步骤到查询语句中。不去看最终的查询,我们先来看一些中间的步骤。

In order to construct a traversal that will yield five results at each depth we need to introduce some
additional steps into the query. Rather than jump straight to the final query though, let’s look at
some intermediate steps first.
首先,我们写一个查询统计从SFO出发,三跳,每个深度我们可以到达的地点的数量。为这一目标,我们可以命名用loops步骤做为groupCount步骤的键。 loops步骤的深度的计数器从0开始,数字0表示当前的深度是1,这个查询生成的统计数字可以包含重复的,因为到深度不为1的达到同一个机场有多种方式。但是这对于我们构建的查询来说不是一个问题。
First of all, lets write a query that counts how many places we might end up at for each depth,
starting at SFO to a depth of three hops. To do this we can use the loops step as the key for a
groupCount step. Remember that loops will tell us the depth we are currently at while executing a
repeat step. The loops step depth counter starts at zero, so a value of zero really means we are at
depth one. The counts generated by this query will include duplicates as there are multiple ways to
get to the same airport beyond depth one but that is not a problem for the query we are building. 

 给它一个标签,在这个例子中标签是airports, groupCount步骤的作用像是一个兼职做的事(顺带做的事)这意味着它统计了经过的路径,但是不会改变传入到查询后继步骤的内容。在查询最后的cap步骤明确返回统计结果给我们。您在下面看到,运行时,查询产生了一个最开始三个深度的路径数的表。深度是表的键,路径数量是各个键对应的值。顺带提一下,看到查询是如何快速的扇出的,这很有趣。既使有重复的访问,在深度是3时,事实上我们已经访问了836000次机场顶点。

When given a label, in this case "airports", the groupCount step acts as a side effect. This means that
it counts what passes through it but does not change what is passed on to subsequent steps in the
query. The cap step at the end of the query explicitly returns the counts to us. As you can see below,
when run, the query produces a map of the route counts at the first three depths, where the depth
is the map key and the count is the map value. As a side note, it is interesting to see how quickly
queries like this one can "fan out". We have essentially visited airport vertices over 836,000 times at
depth three given all the duplicate visits.

 现在我们有一个查询可以统计每个深度我们访问了多少个顶点,我们可以增加约束条件限制返回的结果的数量。

Now that we have a query that can count how many vertices we are visiting at each depth we can 
use that to add a constraint that limits how many we return.
下面的查询是可行的,在样例代码文件中有一个名为 restricted-repeat.groovy的文件,它所在的文件地址是:
https://github.com/krlawrence/graph/tree/master/sample-code.
The query below is available as a script called restricted-repeat.groovy in the
sample-code folder located at https://github.com/krlawrence/graph/tree/master/
sample-code.
更新版的查询如下所示。在最开始的select步骤中增加了where步骤,最开始的select步骤选择了groupCount生成的表,再使用loop步骤从表中选择符合条件的。where步骤会过滤只有5次或者小于5次的顶点。
An updated version of our query is shown below. A where step has been added that initially selects
the map generated by groupCount and from that uses the current loops value to select an entry
from the map. A vertex is passed on by the where step filter only if five or fewer have been
encountered so far.

 运行修改后的查询,输出结果与以前的很不一样。这次在每个深度上都有5个结果返回。这是我们期待的结果。

When we run our modified query, the output is quite different. This time at each depth, five routes
are returned. This is the result we are looking for!

 乍一看,上面用到的where步骤看起来有点令人困惑。下面笔者增加了一些例子,希望它们有助于您的理解。首先,下面的查询简单的用groupCount步骤创建了一个有5个元素的表,表的键就是顶点的ID,是一个常量值。在这个查询中没有where步骤,所以5个入顶点都是结果的一部分。

At first glance, the where step used above can be confusing. I have included some examples below
that hopefully help explain the behavior more easily. First of all, the query below simply creates a
map using groupCount based on five vertex IDs where the key is simply a constant literal value. As
there is no where step present all five incoming vertices are accounted for in the result.

 如果您只想要第三个顶点通过where步骤的过滤,可以像下面这样修改查询语句。这本质上就是“每个深度中的五个”这个查询的意义,但或许更容易理解。

If we only wanted the third vertex to pass through the where step filter we could adjust the query
as follows. This is essentially what our "five at each depth" query does but is perhaps easier to
follow. 

 最后,当我们把约束条件变成小于或等于三,看看会发生什么,在结果有只包含了前三个顶点。

Lastly, we can see what happens when we change the constraint to be less than or equal to three.
Only the first three vertices are included in the result.

 在“随机访问图”这一节中会另一个限定repeat步骤返回结果的例子,它使用了local和limit步骤的组合。

There is another example of limiting the results of a repeat step using a
combination of local and limit steps in the "Randomly walking a graph" section
later in the book.
希望通过这样的分解步骤,您可以更好的理解小精灵查询是怎么工作的。
Hopefully, by decomposing the steps in this way you are able to gain a good understanding of how
this very useful Gremlin query works.
posted on 2022-04-20 17:09  bokeyuannicheng0000  阅读(50)  评论(0)    收藏  举报