或者使用Gremlin中的Match语句



我有一个Janusgraph数据库,具有以下模式:

(Journal(&lt-[PublishedIn]-(论文(&lt-[作者]-(作者(

我正试图使用gremlinmatch()子句编写一个查询,该子句将搜索两种不同的期刊以及标题和作者中带有关键字的相关论文。到目前为止,我拥有的是:

sg = g.V().match(
__.as('a').has('Journal', 'displayName', textContains('Journal Name 1')),
__.as('a').has('Journal', 'displayName', textContains('Journal Name 2')),
__.as('a').inE('PublishedIn').subgraph('sg').outV().as('b'), 
__.as('b').has('Paper', 'paperTitle', textContains('My Key word')),
__.as('b').inE('AuthorOf').subgraph('sg').outV().as('c')).
cap('sg').next()

此查询成功运行,但返回0个顶点和0条边。如果我把查询一分为二,分别搜索每个Journal displayName,我会得到完整的图,所以我认为我的查询的逻辑/语法有问题。

如果我这样写查询:

sg = g.V().or(has('JournalFixed', 'displayName', textContains('Journal Name 1')),
has('JournalFixed', 'displayName', textContains('Journal Name 2'))).
inE('PublishedInFixed').subgraph('sg').
outV().has('Paper', 'paperTitle', textContains('My Key word')).
inE('AuthorOf').subgraph('sg').
outV().
cap('sg').
next()

它返回一个约有7000个节点的网络。如何重写此查询以使用match()子句?

我不确定这是否是你的全部问题,但我认为你的match()正在将你的"displayName"步骤建模为and()而不是or()。你可以用profile()检查,就像我在这里用TinkerGraph:一样

gremlin> g.V().match(__.as('a').has('name','marko'), __.as('a').has('name','josh')).profile()
==>Traversal Metrics
Step                                                               Count  Traversers       Time (ms)    % Dur
=============================================================================================================
TinkerGraphStep(vertex,[name.eq(marko), name.eq...                                             0.067   100.00
>TOTAL                     -           -           0.067        -

我想你可以通过多种方式解决这个问题。在我的例子中,within()的使用,正如您前面问题的另一个答案中所描述的,效果很好:

gremlin> g.V().match(__.as('a').has('name', within('marko','josh'))).profile()
==>Traversal Metrics
Step                                                               Count  Traversers       Time (ms)    % Dur
=============================================================================================================
TinkerGraphStep(vertex,[name.within([marko, jos...                     2           2           0.098   100.00
>TOTAL                     -           -           0.098        -

对于您的情况,我将替换:

or(has('JournalFixed', 'displayName', textContains('Journal Name 1')),
has('JournalFixed', 'displayName', textContains('Journal Name 2')))

带有:

has('JournalFixed', 'displayName', textContains('Journal Name 1').
or(textContains('Journal Name 2'))

基本上利用了CCD_ 8。我认为这两个选项中的任何一个都应该比使用or()-step front更好,但只有JanusGraph的profile()才能说明问题,正如这里所讨论的那样。

话虽如此,我想知道为什么你的or()不能直接翻译成match(),如下所示:

g.V().match(
__.as('a').or(has('Journal', 'displayName', textContains('Journal Name 1')),
has('Journal', 'displayName', textContains('Journal Name 2'))),
__.as('a').inE('PublishedIn').subgraph('sg').outV().as('b'), 
__.as('b').has('Paper', 'paperTitle', textContains('My Key word')),
__.as('b').inE('AuthorOf').subgraph('sg').outV().as('c')).
cap('sg')

不过,我想我对P.or()的建议会更具表现力。

最新更新