Gremlin equivalent of an opencypher query

39 Views Asked by At

I have a working opencypher query but due to some reason I have to go with gremlin.

previous post with data

Query

MATCH (:Account {name: 'account.b'})-[:RESULTS_FROM|DEPENDS_ON*]->(n:Account)
RETURN n

I tried to write gremlin equivalent of it but it's not working as expected.

g.V().has('name', 'account.b').hasLabel('Account')
  .repeat(both('RESULTS_FROM', 'DEPENDS_ON').hasLabel('Account')).emit()
  .dedup()

How can I achieve the same behavior using gremlin.

1

There are 1 best solutions below

0
On

Try something like this. As the Cypher query is unbounded by number of hops, a repeat...until should work. I left the dedup in but in your Cypher query I do not see a DISTINCT so this may still give different answers. I changed both to out, as your Cypher query has a direction specifiied.

g.V().has('name', 'account.b').
      hasLabel('Account').
      repeat(out('RESULTS_FROM', 'DEPENDS_ON')).
      until(hasLabel('Account')).
      dedup()

If there are cycles in the graph and you want to avoid visiting the same node twice for a given path, you can add a simplePath() after the out() as follows.

g.V().has('name', 'account.b').
      hasLabel('Account').
      repeat(out('RESULTS_FROM', 'DEPENDS_ON').simplePath()).
      until(hasLabel('Account')).
      dedup()