[英]Using Gremlin (AWS Neptune), how can I get all paths of length n from a starting node traversing edges with specific criteria?
Starting with node 1, I want to return all the paths (edges and vertices, with id/label/properties) within n hops following any outbound edges, or following inbound edges with a property predicate (p > 50).从节点 1 开始,我想在任何出站边之后的 n 个跃点内返回所有路径(边和顶点,带有 id/label/properties),或者在带有属性谓词(p > 50)的入站边之后返回。
Ideally the paths shouldn't contain any cycles, so a path shouldn't contain the same node twice.理想情况下,路径不应包含任何循环,因此路径不应包含两次相同的节点。
The property p is not present on every edge.属性 p 并不存在于每条边上。
g.addV().property(id, 1).as('1').
addV().property(id, 2).as('2').
addV().property(id, 3).as('3').
addV().property(id, 4).as('4').
addV().property(id, 5).as('5').
addV().property(id, 6).as('6').
addV().property(id, 7).as('7').
addV().property(id, 8).as('8').
addE('pointsAt').from('1').to('2').
addE('pointsAt').from('3').to('1').
addE('pointsAt').from('4').to('1').property('p', 10).
addE('pointsAt').from('5').to('1').property('p', 100).
addE('pointsAt').from('2').to('6').
addE('pointsAt').from('7').to('2').
addE('pointsAt').from('8').to('2').property('p', 100).
iterate()
Assuming we start at Vertex 1 the paths would look like:假设我们从顶点 1 开始,路径将如下所示:
1>2
1>2>6
1>2>8
1>5
I've experimented with many different approaches and I can't seem to get anything close to what I am looking for.我已经尝试了许多不同的方法,但我似乎无法得到任何接近我正在寻找的东西。
I believe this is what you're looking for:我相信这就是您正在寻找的:
g.V(1).
repeat(
union(
outE(),inE().has('p',gt(50))
).
otherV().simplePath()).
emit().
times(2).
path().
by(valueMap().with(WithOptions.tokens))
The repeat() and times() steps dictate that this is a recursive traversal going to a depth of 2. The union() step and containing arguments follow your requirements to include all outgoing edges and only incoming edges with ap property greater than 50. The emit() step forces the repeat() step to stream all paths as they are found. repeat() 和 times() 步骤表明这是一个深度为 2 的递归遍历。 union() 步骤并包含 arguments 遵循您的要求,包括所有传出边和仅 ap 属性大于 50 的传入边。 emit() 步骤强制对 stream 找到所有路径执行 repeat() 步骤。 If you didn't include this, you would only get the paths found of length 2 (declared in the times() step).
如果你不包括这个,你只会得到长度为 2 的路径(在 times() 步骤中声明)。
To wrap this up, we use path() and by() to output the paths found and all of the ids, labels, and properties for each vertex and edge in the path.总结一下,我们使用 path() 和 by() 到 output 找到的路径以及路径中每个顶点和边的所有 id、标签和属性。
The output of this from the graph that you provided looks like this:您提供的图表中的 output 如下所示:
==>[[id:1,label:vertex],[id:0,label:pointsAt],[id:2,label:vertex]]
==>[[id:1,label:vertex],[id:3,label:pointsAt,p:100],[id:5,label:vertex]]
==>[[id:1,label:vertex],[id:0,label:pointsAt],[id:2,label:vertex],[id:4,label:pointsAt],[id:6,label:vertex]]
==>[[id:1,label:vertex],[id:0,label:pointsAt],[id:2,label:vertex],[id:6,label:pointsAt,p:100],[id:8,label:vertex]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.