Gremlin- how to find paths from A to Z that went t

2019-07-29 10:25发布

问题:

Need some help with Gremlin: If I know the start vertex and the end vertex and there are multiple paths between start and end BUT I have a couple of vertexes along the way. How can I find the correct path based on the data I have?

For instance here I what I have to find the paths from 'college' to 'finch'

g.V().has('station','college').
       repeat(out().simplePath())
        .until(has('station','finch'))
        .path().by('station')

Results

==>[college, wellesley, bloor-yonge, rosedale, summerhill, st. clair, davisville, eglinton, lawrence, york mills, sheppard-yonge, north york centre, finch]
==>[college, dundas, queen, king, union, st. andrew, osgoode, st. patrick, queenspark, museum, st. george, bay, bloor-yonge, rosedale, summerhill, st. clair, davisville, eglinton, lawrence, york mills, sheppard-yonge, north york centre, finch]

But how to i get the correct path that went THROUGH 'dundas' for example?

回答1:

You can use a path-bound counter that you only increment if you find a certain element along the path:

g.withSack(0).V().has('station','college').
  repeat(out().simplePath().
         choose(has('station','dundas'),
                  sack(sum).by(constant(1)))).
    until(has('station','finch')).
  filter(sack().is(1)).
  path().
    by('station')

Adding more necessary points (e.g. filter paths that go through G, H and P) is easy with this approach.

However, if it's only one vertex that has to be part of the path, then sel-fish's answer is another valid option (don't know why it got downvoted).



回答2:

Daniel Kuppitz's answer is much better than mine.

g.V().has('station','college').
    repeat(out().simplePath()).
        until(has('station','finch')).
    path().
    where(unfold().has('station', 'dundas')).
    unfold().values('station').fold()