繁体   English   中英

Gremlin查询如何比较遍历相同分支中的边的属性?

[英]Gremlin query how to compare properties of edges in same branch of my traversal?

我正在努力编写一个gremlin查询,以比较遍历给定分支的每个边的每个边的值以及提供的值。

这是一个示例-假设我具有由SchoolsTeamsAthletes组成的以下层次结构,它们中的任何一个都可以通过wears边缘连接到Uniform 还要说我将每个边的创建日期记录为该边中名为created的属性。 在任何可能的分支中,例如用蓝色突出显示的分支,我都需要查询maximum(wears.created, establishes.created, recruits.created) >= 2009

在此处输入图片说明

如何显示运动员自2009年以来穿的每套球衣以及他们开始穿着的日期?

有时该日期是运动员被招募入队的那一年,有时是学校建立一支队的年,而有时则是每一个运动员,球队或学校开始穿新制服的日期。

我需要一种方法来选择一个分支中所有边的最大创建日期(例如上面蓝色中的一个),然后还要将该最大值与提供的2009年进行比较。

像这样的东西? 在gremlin中有可能发生这种情况吗?

g.V().hasLabel("Athlete").as("athlete").union(
    __.outE("wears").has("created", P.gte(2009)).as("when").by("created"),
    __.inE("recruits").as("r").by("created").outV().hasLabel("Team")
        .outE("wears").as("w").by("created")
        .select(max("r", "w")).as("when")
        .where("when", P.gte(2009))
    __.inE("recruits").as("r").by("created").outV().hasLabel("Team")
        .inE("establishes").as("e").by("created").outV().hasLabel("School")
        .outE("wears").as("w").by("created")
        .select(max("r", "e", "w")).as("when")
        .where("when", P.gte(2009))
).inV().hasLabel("Uniform").as("uniform")
.select("athlete", "uniform", "when")

<==编辑============================================== ===>

根据Steven Mallette的评论添加启动脚本以协助测试。

g.addV('School').property('id',1).property("name", "Duke").as('duke').
  addV('School').property('id',2).property("name", "UNC").as('unc').
  addV('Team').property('id',3).property("name", "Soccer").as('soccer').
  addV('Team').property('id',4).property("name", "Football").as('football').
  addV('Team').property('id',5).property("name", "Basketball").as('basketball').
  addV('Athlete').property('id',6).property("name", "Joe").as('joe').
  addV('Athlete').property('id',7).property("name", "Jane").as('jane').
  addV('Athlete').property('id',8).property("name", "Alice").as('alice').
  addV('Athlete').property('id',9).property("name", "Bob").as('bob').
  addV('Uniform').property('id',10).property("color", "red").as('red').
  addV('Uniform').property('id',11).property("color", "pink").as('pink').
  addV('Uniform').property('id',12).property("color", "blue").as('blue').
  addV('Uniform').property('id',13).property("color", "teal").as('teal').
  addV('Uniform').property('id',14).property("color", "green").as('green').
  addE('contains').property("created", 2009).from('duke').to('soccer').
  addE('contains').property("created", 1960).from('unc').to('football').
  addE('contains').property("created", 2007).from('duke').to('basketball').
  addE('contains').property("created", 2016).from('soccer').to('bob').
  addE('contains').property("created", 2008).from('basketball').to('jane').
  addE('contains').property("created", 2010).from('basketball').to('alice').
  addE('contains').property("created", 2015).from('football').to('joe').
  addE('wears').property("created", 2009).from('duke').to('blue').
  addE('wears').property("created", 1999).from('unc').to('red').
  addE('wears').property("created", 2010).from('soccer').to('teal').
  addE('wears').property("created", 2009).from('football').to('pink').
  addE('wears').property("created", 2009).from('basketball').to('teal').
  addE('wears').property("created", 2012).from('alice').to('green')

预期输出如下(根据Daniel Kuppitz的建议添加输出)。 解释输出的第一行:因为篮球队于2007年在duke成立,Jane自2009年以来就一直戴水鸭,Jane于2008年加入该队,但篮球队于2009年开始戴水鸭,因此2009年是Jane和这个制服。

Jane wears teal since 2009
Jane wears blue since 2009
Alice wears teal since 2010
Alice wears blue since 2010
Alice wears green since 2012
Joe wears pink since 2015
Joe wears red since 2015
Bob wears blue since 2016
Bob wears teal since 2016

希望在最近的评论之后进行最终编辑(但现在的结果符合您的预期结果,所以我认为我们很好):

gremlin> g.V().hasLabel("Athlete").as("a").
           union(outE("wears").sack(assign).by("created"),
                 inE("contains").sack(assign).by("created").outV().
                 union(outE("wears").sack(max).by("created"),
                       inE("contains").sack(max).by("created").outV().
                       outE("wears").sack(max).by("created"))).
           filter(sack().is(gte(2009))).
           project("athlete","when","uniform").
             by(select("a").by("name")).
             by(sack()).
             by(inV().values("color"))
==>[athlete:Joe,when:2015,uniform:pink]
==>[athlete:Joe,when:2015,uniform:red]
==>[athlete:Jane,when:2009,uniform:teal]
==>[athlete:Jane,when:2009,uniform:blue]
==>[athlete:Alice,when:2012,uniform:green]
==>[athlete:Alice,when:2010,uniform:teal]
==>[athlete:Alice,when:2010,uniform:blue]
==>[athlete:Bob,when:2016,uniform:teal]
==>[athlete:Bob,when:2016,uniform:blue]

或者,由于您的架构非常统一,因此您实际上可以使用repeat() ,从而摆脱不可读的嵌套union()

gremlin> g.withSack(0).V().hasLabel("Athlete").as("a").
           emit().
             repeat(inE("contains").sack(max).by("created").outV()).
             times(2).
           outE("wears").sack(max).by("created").
           filter(sack().is(gte(2009))).
           project("athlete","when","uniform").
             by(select("a").by("name")).
             by(sack()).
             by(inV().values("color"))
==>[athlete:Joe,when:2015,uniform:pink]
==>[athlete:Joe,when:2015,uniform:red]
==>[athlete:Jane,when:2009,uniform:teal]
==>[athlete:Jane,when:2009,uniform:blue]
==>[athlete:Alice,when:2012,uniform:green]
==>[athlete:Alice,when:2010,uniform:teal]
==>[athlete:Alice,when:2010,uniform:blue]
==>[athlete:Bob,when:2016,uniform:teal]
==>[athlete:Bob,when:2016,uniform:blue]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM