【问题标题】:Janusgraph Gremlin: How to find if a vertex has at least 1 relationship/edge to a thousands of vertex with specific propertyJanusgraph Gremlin:如何查找顶点是否与具有特定属性的数千个顶点至少有 1 个关系/边
【发布时间】:2020-08-05 11:22:58
【问题描述】:

假设我有这个示例数据

Vertex[type: Person, name: Foo] ---hasPet---> Vertex[type: Dog, name: Dog1]
Vertex[type: Person, name: Bar] ---hasPet---> Vertex[type: Cat, name: Cat1]
Vertex[type: Person, name: Bazz] ---hasPet---> Vertex[type: Dog, name: Dog2]
Vertex[type: Person, name: Bazz] ---hasPet---> Vertex[type: Dog, name: Dog3]

然后我想找到至少有宠物的任何人型顶点是狗。

如何在不遍历所有非人类型顶点的情况下找到所有这些人类型顶点?

我尝试了这个查询,但它仍在遍历Bazz 的所有宠物,我想在找到任何匹配项后立即停止遍历,因为Bazz 可能还有数千个其他宠物。

我的查询:

g.traversal().V().has("type", "Person").repeat(out("hasPet").simplePath()).until(has("type", "Dog")).simplePath().in().valueMap("name")

结果如下:

[Foo]
[Bazz]
[Bazz]

我喜欢的是:

[Foo]
[Bazz]

【问题讨论】:

    标签: gremlin janusgraph


    【解决方案1】:

    您始终可以使用dedup 步骤删除任何重复的结果:

    g.V().has('type', 'Person').
      repeat(out('hasPet').simplePath()).
      until(has('type', 'Dog')).simplePath().in().
      valueMap('name').dedup()
    

    我不确定您为什么不对顶点使用不同的标签,至少在您的示例数据中,repeat 步骤和in 是多余的。相反,您可以进行更简单的查询:

    g.V().has('type', 'Person').
      where(out('hasPet').has('type', 'Dog')).
      values('name').dedup()
    

    示例:https://gremlify.com/m33b6dsxnim

    【讨论】:

    • 感谢您的建议,但我写此查询是为了说明。实际上我想知道的是是否有办法避免'out()'遍历所有外部顶点并在第一个匹配时停止,因为外部顶点可能达到数十万并且会影响性​​能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 2014-05-15
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多