简体   繁体   English

在 gremlin 查询中显示子级别

[英]Displaying sub-levels in gremlin query

I have the graph as shown in the figure below.我有如下图所示的图表。 The numbers represent the level format of the node which is required as output from the gremlin query along with the properties of the nodes.这些数字表示节点的级别格式,它是 gremlin 查询中所需的 output 以及节点的属性。 The graph structure can change that is more nodes can be added to the graph.图形结构可以更改,即可以将更多节点添加到图形中。 The level must be returned in a similar format for additional nodes.对于其他节点,必须以类似的格式返回级别。 For example, children of 1.1.1 should return the level as 1.1.1.1,1.1.1.2... The following query but the level is in continuous format 1,2,3...例如,1.1.1 的子级应返回级别为 1.1.1.1,1.1.1.2... 以下查询但级别为连续格式 1,2,3...

g.withSack(0).
  V().
  hasLabel('A').
  has('label_A','A').
  emit().
  repeat(sack(sum).by(constant(1)).out()).
    project('depth', 'properties').
      by(sack()).
      by(valueMap())

A is starting root node. A 是起始根节点。

I know its too complicated.我知道它太复杂了。 If not possible can we at least get the sub depth along with depth using multiple sack variables.如果不可能,我们至少可以使用多个 sack 变量获得子深度和深度。 Following is the example:以下是示例:

depth:0 sub-depth:0 depth:1 sub-depth:1.1 depth:1 sub-depth:1.2 depth:2 sub-depth:2.1 depth:2 sub-depth:2.2 depth:2 sub-depth:2.3 depth:2 sub-depth:2.4深度:0 子深度:0 深度:1 子深度:1.1 深度:1 子深度:1.2 深度:2 子深度:2.1 深度:2 子深度:2.2 深度:2 子深度:2.3 深度: 2子深度:2.4 在此处输入图像描述

A simple way to do what you are looking for is to take advantage of the index step.一个简单的方法来做你正在寻找的东西是利用index步骤。 If we create a simple binary tree as follows:如果我们创建一个简单的二叉树如下:

g.addV('root').property('data',9).as('root').   
  addV('node').property('data',5).as('b').   
  addV('node').property('data',2).as('c').   
  addV('node').property('data',11).as('d').   
  addV('node').property('data',15).as('e').   
  addV('node').property('data',10).as('f').   
  addV('node').property('data',1).as('g').   
  addV('node').property('data',8).as('h').   
  addV('node').property('data',22).as('i').   
  addV('node').property('data',16).as('j').   
  addE('left').from('root').to('b').
  addE('left').from('b').to('c').
  addE('right').from('root').to('d').
  addE('right').from('d').to('e').
  addE('right').from('e').to('i').
  addE('left').from('i').to('j').
  addE('left').from('d').to('f').
  addE('right').from('b').to('h').
  addE('left').from('c').to('g').iterate()    

We can combine loops and index as follows (I added the unfold to improve readability):我们可以如下组合loopsindex (我添加了unfold以提高可读性):

gremlin> g.V().hasLabel('root').
......1>       emit().
......2>       repeat(group('x').by(loops()).by(values('data').fold().index()).out()).
......3>       cap('x').unfold()   

==>0=[[9, 0]]
==>1=[[5, 0], [11, 1]]
==>2=[[2, 0], [8, 1], [10, 2], [15, 3]]
==>3=[[1, 0], [22, 1]]
==>4=[[16, 0]]

Given your comment about a simpler form being acceptable I think the above gets pretty close.鉴于您对可以接受的更简单形式的评论,我认为上述内容非常接近。 You should be able to tweak this query to make any changes in the output formatting that you require.您应该能够调整此查询以对您需要的 output 格式进行任何更改。

You can go one step further and group using the parent vertex as follows.您可以进一步使用 go 并使用父顶点进行分组,如下所示。 From this you can build whatever projections of the final results you require.由此,您可以构建所需的最终结果的任何预测。

gremlin> g.V().hasLabel('root').
......1>       repeat(outE().group('x').
......2>         by(loops()).
......3>         by(group().
......4>           by(outV()).
......5>           by(inV().values('data').fold().index())).
......6>         inV()).
......7>         times(4).
......8>       cap('x').
......9>       unfold() 

==>0={v[0]=[[5, 0], [11, 1]]}
==>1={v[2]=[[2, 0], [8, 1]], v[6]=[[10, 0], [15, 1]]}
==>2={v[4]=[[1, 0]], v[8]=[[22, 0]]}
==>3={v[16]=[[16, 0]]}       

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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