我们看一个例子,使用闭包会很有用。在各种数据库处理中,想要做一些花式的文本搜索或者进行正则搜索,这是常见的需求。TinkerPop3 它本身没有直接支持该功能。换而言之,现在没有复杂的文本搜索方法能够超过我们在上面已经看到的基本的has()类步骤。然而下层的图存储仍然开放了这样的能力。
Let’s take a look at one case where use of closures might be helpful. It is a common requirement
when working with any kind of database to want to do some sort of fuzzy text search or even to
search using a regular expression. TinkerPop 3 itself does not provide direct support for this. In
other words there currently is no sophisticated text search method beyond the basic has() type
steps we have looked at above. However, the underlying graph store can still expose such
capabilities.
为了各各类型的布署,您可能会用到的绝大多数支持TinkerPop的图数据存储都会在后边有类似Solr或者ES这样的索引技术。在这种情况下,大量复杂的搜索方法可供您使用。您应当总是查看您所使用的系统的文档,看看推荐您用什么。
Most TinkerPop enabled graph stores that you are likely to use for any sort of
serious deployment will also be backed by an indexing technology like Solr or
Elasticsearch. In those cases some amount of more sophisticated search methods
will likely be made available to you. You should always check the documentation
for the system you are using to see what is recommended.
在使用廷克图和小精灵控制台时,如果我们想要做超出了基本的类似“city==“Dallas””的任何文本搜索,我们都得使用Lambad函数利用底层的Groovy和Java的特性。注意即使是后边有一个真实索引的图数据系统,我们想查看的这些例子也仍然是工作的,但它们不是更好方式。
When working with Tinkergraph and the Gremlin console if we want to do any sort of text search
beyond very basic things like city == "Dallas" then we will have to fall back on the Lambda function
concept to take advantage of underlying Groovy and Java features. Note that even in graph systems
backed by a real index the examples we are about to look at should still work but may not be the
preferred way.
所以,让我们看一些例子。首先,在航线图中每个机场都有一个描述,如果果DFW,它的描述信息像这样“Dallas Fort Worth International Airport”。如果我们想找到图中任意的机场顶点,它的描述信息中包含了"Dallas"单词,我们就可以利用Groovy字符串的contains方法,像这样做。
So let’s look at some examples. First of all, every airport in the air routes graph contains a
description which will be something like Dallas Fort Worth International Airport in the case of DFW.
If we wanted to search the vertices in the graph for any airport that has the word Dallas in the
description we could take advantage of the Groovy String.contains() method and do it like this.
 

 

 

当您想要使用正则表达式做为查询的一部分,事情将会变得更加的有趣。注意下面第一个例子可以用小精灵的within()步骤实现。因为它只是做了字符串比较,但是它给了我们一个如何写包含正则表达式的查询语句的模板。下面这个例子是找到城市名中以Dal开头的所有的机场,所有它会找到 Dallas, Dalaman, Dalian, Dalcahue, Dalat 和 Dalanzadgad!
Where things get even more interesting is when you want to use a regular expression as part of a
query. Note that the first example below could also be achieved using a Gremlin within() step as it is
still really doing exact string comparisons but it gives us a template for how to write any query
containing a regular expression. The example that follows finds all airports in cities with names
that begin with Dal so it will find Dallas, Dalaman, Dalian, Dalcahue, Dalat and Dalanzadgad!.
 

 

总之知道闭包是相当有用的,您可以用它来过滤,但就像上面说的,谨慎地节省地使用它们,只有纯小精灵查询语句不能奏效的时候使用它们。

So in summary it is useful to know about closures and the way you can use them with filters but as
stated above - use them sparingly and only when a "pure Gremlin" alternative does not present
itself.
我们可以更进一步,创建一个客户化的断言(见下一节),让它为我们处理正则表达式。
We could actually go one step further and create a custom predicate (see next
section) that handles regular expressions for us.
posted on 2022-04-25 12:13  bokeyuannicheng0000  阅读(55)  评论(0)    收藏  举报