[英]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.