How to convert cypher to gremlin?

151 Views Asked by At

I am new to gremlin.I need to convert my cypher query to gremlin.

My cypher query:

match(s:Student)-[:STUDIED_AT]-(c:College) 
with s,c 
match(s)-[:LIVES_IN]-(l:Location) 
return s,c,l limit 10

I need to convert this to gremlin. My question is , here in cypher with the help of 'WITH' I was able to reuse student Vertex in later part of query .How do I do that in gremlin?

1

There are 1 best solutions below

0
Kelvin Lawrence On

In Gremlin, there is the as step that can be used to label part of a traversal/query so that you can refer to it later using a select step. However, this is not always needed. For your example the Cypher would end up looking something like the example below.

so

match(s:Student)-[:STUDIED_AT]-(c:College) 
with s,c 
match(s)-[:LIVES_IN]-(l:Location) 
return s,c,l limit 10

might become

g.V().hasLabel('student').as('s').
      both('STUDIED_AT).hasLabel('College').as('c').
      both('LIVES_IN).hasLabel('Location').as('l').
      select(s,c,l).
        by(elementMap()).
      limit(10)

Note that I used both rather than out or in as you did not specify an edge direction in the Cypher query.