You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gremlin中Filter内修改Sack无效,foundProps始终为0的问题

问题:Gremlin查询中sack()返回0,无法获取匹配属性数量

我正在通过属性过滤顶点,并使用project()步骤对其进行映射。希望在结果中获取所有至少有一个属性匹配过滤条件的顶点,以及匹配属性的数量。我已尝试使用as()结合select(),但得到了空值;aggregate()步骤返回的数组包含了所有遍历器的匹配属性数量。现在尝试使用sack(),但foundProps字段始终返回0。

以下是我的简化查询语句:

g.V(someId)
    .project("child_label","other_child_label")
    .by(
       __.out()
       .hasLabel("child_label")
       .filter(
               __.properties()
               .filter(__.key().is(Text.contains("foo")))
               .filter(__.value().is(Text.contains("bar")))
               .fold()
               .sack(assign).by(__.count())
               .count()
               .is(P.gt(0L))
       )
       .project("name","foundProps")
       .by("name")
       .by(__.sack())
       .fold()
    )
  // 逻辑完全一致,只是多了一层out()步骤
  .by(
       __.out()
       .out()
       .hasLabel("child_label")
       .filter(
               __.properties()
               .filter(__.key().is(Text.contains("foo")))
               .filter(__.value().is(Text.contains("bar")))
               .fold()
               .sack(assign).by(__.count())
               .count()
               .is(P.gt(0L))
       )
       .project("name","foundProps")
       .by("name")
       .by(__.sack())
       .fold()
    )
)

我已验证count()能返回正确结果,请问为什么foundProps始终为0?


原因及解决方案

问题原因

你在filter步骤内部对fold()后的结果执行sack(assign),但这个操作仅作用于filter内部的遍历分支,并没有将赋值传递到外部的顶点遍历器上。当后续调用__.sack()时,获取的是顶点遍历器的初始sack值(默认0),自然得不到正确的计数。

修正方案

不要在filter内部处理计数,而是先为每个顶点计算匹配属性的数量,将其作为临时值存储,再过滤出数量大于0的顶点,最后在project中直接引用这个计数:

g.V(someId)
    .project("child_label", "other_child_label")
    .by(
        __.out()
           .hasLabel("child_label")
           // 先计算当前顶点的匹配属性数量,存储为临时变量
           .as("vertex")
           .properties()
           .filter(__.key().is(Text.contains("foo")))
           .filter(__.value().is(Text.contains("bar")))
           .count()
           .as("matchCount")
           // 过滤出匹配数>0的顶点
           .filter(select("matchCount").is(P.gt(0)))
           // 回到顶点,进行project映射
           .select("vertex")
           .project("name", "foundProps")
           .by("name")
           .by(select("matchCount"))
           .fold()
    )
    .by(
        __.out().out()
           .hasLabel("child_label")
           .as("vertex")
           .properties()
           .filter(__.key().is(Text.contains("foo")))
           .filter(__.value().is(Text.contains("bar")))
           .count()
           .as("matchCount")
           .filter(select("matchCount").is(P.gt(0)))
           .select("vertex")
           .project("name", "foundProps")
           .by("name")
           .by(select("matchCount"))
           .fold()
    )

或者更简洁的写法,用project先计算计数再过滤:

g.V(someId)
    .project("child_label", "other_child_label")
    .by(
        __.out()
           .hasLabel("child_label")
           .project("name", "matchCount")
           .by("name")
           .by(
               __.properties()
                  .filter(__.key().is(Text.contains("foo")))
                  .filter(__.value().is(Text.contains("bar")))
                  .count()
           )
           .filter(select("matchCount").is(P.gt(0)))
           // 重命名字段为需求的foundProps
           .project("name", "foundProps")
           .by(select("name"))
           .by(select("matchCount"))
           .fold()
    )
    .by(
        __.out().out()
           .hasLabel("child_label")
           .project("name", "matchCount")
           .by("name")
           .by(
               __.properties()
                  .filter(__.key().is(Text.contains("foo")))
                  .filter(__.value().is(Text.contains("bar")))
                  .count()
           )
           .filter(select("matchCount").is(P.gt(0)))
           .project("name", "foundProps")
           .by(select("name"))
           .by(select("matchCount"))
           .fold()
    )

这两种写法都能正确获取每个顶点的匹配属性数量,同时过滤掉没有匹配属性的顶点。


内容的提问来源于stack exchange,提问作者Zonko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 01:58:14