下面的查询显示了多种方式找到并呈现所有长度超过8000英尺的方法。每个查询都比前一个有所提升,并增加了一些新的特征或者用一个步骤简化了查谒。首先我们找到所有路径长超8000英里的。这将所qynk了机场对之间两个方向的路径。
This next set of queries show various ways of finding and presenting all routes longer than 8,000
miles. Each query improves upon the one before by adding some additional feature or using a step
that simplifies the query. First of all let’s just find all routes longer than 8,000 miles. This will
includes routes in both directions between airport pairs.
这是我们的查询生成的。和前边一样为了方便阅读,笔者将输出分栏了。
Here is what our query produces. As before I have arranged the output in columns to aid
readability.
现在我们通过在查询结果中包含每个路径的长度来优化查询。
Now let’s improve the query by including the distance of each route in the query results.
这是修改以后的输出,显示了机场之间的距离。
Here is the modified output showing the distance between the airports.
接下来我们一起让事情简单一点,在用as和select完成工作的时候,使用path,by来简化查询让它更可读一些。和前边一样,记得在一些情况下,使用path会消耗大量的内存。对我们来说这可能不是一个问题, 我们写的查询还相当的简单。
Next let’s simplify things a bit. While using as and select gets the job done, using path and by
shortens the query and makes it more readable. As before remember the warning that in some
cases using path can consume large amounts of memory. That should not be an issue for us here as
we are writing a fairly simple query still.
输出现在看起来更可读了。
The output indeed now looks more readable.
我们的查询看起来相当棒了。但它可以更好,如果不报告同样的路径两次的话。换句话说,两个机场之间的距离只要一个方向的就是我们想要的了。这让事情稍复杂了一点。我们可以找到一种方法排除掉我们想忽略的路径。一种方法是确保起始机场的代码小于目标机场的代码,这看起有点奇怪,但它是我们只想要的我们还没见过的机场对的一种方法。思考这个例子,ATL和JNB之间的路径。lt少于这个测试,我们会允许ATL和JNB之间的路径因为ATL的按字母序比JNB小,但是JNB到ATL的就会被排除。这是一个有用的技术在很多情况下都很有用,您可以排除掉您不想要的结果。让我们修改查询像下面这样,应用这个过滤。
Our query is looking pretty good but it would be nice to not report the same route pair twice. In
other words the distance between two airports in just one direction is all we really want. This adds
a little complexity to things as we have to find a way to filter out the routes that we want to ignore.
One way to do this is to filter by making sure the code for the source airport is less than the code for
the destination airport. This may seem a bit odd but it is a way of saying we only want route pairs
we have not already seen. Consider the case of the route between ATL and JNB. The less than (lt)
test works as we will allow the route between ATL and JNB through (as ATL is alphabetically less
than JNB) but the route between JNB and ATL will be filtered out. This is a useful technique that can
be very helpful in many situations where you are filtering out unwanted results. So let’s modify the
query as shown below to apply this filter.
这是运行修改后的查询得到的输出。如您所见,从ATL到JNB的路径还在,但是反向的路径JNB到ATL的就不在了。对于其它的返程的路径也是这样的。
Here is the output from running the modified query. As you can see the route from ATL to JNB is
still shown but the reverse route from JNB to ATL is now gone. The same is true for all the other
return journey routes.
最后,我们已经有了我们想要的路径,稍调整一下查询这样就可以按距离的降序排列路径啦。我们可以这么做在找到我们感兴趣的路径后,加一个order步骤。
Lastly, now that we have the routes we want, let’s tweak the query so that the routes are sorted by
descending order of distance. We can do this by adding an order step after finding the routes we are
interested in.
这是结果啦。这次是按路径距离的降序排列的。
Here are the results again, this time sorted by route distance in descending order.
做为TinkerPop 3.2.3的一部分,一个额外的能力加到了where步骤中,那就是允许后边跟一个by调节器。这将允许我们,如查我们想这样,像下面这样简化我们的查询。
As part of the TinkerPop 3.2.3 release an additional capability was added to the where step that
allows it to be followed by a by modulator. This allows us, should we so desire, to simplify our
query a bit more as follows.
如您所见,从修改后的查询中,我们得到了同样的结果。几乎是相同的结果。注意实际上我们得到了返程路径的返回。做为一个例子, 我们得到了从DXB到AKL的路径,而不是从AKL到DXB的路径(这是前边的查询中我们得到的)。这是因为我们按相反的顺序进行了比较。
As you can see, we got the same results from our modified query. Well, almost the same results!
Notice that we actually got the return routes returned. So for example, we got the route from DXB to
AKL and not the route from AKL to DXB that we got in the prior case. This is because we compared
the values in the opposite order!
顺带提示:如果我们用gt 代替lt,返回的路径就会是相反的顺序。这也是真的,对于上面的查询也可以这样。
As an interesting side note, and this would be true for the queries above as well, if we replace the lt
with a gt we will get the routes returned in the reverse order.
前边的查询第一条结果是[DOH,9025,AKL].如您所见,现在我们的第一条结果是 [AKL,9025,DOH].
Using the prior query the first result was [DOH,9025,AKL]. As you will see below, our first result is
now [AKL,9025,DOH].