Transform gremlin to cyper query

Hi All,
I am very new in cypher language, can someone help me to translate the following gremlin queries to cypher query.

  1. g.V('v1').outE().valueMap().count() //Count all the out going path from given vertex.
  2. g.V('v2').repeat(out()).times(2).count() // Count 2 hops paths from given vertex.
  3. g.V().has("value", "abc").path(). //Print all the paths from given vertex.
  4. g.V().has("value", "xyz").path().by("value") //Print all the paths from given vertex along with vertex attributes value.
  5. g.V().has("value", "klm").inE().outV().path() //Print all the paths of out going vertices of all the incoming vertices of a given vertex
  6. g.V().has("value", "pqr").outE().inV().path() //Print all the paths of incoming vertices of all the out going vertices of a given vertex.
    7.g.V().has('CID','value','lmn').values() //Print all the attributes of a given vertex in label CID.


What have you tried so far?

1 Like

Hello Ramdabam,
For the following, we will be using on the default graph so please paste and run the queries on the botton textbox.
Generically, in Cypher terms:

  • 1 a vertex ( is a node so for outE(), in Cypher you can count all outgoing relationships (edges in Gremlin) (and also incoming) by specifying the direction of the relationship by using ">" (or "<"). It will become more obvious where to use the ">" in the following examples.
    "match (n:Crew{name:'Neo'})-[r]->() return n, count(r)"
    • "match" is the instruction to parse the graph
    • "(n:Crew{name:'Neo'})" is the starting node searched by label "Crew" and by property called "name" with value "Neo"
    • -[r]->* is the outgoing relationship. notice the direction: ">" for outgoing and "<-[r]-" for incoming
    • () means any node, as means any relationship
  • 2 repeat does not exist but you can instruct cypher to walk past a variable number of relationships
    "match (n:Crew{name:'Neo'})-[r:KNOWS2..2]-(m) return n as n,r,m"
    Notice the 2..2, where "
    " gives the instruction for a variable length, first 2 is a minimum of path length and second 2 is the maximum length of path
  • 3 all paths from current node is just letting the path length be free :)) and for "has("value", "abc")" you either use "{name:'Neo'}" or the "where" clause for match , so "match (n:Crew{name:'Neo'})-[r:KNOWS*..]-(m) return n as n,r,m"
  • 4 all vertex attribute values: well, you can name your path and return everything in it: "match path=(n:Crew{name:'Neo'})-[r:KNOWS*0..]-(m) return n,r,m, path"
  • 4 displaying a property of all nodes in path a little more tricky, as a traversal in Gremlin is not how things work in Cypher: whereas in Gremnlin you wall a path, in Cyher you get the path, so you need to iterate the path afterwards "match path=(n:Crew{name:'Neo'})-[r:KNOWS*0..]-(m) with nodes(path) as noduri unwind noduri as nod return"
  • 5 notice the direction of the relationship as "<" into node n. by using it means you don't care about the relationships and also don't name them as you don't need to return name. but, the following query does this: for all nodes, first match the incoming nodes and then bring the outgoing nodes of those "match (n)<--(m)-->(p) return n,m,p"
  • 6 is the vice-versa of 5 - do notice the location of "<" and ">" : "match (n)-->(m)<--(p) return n,m,p"
  • 7 well, as said above, for label we use ":label" and for attributes, just the .property . So it's the first query, "match (n:Crew{name:'Trinity'}) return n" where "Crew" is the label and "Trinity" is the value of attribute "name"