首页 > 解决方案 > gremlin order by with coalesce 重复了一些值

问题描述

在某些情况下,当我使用order().by(...)with时会得到莫名其妙的结果coalesce(...)。使用标准的现代图,

gremlin> g.V()
          .hasLabel("person")
          .out("created")
          .coalesce(values("name"), constant("x"))
          .fold()
==>[lop,lop,ripple,lop]

但是,如果我在合并之前按名称排序,我会得到 9lop而不是 3:

gremlin> g.V()
          .hasLabel("person")
          .out("created")
          .order().by("name")
          .coalesce(values("name"), constant("x"))
          .fold()
==>[lop,lop,lop,lop,lop,lop,lop,lop,lop,ripple]

为什么两个查询之间的元素数量不同?

标签: gremlintinkerpop

解决方案


这看起来像一个错误 - 我在 JIRA 中创建了一个问题。有一种解决方法,但首先要考虑到即使将错误放在一边,您的遍历也不会真正起作用,order()因为您引用了by()调制器中可能不存在的键,所以会失败。因此,您需要以不同的方式考虑这一点:

g.V().
  hasLabel("person").
  out("created").
  order().by(coalesce(values('name'),constant('x')))

然后我曾经choose()coalesce()应该做的事情:

g.V().
  hasLabel("person").
  out("created").
  order().by(coalesce(values('name'),constant('x'))).
  choose(has("name"),values('name'),constant('x')).
  fold()

这似乎工作正常。


推荐阅读