![](/img/trans.png)
[英]How to write a gremlin query that finds common vertices between other vertices and return sorted by the count of overlap?
[英]How to write Gremlin query to find parent vertices which have a specified edge?
我是gremlin查詢的新手。 我有一個如下圖,我的源頂點是P3,我想編寫一個查詢,該查詢將獲取所有父\\祖先頂點(如果該頂點到具有邊緣的P3路徑,則該頂點是P3的父\\祖先頂點類型為“零件”的“包含”),並具有與其相關聯的所有者。 因此,在這種情況下,查詢應返回P1和P2,但不返回P。
查詢以創建樣本數據:
g.addV(id, 'P1').property('label','part').as('p1')
.addV(id, 'P2').property('label','part').as('p2')
.addV(id, 'P3').property('label','part').as('p3')
.addV(id, 'P4').property('label','part').as('p4')
.addV(id, 'owner1').property('label','owner').as('o1')
.addV(id, 'owner2').property('label','owner').as('o2')
.addE('contains').from('p1').to('p2')
.addE('contains').from('p2').to('p3')
.addE('contains').from('p4').to('p3')
.addE('owns').from('o1').to('p1')
.addE('owns').from('o2').to('p2')
這是我提出的查詢,但是一旦找到與之關聯的所有者頂點的零件頂點,遍歷就會停止。 如何更新它以同時返回P1和P2。
g.V('P3')
.union(
inE().hasLabel('owns').inV(),
repeat(inE().hasLabel('contains')
.outV().hasLabel('part'))
.until(inE().hasLabel('owns'))
).dedup()
我也嘗試使用sideEffect步驟來收集零件頂點,但是沒有得到所需的結果。
g.V('P3').union(
inE().hasLabel('owns').inV(),
repeat(inE().sideEffect(hasLabel('owns').outV().as('parts'))
.hasLabel('contains')
.outV().hasLabel('part'))
)
.select('parts').dedup()
由於語法錯誤,我對示例數據代碼進行了一些修改:
gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV('part').property(id, 'P1').as('p1').
......1> addV('part').property(id, 'P2').as('p2').
......2> addV('part').property(id, 'P3').as('p3').
......3> addV('part').property(id, 'P4').as('p4').
......4> addV('owner').property(id, 'owner1').as('o1').
......5> addV('owner').property(id, 'owner2').as('o2').
......6> addE('contains').from('p1').to('p2').
......7> addE('contains').from('p2').to('p3').
......8> addE('contains').from('p4').to('p3').
......9> addE('owns').from('o1').to('p1').
.....10> addE('owns').from('o2').to('p2').iterate()
我認為您可以將遍歷簡化為一個簡單的repeat()
:
gremlin> g.V('P3').emit(inE('owns')).repeat(__.in('contains'))
==>v[P2]
==>v[P1]
請注意emit()
步驟的位置,該步驟控制從循環輸出的頂點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.