Knowledge sharing --> Below query traverse from A(node)<--W(edge)<--B(node)-->X(edge) --> C(node)-->Y(edge)--> D(node)<--Z(edge)<--E(node)
g.V().hasLabel("A").as("A").
inE("W").as("W").
outV().
hasLabel("B").as("B").
outE("X").as("X").
inV().
hasLabel("C").as("C").
outE("Y").
has("Y propery","10000").as("Y").
inV().
hasLabel("D").as("D").
inE("Z").as("Z").
outV().
hasLabel("E").as("E").
select("A","W","B","X","C","Y","Y","D","Z","E")
.by(valueMap()).toList()
So a few things with this query...
First, labels should be used as lower cardinality groups of nodes and edges in your graph. If you're coming from a relational database perspective, think of labels as table names (i.e. Customers, Products, Movies, Actors, etc.). If your plan is to look up an individual node or edge, you should use unique ID values for each node and edge.
Second, we try to ask that users asking for Gremlin query help provide a sample graph so that someone stumbling upon this question in the future has something that can import into their own Gremlin Server, Gremlify, or otherwise to validate the result of the question. For this particular question, you could use something like:
Now with this graph, if we wanted to return the full path from A to E, we could do:
That would return the entire path traversed by the query and an elementMap() of all nodes and edges crossed.
If you just wanted to traverse from node 'A' to the last leaf node, you could just do something like:
If you're running these queries in the Gremlin console, you don't need to end with a
toList()ornext(). But if using any of the Gremlin clients within code, you'll need to use those as they are Terminal Steps required to send the queries to the connected TinkerPop-enabled server.A good resource to use to learn Gremlin is here: https://www.kelvinlawrence.net/book/PracticalGremlin.html