【问题标题】:How to search multiple values for same propertykey in gremlin如何在 gremlin 中搜索相同属性键的多个值
【发布时间】:2017-02-24 12:44:04
【问题描述】:

用相同的属性键获取值的最佳方法是什么?

编辑:很抱歉改变了我的要求是从任何一个部门获得员工

我需要获取为 IT 或销售部门工作并由 ID 为 123 的经理管理的所有员工。

我用过

g.V().has('managerId',123).out('manages').as('employee')
   .out('worksFor').has('departmentName','IT','Sales')
   .select('employee')

out('worksAt') 给出部门。

我们可以在has() 步骤中执行此操作,还是应该使用union() 之类的步骤

g.V().has('managerId',123).out('manages').as('employee').out('worksFor')
    .union(__.has('departmentName','IT'),__.has('departmentName','Sales')
    .select('employee')

【问题讨论】:

    标签: gremlin tinkerpop tinkerpop3


    【解决方案1】:

    您可能只缺少within predicate,这也在has step in the TinkerPop documentation 的上下文中进行了解释:

    g.V().has('managerId',123).out('manages').as('employee').out('worksFor').
        has('departmentName',within('IT','Sales')).select('employee')
    

    编辑:阅读斯蒂芬的回答后,我注意到我阅读了您问题中的

    为 IT 销售部门工作的员工

    这使我的回答当然无效。我仍然把它留在这里,以防你以后使用union 步骤表明你实际上是指

    【讨论】:

    • 嘿!如何在方法内查询 gremlin-python?
    • 它应该与 gremlin-python 以完全相同的方式工作。你可能只需要some imports
    【解决方案2】:

    这是一个示例图:

    gremlin> graph = TinkerGraph.open()
    ==>tinkergraph[vertices:0 edges:0]
    gremlin> g = graph.traversal()
    ==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
    gremlin> g.addV("managerId",123).as("manager").
    ......1>   addV("employee","A").as("A").
    ......2>   addV("employee","B").as("B").
    ......3>   addV("department", "IT").as("it").
    ......4>   addV("department", "Sales").as("sales").
    ......5>   addE("manages").from("manager").to("A").
    ......6>   addE("manages").from("manager").to("B").
    ......7>   addE("worksFor").from("A").to("it").
    ......8>   addE("worksFor").from("B").to("it").
    ......9>   addE("worksFor").from("A").to("sales").iterate()
    

    在这种情况下,我使员工 A 同时在“销售”和“IT”中,但员工 B 仅在“IT”中。既然你说你想要在两个部门工作的员工,员工 A 应该从查询中返回,B 应该被过滤掉。

    请注意,在这种情况下使用 within 会产生不正确的答案:

    gremlin> g.V().has('managerId',123).
    ......1>   out('manages').
    ......2>   where(out('worksFor').
    ......3>         has('department',within('IT','Sales'))).
    ......4>   valueMap()
    ==>[employee:[A]]
    ==>[employee:[B]]
    

    如果您需要两个部门,请使用以下方法:

    gremlin> g.V().has('managerId',123).
    ......1>   out('manages').
    ......2>   where(out('worksFor').
    ......3>         has('department','Sales')).
    ......4>   where(out('worksFor').
    ......5>         has('department','IT')).
    ......6>   valueMap()
    ==>[employee:[A]]
    

    【讨论】:

    • 谢谢斯蒂芬,我有一个小小的疑问,我不能使用 has() 步骤两次来获得相同的结果
    • 我的要求是让所有为 IT 或销售工作的员工
    • 然后弗洛里安·霍克曼最初发布了正确的答案,尽管我更喜欢使用within 的答案,因为你摆脱了as('employee')select('employee')。 imo,where 的使用一目了然更容易阅读/理解。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    相关资源
    最近更新 更多