2017-06-16 52 views
1

假设我有一个节点的数值ID中的Gremlin ..与寻找具有特定关系的所有间接连接的节点的Gremlin

g.V(n_id) 

利用说这个节点是一个课题。

每个主题都可以有关系的问题threadOf

每个问题可以有一个答案,或者如果我得到一个数字ID作为输入,我想一个小鬼查询返回所有与此主题相关的问题和所有答案或评论的关系threadOf

评论有关这些问题

所有的关系都是threadOf

这是可能的小鬼?

回答

5

有几种方法可以用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:[]]]] 
+0

感谢非常详细的解答..当我用'in',而不是第一溶液中的部分作品“out”,正如你所说我不知道​​树的深度,所以需要使用第二种解决方案,但是当我使用'in'时,我得到了一个意外的令牌错误 –

+0

'in'是groovy中的一个保留字所以你需要d用'in()'步骤的静态导入的类名作为前缀。换句话说,'g.V(0L).repeat(__。in('threadOf'))。emit()。tree()。​​by('text')' –

+0

令人惊叹!非常感谢 –

相关问题