gpt4 book ai didi

gremlin - 如何在 gremlin 中为空遍历添加默认值?

转载 作者:行者123 更新时间:2023-12-01 21:59:01 24 4
gpt4 key购买 nike

我正在处理一个 gremlin 查询,该查询沿多个边导航并最终生成一个 String。根据图形内容,此遍历可能为空。如果遍历最终为空,我想返回一个默认值。

这是我目前正在做的事情:

    GraphTraversal<?, ?> traversal = g.traversal().V().

// ... fairly complex navigation here...

// eventually, we arrive at the target vertex and use its name
.values("name")

// as we don't know if the target vertex is present, lets add a default
.union(
identity(), // if we found something we want to keep it
constant("") // empty string is our default
)
// to make sure that we do not use the default if we have a value...
.order().by(s -> ((String)s).length(), Order.decr)
.limit(1)

这个查询有效,但它相当复杂 - 如果遍历最终没有找到任何东西,我想要的只是一个默认值。

有人有更好的建议吗?我唯一的限制是它必须在 gremlin 本身内完成,即结果必须是 GraphTraversal 类型。

最佳答案

您或许可以通过某种方式使用 coalesce():

gremlin> g.V().has('person','name','marko').coalesce(has('person','age',29),constant('nope'))
==>v[1]
gremlin> g.V().has('person','name','marko').coalesce(has('person','age',231),constant('nope'))
==>nope

如果您有更复杂的逻辑来确定是否找到某些内容,请考虑 choose() 步骤。

关于gremlin - 如何在 gremlin 中为空遍历添加默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54461590/

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