[英]Gremlin continue traversal only if 2 vertices are not the same
我有一个查询,它查看 2 个不同的顶点,如果它们不都通过“包含”边的路径滚动到同一个根祖先,我想停止遍历。
g.V('node1')
.until(hasLabel('root')).repeat(in('contains')).as('node1Root')
.V('node2')
.until(hasLabel('root')).repeat(in('contains')).as('node2Root')
//FILTER|WHERE clause
在继续遍历之前,我想确认 node1Root 和 node2root 是同一个顶点,但是对于我来说,我无法弄清楚如何做到这一点。
我尝试了以下方法:
g.V('node1')
.until(hasLabel('root')).repeat(in('contains')).as('node1Root')
.V('node2')
.until(hasLabel('root')).repeat(in('contains')).as('node2Root')
//.where('node1Root', P.eq('node2Root')
//.where(select("node1Root").is(P.eq("node2Root")))
//.where(select("node1Root").is("node2Root"))
有趣的是,以下查询确实可以进行适当的过滤。
g.V('node1').as('1')
.V('node2').as('2')
.where('1', P.eq('2'))
我不确定直到/重复是否有什么问题导致它搞砸了,或者我只是在做一些公然错误的事情。 任何帮助将非常感激。
谢谢!
我发现如何检查 Gremlin 中早期查询部分的节点是否相等?
并且您似乎使用“as”与之前的“as”具有相同的键,并且它们是否匹配其认为相等。
所以这是赢家(我认为):
g.V('node1')
.until(hasLabel('root')).repeat(in('contains')).as('node1Root')
.V('node2')
.until(hasLabel('root')).repeat(in('contains')).as('node2Root')
.where(select('node1Root').as('node2Root')
//.not(select('node1Root').as('node2Root')) //OR this to determine they aren't the same
//continue traversal
我还发现我最初的问题是 .until().repeat() 步骤可以返回一个 LIST,但在我的情况下,我知道我的图表 model 将始终返回一个“根”,所以为了让它工作,我可以使用“展开”
g.V('node1')
.until(hasLabel('root')).repeat(in('contains')).unfold().as('node1Root')
.V('node2')
.until(hasLabel('root')).repeat(in('contains')).unfold().as('node2Root')
.where('node1Root', P.eq('node2Root')
我想我会采用第二种解决方案,因为我对此更有信心,除非我听到其他情况。
你可以试试这个 gremlin 查询
g.V(node1-id)
.map(until(hasLabel('root')).repeat(in().aggregate('x')).cap('x')).as("array")
.V(node2-id)
.until(
as("i").select("array").unfold().as("j")
.where("i", eq("j"))
).repeat(in())
在这里,我们将从 node1 到根的路径中的所有顶点放入一个数组中,其次我们正在检查数组中是否存在节点。
此查询只能与只有一次迭代的遍历一起使用,因为aggregate
步骤收集到一个全局变量以进行遍历,这意味着每次迭代它将是相同的数组。 解决此问题 如果您在 jvm 上执行此操作,请使用 lamda/groovy 闭包
g.V(node-start-id-1,node-start-id-2)
.map(
{ x->
var v = x.get()
var g = getGraph().get().traversal();
g.V(v.id())until(hasLabel('root')).repeat(in().aggregate('x')).cap('x')).next()
}
)
.as("array")
.V(node2-id)
.until(
as("i").select("array").unfold().as("j")
.where("i", eq("j"))
).repeat(in())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.