小精灵提供了一系列的逻辑运算符如and, or 和not,它们可以用于布尔类型的查询中。在很多情况下,笔者发现可以通过使用within来避免使用or 。not可以通过使用without来避免;但是知道这些运算存在总是一件好事。and运算有进可以用串联起来的has步骤来代替。也就是说,总有一些情况,这些布尔运算的步骤是相当有用的。
Gremlin provides a set of logical operators such as and, or and not that can be used to form Boolean
(true/false) type queries. In a lot of cases I find that or can be avoided by using within for example
and that not can be sometimes avoided by using without but it is still good to know that these
operators exist. The and operator can sometimes be avoided by chaining has steps together. That
said there are always cases where having these boolean steps available is extremely useful.

 只要您想把两个遍历合在一起,您总是可以像下面这样把and 植入到中间。

You can also use and in an infix way as follows so long as you only want to and two traversals
together.

如您所料,or步骤可以有两个以上选项。下面的这个有四个。实际上,这个查询使用within会是一个更好的方法,但这里它是使用or 表达式的一个好例子。

As you would probably expect, an or step can have more than two choices. This one below has four.
Also, note that in practice, for this query, using within would be a better approach but this suffices
as an example of a bigger or expression. 

上面的例子用within可以写成这样。头脑中要记得,对于一个查询,使用or 并不总是最佳的方案。我们将在下面的一节中更进一步研究within,without步骤。 

Using within the example above could be written like this so always keep in mind that using or may
not always be the best approach to use for a given query. We will look more closely at the within
and without steps in the following section.

 下面的例子使了and 步骤找到位置在Texas有一个跑道至少12000英尺长的机场。

This next example uses an and step to find airports in Texas with a runway at least 12,000 feet long.

 用or步骤,使用and不是必需的。我们可以像下面这样重写前边的查询。

As with the or step, using and is not always necessary. We could rewrite the previous query as
follows. 

 小精灵也提供了not步骤,它的工作和您期想的一样。。这个查询找到不是机场的顶点。

Gremlin also provides a not step which works as you would expect. This query finds vertices that
are not airports.

 前边的查询也可以写成下面这样。

This previous query could also be written as follows.

 取决于您的图的模型和查询语句它自己是否需要布尔步骤。有时,像上面描述的联接has步骤在一起或许更有效,或者使用within,without可能更有意义。

Depending on the model of your graph and the query itself it may or may not
make sense to use the boolean steps. Sometimes, as described above chaining has
steps together may be more efficient or using a step like within or without may
make more sense.
布尔步骤and也可以用点号连接起来,如下像所示。这个查询找到所有有少于100个出边但是有大于94个出边的机场,并按机场代码和路线数量分组返回查询结果。注意:在这个场景中,and步骤是通过点运算符加在了lt步骤上,而不是用and做为整个测试的容器步骤。运行查询的结果如下所示。
Boolean steps such as and can also be dot combined as the example below shows. This query finds
all the airports that have fewer than 100 outbound routes but more than 94 and returns them
grouped by airport code and route count. Notice how in this case the and step is added to the lt step
using a dot rather than having the and be the containing step for the whole test. The results from
running the query are shown below as well.

 我们刚看的查询也可以写成下面这样,但这次使用了内联的and步骤,对笔者而言这个看起来比用点连接更清晰,更一目了然。如您所见我们得到了和前例一样的结果。

The query we just looked at could also be written as follows but in this case using the and step
inline by dot combining it (as above) feels cleaner to me. As you can see we get the same result as
before.

 如笔者所说的,很多时候,写一个查询可以有多种方法产生相同的结果。这是一个重写前边两个查询的例子,用到了between步骤代替了and步骤。记住between它是左闭右开的。这样我们就要指定101做为右边界(上边界)95做为左边界(下边界)。

As I have pointed out several times already, there are often many ways to write a query that will
produce the same result. Here is an example of the previous two queries rewritten to use a between
step instead of an and step. Remember that between is inclusive/exclusive, so we have to specify 101
as the upper bound and 95 as the lower bound.

这是同一个查询但是用inside步骤重写了。

Just for fun, here is the same query but rewritten to use an inside step.

 顺带提一下,如果我们想要修改组从而使得机场按数量而不是按代码分组,我们可以像下面这么做。

As a side note, if we wanted to reverse the grouping so that the airports were grouped by the counts
rather than the codes we could do that as follows.

 您也可以增加内联的and步骤到查询中,如下所示,注意这次AUH和NCE不再是结果集的一部分。因为它们有97条路线,这是我们的新测试排除掉的。

You can also add additional inline and steps to a query as shown below. Notice that this time AUH
and NCE are not part of the result set as they have 97 routes which our new and test eliminates.

 在上面的例子中大量使用了where步骤。相信它的作用您已很清晰了。尽管如此,笔者仍会在下一节中详细解释“where步骤如何工作的。”

The where step was used lot in the examples above. Hopefully the effect of using it was clear.
Nonetheless I will explain in more detail how the where step works in the next section. 
posted on 2022-04-17 22:43  bokeyuannicheng0000  阅读(28)  评论(0)    收藏  举报