gpt4 book ai didi

graph-databases - Gremlin 在遍历时查询深度信息

转载 作者:行者123 更新时间:2023-12-04 23:35:38 25 4
gpt4 key购买 nike

我有一个用例,其中用户处于这样的层次结构中

用户 1 --HAS_CHILD--> 用户 2 --HAS_CHILD--> 用户 3 --HAS_CHILD--> 用户 4

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

前任。给定的用户是 用户 2 ,那么 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())

关于graph-databases - Gremlin 在遍历时查询深度信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58136852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com