繁体   English   中英

查找具有特定关系的所有间接连接的节点Gremlin

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM