1

我有一段(损坏的)gremlin 代码来生成从给定顶点到具有参数的最短路径test_parameter。如果在边上找不到该参数,则不应返回任何路径。

s.V(377524408).repeat(bothE().has('date', between(1554076800, 1556668800)).otherV()) /* date filter on edges */
    .until(or(__.bothE().has('test_property', gt(0)),
           loops().is(4)))                                /* broken logic! */
    .path()                                             
    .local(unfold().filter(__.has('entity_id')).fold())   /* remove edges from output paths*/

断线的是.until(or(__.outE().has('test_property', gt(0)), loops().is(4)))

目前——而且它的原因是有道理的——它给出了距离起始顶点 4 跳的所有路径。

我正在尝试对其进行调整,以便如果遍历是 4 次迭代,并且如果找不到该属性test_property那么它不应该返回任何路径。如果test_property找到,它应该只返回该顶点的路径。

我试图在times(4)条件中加入约束并删除loops()条件,但不知道如何同时拥有times(4)this 和.has('test_property', gt(0))约束。

4

2 回答 2

4

丹尼尔的回答几乎没有问题(见评论)。此查询返回正确的结果:

g.V(377524408)
  .repeat(bothE().has('date', between(1554076800, 1556668800)).otherV().simplePath().as("v"))
  .until(and(bothE().has('tp', gt(0)), loops().is(lte(4))))
  .select(all, "v")
  .limit(1)

simplePath()是必需的,因此我们不会来回走动并避免循环。

重复循环直到满足条件并且我们还没有达到最大跳数。

仅返回第limit(1)一个(最短)路径。省略获取所有路径。

请注意,如果图形是有向的,最好使用outE()而不是bothE().

于 2019-10-12T11:23:10.580 回答
0

这应该有效:

s.V(377524408).
  repeat(bothE().has('date', between(1554076800, 1556668800)).otherV().as('v')).
    times(4).
  filter(bothE().has('test_property', gt(0))).
  select(all, 'v')

另请注意,我用local(unfold().filter(__.has('entity_id')).fold())更简单的东西替换了你的东西(假设唯一的目的是从路径中移除边缘)。

于 2019-10-11T18:10:51.857 回答