Gremlin 是否可以从内部步骤中选择数据

问题描述

我有一个案例

我需要选择 node_1,它有一些边缘,或者只选择一个 node_1 本身

traversal()
        .V().hasLabel("label_1")
        .has("property_key",P.within(numbers)).as("node_1")
        .or(
                __.bothE().hasLabel("label_2").dedup().as("rels").otherV().as("node_2"),__.constant(true)
        )
        .project("n1","r","n2")
        .by(__.select("node_1"))
        .by(__.coalesce(__.unfold().select("rels"),__.unfold()))
        .by(__.coalesce(__.unfold().select("node_2"),__.unfold()))
        .toList()

但结果,当我观看 rels 时,有一个 node_1。 在 node_2 中,还有一个 node_1

你可以看到:

enter image description here

我猜是因为gremlin在步骤结构中找不到relsnode_2

但是在图中 - 这样的顶点和边存在

所以如果我不带“或”写相同的内容

traversal()
        .V().hasLabel("label_1")
        .has("property_key",P.within(numbers)).as("node_1")
        .bothE().hasLabel("label_2").dedup().as("rels").otherV().as("node_2")
        .project("n1","n2")
        .by(__.select("node_1"))
        .by(__.select("rels"))
        .by(__.select("node_2"))
        .toList()

我得到了结果

enter image description here

如你所见

我明白,在第一种情况下,__.unfold() 被触发,所以我们到处都有 node_1

但是我如何获得 relsnode_2 - 我无法理解

是否有任何解决方案可以从 or 步骤获取步骤

或者也许有其他解决方案可以找到

"node_1" + "rels" + "node_2"

或仅查找

"node_1" + default_value + default_value

只使用一个查询

感谢回答!

附言

这里有一些文字可以在谷歌中更好地搜索,我在搜索答案时使用了这些查询,从浏览器历史记录中获取

gremlin 从或选择步骤, 小鬼选择内部步骤, 小鬼选择步骤, gremlin 选择内部步骤名称, gremlin 选择乘法步骤名称, 小鬼作为内部或

解决方法

过滤器内没有到 select() 的路径:

gremlin> g.inject(1,2,3).as('a').or(__.is(1).as('b'),__.is(2).as('c')).path()
==>[1]
==>[2]
gremlin> g.inject(1,3).as('a').filter(__.is(1).as('b')).path()
==>[1]

您实际上需要让 Gremlin 遍历一条路径才能将其添加到历史记录中。我不确定我是否遵循了您要执行的操作,但我认为您可以将整个遍历的结构简化为:

g.V().hasLabel("label_1").
  has("property_key",P.within(numbers)).
  coalesce(bothE("label_2").elementMap(),identity())

这样,您的条件由 coalesce() 处理,并且返回每个边。如果您需要返回两个顶点上的数据,您显然可以将 elementMap() 替换为更强大的东西 - 也许只需将其替换为 project() 变体。