[英]Gremlin - finding connected nodes with several boolean conditions on both nodes and edges properties
[英]Finding all indirectly connected nodes with specific relationship Gremlin
假設我在Gremlin中有一個Node的數字ID。
g.V(n_id)
說這個節點是一個話題。
每個主題都可能有一個與threadOf
關系的問題。
每個問題都可以具有與threadOf
關系的答案或評論
如果我將數字ID作為輸入,則需要一個gremlin查詢,該查詢返回與此主題相關的所有問題以及與這些問題相關的所有答案或評論
所有關系都是threadOf
Gremlin有可能嗎?
使用Gremlin有幾種方法可以做到這一點。 讓我們假設這個圖(對於Gremlin問題,像這樣在問題本身中包含一個小的圖樣本總是有幫助的):
gremlin> graph = TinkerGraph.open()
==>tinkergraph[vertices:0 edges:0]
gremlin> g = graph.traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV('topic').property('text','topic').as('t').
......1> addV('question').property('text','question 1').as('q1').
......2> addV('question').property('text','question 2').as('q2').
......3> addV('comment').property('text','comment 1').as('c1').
......4> addV('comment').property('text','comment 2').as('c2').
......5> addV('answer').property('text','answer 1').as('a1').
......6> addV('answer').property('text','answer 2').as('a2').
......7> addE('threadOf').from('t').to('q1').
......8> addE('threadOf').from('t').to('q2').
......9> addE('threadOf').from('q1').to('c1').
.....10> addE('threadOf').from('q1').to('c2').
.....11> addE('threadOf').from('q1').to('a1').
.....12> addE('threadOf').from('q2').to('a2').iterate()
上圖是一棵樹,因此最好將其返回為一棵。 為此,我們可以使用tree step 。 主題位於頂點ID“ 0”,因此,如果我們想要所有的“ threadOf”層次結構,我們可以這樣做:
gremlin> g.V(0L).out('threadOf').out('threadOf').tree().by('text')
==>[topic:[question 1:[comment 2:[],comment 1:[],answer 1:[]],question 2:[answer 2:[]]]]
那是可行的,但是假設我們知道“ threadOf”邊的樹的深度(我們從頂點“ 0”步兩次out()
。如果我們不知道深度,我們可以這樣做:
gremlin> g.V(0L).repeat(out('threadOf')).emit().tree().by('text')
==>[topic:[question 1:[comment 2:[],comment 1:[],answer 1:[]],question 2:[answer 2:[]]]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.