Search code examples
consolegremlintinkerpop

how can i write the query on gremlin console to return the pair vertices these have the parallel edge?


I like to transform this cypher query to gremlin.

(n:Person)-[:friend]->(t:Person)-[:friend]->(n:Person)

Thanks


Solution

  • Using the air-routes data set, one way to do this is to use the cyclicPath step as follows.

    gremlin> g.V('44').outE().inV().outE().inV().cyclicPath().path()
    ==>[v[44],e[5019][44-route->8],v[8],e[3975][8-route->44],v[44]]
    ==>[v[44],e[5020][44-route->13],v[13],e[4158][13-route->44],v[44]]
    ==>[v[44],e[5021][44-route->20],v[20],e[4387][20-route->44],v[44]]
    
    
    gremlin> g.V('44').outE().inV().outE().inV().cyclicPath().path().by('code').by()
    ==>[SAF,e[5019][44-route->8],DFW,e[3975][8-route->44],SAF]
    ==>[SAF,e[5020][44-route->13],LAX,e[4158][13-route->44],SAF]
    ==>[SAF,e[5021][44-route->20],PHX,e[4387][20-route->44],SAF]
    ==>[SAF,e[5022][44-route->31],DEN,e[4736][31-route->44],SAF]
    ==>[v[44],e[5022][44-route->31],v[31],e[4736][31-route->44],v[44]]
       
    

    Or if you just want the edge IDs

    gremlin> g.V('44').outE().inV().outE().inV().cyclicPath().path().by('code').by(id)
    ==>[SAF,5019,DFW,3975,SAF]
    ==>[SAF,5020,LAX,4158,SAF]
    ==>[SAF,5021,PHX,4387,SAF]
    ==>[SAF,5022,DEN,4736,SAF]  
    

    Another way to write this query involves a where step

    gremlin> g.V('44').as('a').outE().inV().outE().inV().where(eq('a')).path().by('code').by()
    ==>[SAF,e[5019][44-route->8],DFW,e[3975][8-route->44],SAF]
    ==>[SAF,e[5020][44-route->13],LAX,e[4158][13-route->44],SAF]
    ==>[SAF,e[5021][44-route->20],PHX,e[4387][20-route->44],SAF]
    ==>[SAF,e[5022][44-route->31],DEN,e[4736][31-route->44],SAF]