如何在 gremlin 中进行遍历的交集

How to do intersection of traversals in gremlin

假设我有 2 个匿名遍历 A 和 B。遍历 A 的结果是 V[1]、V[2]、V[3],遍历 B 的结果是 V[3]、V[ 4] 和 V[5].

A 和 B 都在搜索不在任何单个顶点中的属性。

"A" 遍历搜索 属性 (x == y) 在 V[1] 中,V[1] 连接到 V[2] 和 V[3]部分遍历结果

"B" 遍历搜索 属性 (u == w) 在 V[5] 中,V[5] 连接到 V[3] 和 V[4]部分遍历结果

如何找到这些结果的交集?

我试过:

__.and(A, B)

但是这样的结果不是交集

例如:

A = __.has('name', 'marko').out()

B = __.has('name', 'josh').out()

__.and(A, B) 不正确。

注意:为简单起见,采用上述示例。在实际查询中,A 和 B 相当大,每个查询中都有多个联合。

我浏览了以下链接:

https://groups.google.com/forum/#!msg/gremlin-users/6_MRJxBnivo/wT_71IAzCwAJ

这里的所有建议都提供了一种交集的方法,前提是顶点满足所有条件,但这里不是这种情况。

所以你可以尝试用 groupCount 步骤做交集:

g.V().union(
    __.has('name', 'marko').out(),
    __.has('name', 'josh').out()
).groupCount().by().unfold()
.where(select(values).is(gt(1))).select(keys)

另一种方法是使用斯蒂芬在您提到的 post 中建议的简化查询:

g.V().has('name', 'marko').out().as('a').
V().has('name', 'josh').out().as('b').
select('a').
where('a',eq('b'))

我在这里都试过了: https://gremlify.com/31