或在Gremlin中带有Match语句的语句

问题描述 投票:0回答:1

我有一个Janusgraph数据库,其模式如下。

(Journal)<-[PublishedIn]-(Paper)<-[AuthorOf]-(Author)

我试图用gremlin的方法写一个查询 match() 子句,将搜索两个不同的期刊和相关的论文,标题和作者中有一个关键词。这是我目前的情况。

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,我得到的是完整的图,所以我认为我的查询的logicsyntax有问题。

如果我把查询写成这样。

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() 子句?

gremlin graph-databases janusgraph
1个回答
2
投票

我不知道这是否是你所有的问题,但我认为你的 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'))

基本上是利用... P.or(). 我认为这两种方案都应该比使用 or()-踏上前台,但只是一个 profile() 的JanusGraph会告诉你,如讨论的 此处.

说了这么多,我想知道为什么你的? 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() 是性能显著提高。

© www.soinside.com 2019 - 2024. All rights reserved.