gremlinamazon-neptunegraph-notebookopencypher

openCypher equivalent of Gremlin repeat...until loop


Using the air-routes data set, the following Gremlin query will find five routes between Austin (AUS) and Wellington (WLG).

g.V().has('code','AUS').
  repeat(out('route').simplePath()).
  until(has('code','WLG')).
  limit(5).
  path().
    by('code')

which returns the paths (routes) with each airport code displayed:

1   path[AUS, DFW, SYD, WLG]
2   path[AUS, IAH, SYD, WLG]
3   path[AUS, IAH, AKL, WLG]
4   path[AUS, LAX, SYD, WLG]
5   path[AUS, LAX, MEL, WLG]

In openCypher a similar query can be written, along the lines of

MATCH p=(a:airport {code: 'AUS'})-[:route*]->(w:airport {code: 'WLG'})
RETURN p
LIMIT 5

But this returns all of the properties for the entire path (nodes and edges). Is there a simple way to get a result back that resembles the output from the Gremlin query?

The queries were run using Amazon Neptune which allows Gremlin and openCypher queries over the same data, and using the graph-notebook notebooks.


Solution

  • The openCypher query can be modified to use "list comprehension" as follows

    MATCH p=(a:airport {code: 'AUS'})-[:route*]->(w:airport {code: 'WLG'})
    RETURN [apt in nodes(p) | apt.code] as route
    LIMIT 5
    

    which returns

    #   route
    1   ['AUS', 'DFW', 'SYD', 'WLG']
    2   ['AUS', 'LAX', 'SYD', 'WLG']
    3   ['AUS', 'IAH', 'SYD', 'WLG']
    4   ['AUS', 'LAX', 'AKL', 'WLG']
    5   ['AUS', 'IAH', 'AKL', 'WLG']
    

    These work in a similar way to list comprehension in Python. For each node, in each path, the variable apt is assigned. From there we can just extract the airport code using apt.code.