我有一个像这样的小图:
g.addV('person').property(id, 'p1').property('name', 'mark')
g.addV('person').property(id, 'p2').property('name', 'mark')
g.addV('person').property(id, 'p3').property('name', 'jack')
g.addV('person').property(id, 'p4').property('name', 'steve')
g.addE('knows').from(V('p1')).to(V('p2')).property('since', 2001)
g.addE('knows').from(V('p1')).to(V('p3')).property('since', 2010)
g.addE('knows').from(V('p2')).to(V('p3')).property('since', 2012)
g.addE('knows').from(V('p3')).to(V('p4')).property('since', 2019)
我想提交查询以满足以下两个需求:
inV
的{{1}}朋友 到目前为止,我只能编写以下查询:
p1
查询结果符合预期:
g.E().hasLabel('knows').has('since', gt(2005)).
sideEffect(
V('p1').out().aggregate('friends')).
where(inV().where(within('friends')))
问题是gremlin> g.E().hasLabel('knows').has('since', gt(2005)).
......1> sideEffect(
......2> V('p1').out().aggregate('friends')).
......3> where(inV().where(within('friends')))
==>e[26][p1-knows->p3]
==>e[27][p2-knows->p3]
步骤运行了三次,我只想运行一次。
答案 0 :(得分:2)
sideEffect()
将运行与遍历遍历遍历的次数相同的次数。您可以profile()
遍历以查看发生了什么事:
gremlin> g.E().hasLabel('knows').has('since', gt(2005)).
......1> sideEffect(V('p1').out().aggregate('friends')).
......2> where(inV().where(within('friends'))).profile()
==>Traversal Metrics
Step Count Traversers Time (ms) % Dur
=============================================================================================================
TinkerGraphStep(edge,[~label.eq(knows), since.g... 3 3 0.180 10.58
TraversalSideEffectStep([TinkerGraphStep(vertex... 3 3 0.859 50.48
TinkerGraphStep(vertex,[p1]) 3 3 0.234
VertexStep(OUT,vertex) 6 6 0.134
AggregateStep(friends) 6 6 0.226
TraversalFilterStep([EdgeVertexStep(IN), Profil... 2 2 0.662 38.95
EdgeVertexStep(IN) 3 3 0.036
WherePredicateStep(within([friends])) 0.126
>TOTAL - - 1.702 -
在给定过滤器的情况下, g.E()
会产生3个遍历器,因此sideEffect()
会得到3个遍历器,这意味着该步骤将执行3次。要使其执行一次,只需将3减少到1-可以使用fold()
来完成,它将将三个收集到一个列表中,然后在sideEffect()
之后展开该列表:>
gremlin> g.E().hasLabel('knows').has('since', gt(2005)).
......1> fold().
......2> sideEffect(V('p1').out().aggregate('friends')).
......3> unfold().
......4> where(inV().where(within('friends'))).profile()
==>Traversal Metrics
Step Count Traversers Time (ms) % Dur
=============================================================================================================
TinkerGraphStep(edge,[~label.eq(knows), since.g... 3 3 0.203 18.23
FoldStep 1 1 0.072 6.52
TraversalSideEffectStep([TinkerGraphStep(vertex... 1 1 0.329 29.54
TinkerGraphStep(vertex,[p1]) 1 1 0.075
VertexStep(OUT,vertex) 2 2 0.061
AggregateStep(friends) 2 2 0.076
UnfoldStep 3 3 0.275 24.67
TraversalFilterStep([EdgeVertexStep(IN), Profil... 2 2 0.234 21.04
EdgeVertexStep(IN) 3 3 0.032
WherePredicateStep(within([friends])) 0.066
>TOTAL - - 1.115
所以,我认为这是您问题的直接答案。如果我进行更深入的研究,我想知道您是否有理由说明此处描述的复杂性。如果您想找到代表“ p1”的朋友(“ 2005”之后)的传入边缘的边缘,那么我认为可以通过以下方式实现:
gremlin> g.V('p1').out('knows').inE('knows').has('since',gt(2005))
==>e[5][p1-knows->p3]
==>e[6][p2-knows->p3]