[英]Gremlin query with nested vertices
My use case is: Bag
vertex has edge holds
to Box
vertex and Box
vertex has edge contains
to Fruit
vertex. 我的用例是: Bag
顶点对Box
顶点具有边holds
,而Box
顶点对Fruit
顶点具有边contains
。 So it's a parent-child relation between 3 vertices. 因此,这是3个顶点之间的父子关系。
Is it possible to write gremlin query which returns all related 3 vertices. 是否可以编写返回所有相关3个顶点的gremlin查询。 for eg i need to fetch all Bags by id
including Box
vertex and further down to Fruit
vertex for that Bag id. 例如,我需要按id
提取所有Bags(包括Box
顶点),然后进一步获取该Bag ID的Fruit
顶点。 In SQL like syntax it's a simple select * from bag where id = 1
. 在类似SQL的语法中,这是一个简单的select * from bag where id = 1
。
sample structure: 样本结构:
g.addV('bag').property('id',1).property('name','bag1').property('size','12').as('1').
addV('box').property('id',2).property('name','box1').property('width','12').as('2').
addV('fruit').property('id',3).property('name','apple').property('color','red').as('3').
addV('bag').property('id',4).property('name','bag2').property('size','44').as('4').
addV('box').property('id',5).property('name','box2').property('width','14').as('5').
addV('fruit').property('id',6).property('name','orange').property('color','yellow').as('6').
addE('holds').from('1').to('2').
addE('contains').from('2').to('3').
addE('holds').from('4').to('5').
addE('contains').from('5').to('6').iterate()
I want to get all properties of 1
, 2
, 3
when i query for vertices 1
. 我想获得的所有特性1
, 2
, 3
,当我查询顶点1
。
I want the response in the below format. 我想要以下格式的回复。
"bags" : [{ "id":"1", "name":"bag1", "size" :"12", "boxes":[ { "id" : "2", "name":"box1", "width" : "12", "fruits": [{ "id":"3", "name" : "apple", "color" : "red" }] }] }, { "id":"4", "name":"bag2", "size" : "44", "boxes":[ { "id" : "5", "name":"box2", "width" : "44", "fruits": [{ "id":"6", "name" : "orange" "color" : "yellow" }] }] }]
But not sure if similar case is possible in gremlin as there are no implicit relation between vertices. 但不确定在gremlin中是否可能存在类似情况,因为顶点之间没有隐式关系。
I would probably use project()
to accomplish this: 我可能会使用project()
完成此操作:
gremlin> g.V().hasLabel('bag').
......1> project('id', 'name','boxes').
......2> by('id').
......3> by('name').
......4> by(out('holds').
......5> project('id','name','fruits').
......6> by('id').
......7> by('name').
......8> by(out('contains').
......9> project('id','name').
.....10> by('id').
.....11> by('name').
.....12> fold()).
.....13> fold())
==>[id:1,name:bag1,boxes:[[id:2,name:box1,fruits:[[id:3,name:apple]]]]]
==>[id:4,name:bag2,boxes:[[id:5,name:box2,fruits:[[id:6,name:orange]]]]]
I omitted the "bags" root level key as there were no other keys in the Map
and it didn't seem useful to add that extra level. 我省略了“ bags”根级别键,因为Map
中没有其他键,添加额外的级别似乎没有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.