【问题标题】:Get all edges between multiple vertices - JanusGraph获取多个顶点之间的所有边 - JanusGraph
【发布时间】:2019-04-30 01:01:24
【问题描述】:

我正在尝试编写一个 gremlin 查询来获取顶点列表之间的所有边。

为了保护隐私,对数据进行了编辑。用户 A(42651832) -reports_to-> 用户 B(28729440) -reports_to-> 用户 C(19546208)

id = [19546208, 28729440, 42651832]

我需要找到任意顶点列表之间的所有边

看起来很简单,但我无法编写提供所需结果的查询。

gremlin> g.V(42651832).outE('reports_to').otherV().id()
==>28729440
gremlin> g.V(28729440).outE('reports_to').otherV().id()
==>19546208
gremlin> ids = [19546208, 28729440, 42651832]
==>19546208
==>28729440
==>42651832
gremlin> g.V(ids)
==>v[19546208]
==>v[28729440]
==>v[42651832]
gremlin> g.V(ids).bothE().where(otherV().hasId(ids))
gremlin> g.V(ids).bothE().where(otherV().hasId(within(ids)))
gremlin> g.V(ids).bothE().where(otherV().hasId(within(19546208, 28729440, 42651832)))

显然,我认为 gremlin 控制台和 JanusGraph 之间存在类型转换问题。

引用为字符串或转换为 Long 似乎可行。

gremlin> g.V(ids).bothE().where(otherV().hasId(within("19546208", "28729440", "42651832")))
==>e[10r7d8-h3rs0-i6t-bmxy8][28729440-reports_to->19546208]
==>e[10r7d8-h3rs0-i6t-bmxy8][28729440-reports_to->19546208]
==>e[128qvr-pe6d4-i6t-h3rs0][42651832-reports_to->28729440]
==>e[128qvr-pe6d4-i6t-h3rs0][42651832-reports_to->28729440]
gremlin> g.V(ids).bothE().where(otherV().hasId(within(19546208L, 28729440L, 42651832L)))
==>e[10r7d8-h3rs0-i6t-bmxy8][28729440-reports_to->19546208]
==>e[10r7d8-h3rs0-i6t-bmxy8][28729440-reports_to->19546208]
==>e[128qvr-pe6d4-i6t-h3rs0][42651832-reports_to->28729440]
==>e[128qvr-pe6d4-i6t-h3rs0][42651832-reports_to->28729440]
gremlin>  

gremlin> g.V(ids).bothE().where(otherV().hasId(within(19546208L, 28729440L, 42651832L))).dedup()
==>e[10r7d8-h3rs0-i6t-bmxy8][28729440-reports_to->19546208]
==>e[128qvr-pe6d4-i6t-h3rs0][42651832-reports_to->28729440]

任何其他建议。不知道 JanusGraph 为什么会这样工作。

【问题讨论】:

    标签: gremlin janusgraph


    【解决方案1】:

    这不是一个完整的答案,但希望它能让你足够接近。我用 GraphOfTheGods 来测试一下。

    这将获取从 ids 列表到 hasID() 中引用的 ids 的所有路径,然后输出每个路径中遍历的所有边的列表。我添加了可读性限制。您可以轻松地将所有值添加到集合中以获得重复数据。

    # Save all the graph of the gods vertex ids to a variable
    ids = [4112,4128,4136,8232,12328,16424,20520,4296,4328,4344,8440,12536]
    paths = g.V(ids).until(hasId("8440","12536")).repeat(bothE().aggregate("e").otherV().simplePath()).limit(3).select('e')
    ==>[e[74v-6ig-9hx-368][8440-battled->4112]]
    ==>[e[74v-6ig-9hx-368][8440-battled->4112],e[7xb-6ig-9hx-36o][8440-battled->4128],e[1l0-36o-b2t-9o8][4128-lives->12536],e[9vp-co8-bv9-36o][16424-pet->4128]]
    ==>[e[74v-6ig-9hx-368][8440-battled->4112],e[7xb-6ig-9hx-36o][8440-battled->4128],e[1l0-36o-b2t-9o8][4128-lives->12536],e[9vp-co8-bv9-36o][16424-pet->4128]]
    

    我最初能够得到的是一个包含连接顶点的完整路径,以防它可能有用。

    paths = g.V(ids).until(hasId("8440","12536")).repeat(bothE().otherV().simplePath()).path().limit(5)
    ==>[v[4112],e[74v-6ig-9hx-368][8440-battled->4112],v[8440]]
    ==>[v[4128],e[7xb-6ig-9hx-36o][8440-battled->4128],v[8440]]
    ==>[v[4128],e[1l0-36o-b2t-9o8][4128-lives->12536],v[12536]]
    ==>[v[4128],e[9vp-co8-bv9-36o][16424-pet->4128],v[16424],e[9hh-co8-b2t-9o8][16424-lives->12536],v[12536]]
    ==>[v[4128],e[9vp-co8-bv9-36o][16424-pet->4128],v[16424],e[8p1-co8-cnp-3co][16424-brother->4344],v[4344],e[6cf-6ig-7x1-3co][8440-father->4344],v[8440]]
    

    另外,我对 GraphOfTheGods 和 explain() 步骤进行了一些检查,这绝对看起来像是一个错误。如果我将一个列表设置为变量,它将执行一个相等的步骤而不是一个内步骤。

    paths = g.V(ids).until(hasId(ids)).repeat(out().simplePath()).limit(10).path().explain()
    ...RepeatStep(until([HasStep([~id.eq([4112, 4128, ...])])]),
    

    如果在引号中列出,它将正确地进行内部检查。

    paths = g.V(ids).until(hasId("8440","12536")).repeat(outE().simplePath()).limit(10).path().explain()
    ...RepeatStep(until([HasStep([~id.within([8440, 12536])])])
    

    【讨论】:

    • 谢谢克里斯。我会试试你的逻辑。我同意您的分析,这是一个错误,因为在运行 g.V(ids) 时内部步骤似乎正在工作......内部子步骤有问题。
    • 仅供参考,我打开了一个问题来报告该行为,issues.apache.org/jira/browse/TINKERPOP-2209?filter=-2
    猜你喜欢
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    • 2018-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 2017-03-02
    相关资源
    最近更新 更多