Gremlin 在遍历时查询深度信息

Gremlin querying with the depth information while traversing

我有一个 use-case,其中用户的层次结构类似于

user1 --HAS_CHILD--> user2--HAS_CHILD--> user3 --HAS_CHILD--> user4

对于给定的用户,我需要获取该用户拥有的所有汽车以及该用户的 children 拥有的所有汽车。对于汽车,我需要有用户(所有者)以及该用户与给定用户的深度。

例如。给定的用户是 user2,那么 user3 的深度为 1,user4 的深度为 2.

我可以使用以下查询获取汽车详细信息和车主信息,但如何获取 childDepth

g.V().has("User", "id", "user2")
 .union(
    __.out("OWNS").hasLabel("Car"),
    __.repeat(
       __.out("HAS_CHILD").hasLabel("User")
    ).emit().out("OWNS").hasLabel("Car")
 )
 .project("plateNumber", "owner", "model", "year", "childDepth")
 .by(__.values("plateNumber").fold())
 .by(__.in("OWNS").values("owner").fold())
 .by(__.values("model").fold())
 .by(__.values("year").fold())
 .by(???)

您可以使用 withSack:

g.withSack(0).V().has("User", "id", "user2")
 .union(
    __.out("OWNS").hasLabel("Car"),
    __.repeat(
         __.sack(sum).by(constant(1))
       .out("HAS_CHILD").hasLabel("User")
    ).emit().out("OWNS").hasLabel("Car")
 )
 .project("plateNumber", "owner", "model", "year", "childDepth")
 .by(__.values("plateNumber").fold())
 .by(__.in("OWNS").values("owner").fold())
 .by(__.values("model").fold())
 .by(__.values("year").fold())
 .by(__.sack())