用Gremlin递归查询更简单的树状结构 [英] Recursively query simpler tree-like structures with Gremlin
问题描述
请考虑以下数据:
g.addV('RootTopic').property('name', 'A').as('A')
.addV('RootTopic').property('name', 'M').as('M')
.addV('Topic').property('name', 'A1').as('A1')
.addV('Topic').property('name', 'A2').as('A2')
.addV('Topic').property('name', 'B1').as('B1')
.addV('Topic').property('name', 'B2').as('B2')
.addV('Topic').property('name', 'N1').as('N1')
.addV('Topic').property('name', 'N2').as('N2')
.addV('Topic').property('name', 'O1').as('O1')
.addE('refines').from('A').to('A1')
.addE('refines').from('A').to('A2')
.addE('refines').from('A1').to('B1')
.addE('refines').from('A1').to('B2')
.addE('refines').from('M').to('N1')
.addE('refines').from('M').to('N2')
.addE('refines').from('N2').to('O1')
.addE('refines').from('N2').to('O2')
我想要的是使用tree()
步骤得到的东西:
What I would like is something that one gets by using the tree()
-step:
g.V().hasLabel('RootTopic').repeat(out()).times(2).emit().tree()
但是,这会拉出整个顶点.在这种情况下,我真正需要的是顶点的属性,例如名称,这样我们就可以得到一棵包含例如只是顶点的名称属性.
However, this pulls out the full vertex. What I really just need in this situation are properties of the vertex, e.g. the name, such that we get a tree that contains e.g. just the name-property of the Vertex.
我知道,如果我写.tree().by('name')
,我似乎得到了一个以名称为键的树,但是我试图找到一种方法,使我可以选择例如顶点的多种属性,例如只是具有某些特定元属性的某种属性.
I know that if I write .tree().by('name')
I seem to get a tree with the name as key, but I am trying to find a way which would allow me to select e.g. multiple properties of a Vertex, or e.g. just a certain property having some specific meta-property.
这可能吗?
推荐答案
by()
调制器可以将多个属性键值用作参数.您还可以传入匿名遍历,从而允许:
The by()
modulator can take more than just a property key value as an argument. You can also pass in an anonymous traversal which would thus allow:
g.V().hasLabel('RootTopic').
repeat(out()).times(2).
emit().
tree()
by(values('name','k1','k2').fold())
,否则,如果输出更为复杂,则可以使用project()
:
or you might use project()
if you had more complex output:
g.V().hasLabel('RootTopic').
repeat(out()).times(2).
emit().
tree()
by(project('name','k1','degree').
by('name').
by('k1').
by(both().count())
这里要讲的要点是,通过匿名遍历,您可以开发几乎任何想要的输出.
The main point to take away here is that with an anonymous traversal you can develop just about any output you would like.
这篇关于用Gremlin递归查询更简单的树状结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!