【问题标题】:Graph Traversal & Filtering with Gremlin using OrientDB使用 OrientDB 使用 Gremlin 进行图遍历和过滤
【发布时间】:2017-11-09 10:51:51
【问题描述】:
Group[code=a]->Choice[selected=true]
Group[code=a]->Choice[selected=false]
Group[code=a]->Choice[selected=false]
Group[code=b]->Choice[selected=false]
Group[code=b]->Choice[selected=false]
Group[code=c]->Choice[selected=false]
Group[code=c]->Choice[selected=true]

鉴于上述顶点,我正在查询组顶点,其中一个组没有任何选择顶点,选择属性为 true。

因此结果应该只返回 Group b

Group[code=b]

感谢任何帮助。

【问题讨论】:

    标签: orientdb gremlin


    【解决方案1】:

    这是您的图表 - 在询问有关 Gremlin 的问题时,以这种方式提供您的示例数据总是有帮助的:

    graph = TinkerGraph.open()
    g = graph.traversal()
    g.addV('group').property('code','a').as('a').
      addV('group').property('code','b').as('b').
      addV('group').property('code','c').as('c').
      addV('choice').property('selected',true).
      addE('link').from('a').
      addV('choice').property('selected',false).
      addE('link').from('a').
      addV('choice').property('selected',false).
      addE('link').from('a').
      addV('choice').property('selected',false).
      addE('link').from('b').
      addV('choice').property('selected',false).
      addE('link').from('b').
      addV('choice').property('selected',false).
      addE('link').from('c').
      addV('choice').property('selected',true).
      addE('link').from('c').iterate()
    

    获得所需答案的一种方法是进行如下遍历:

    gremlin> g.V().hasLabel('group').
    ......1>   where(__.not(out('link').has('selected',true))).
    ......2>   values('code')
    ==>b
    

    以上答案适用于 TinkerPop 3.x。在 TinkerPop 2.x 中,模式是相同的。你基本上会这样做:

    g.V().has('label','group').filter{ it._().out('link').has('selected',true).hasNext() }
    

    【讨论】:

    • 谢谢! - 虽然这适用于 Tinkerpop 3,但我一直在寻找使用 Tinkerpop 2x 的解决方案,因为 OrientDB 社区版本仍支持旧的 tinkerpop 规范..
    • 我对 2.x 的了解是不稳定的,但模式大致相同。更新了我的答案以包括 2.x 并且还对 3.x 答案进行了额外的改进。遍历现在在遇到第一个 true 时立即停止,而不是遍历所有相邻的顶点,寻找并可能找到多个 true。
    • 非常感谢斯蒂芬!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-04
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    相关资源
    最近更新 更多