In earlier sections, steps such as outV(), inV(), and bothV() were explained by explicitly following edge direction. While this is precise, many real-world questions are simpler, from this entity, give me the entity on the other side of the relationship.
This is especially common in business graphs, such as an e-commerce domain, where relationships are meaningful primarily because they connect two entities. The Gremlin step that answers this question is otherV().
otherV() returns the vertex at the opposite end of an edge, excluding the vertex from which the traversal arrived at that edge. It is context-aware, the result depends on where the traversal came from.
Example: E-Commerce example
graph = TinkerGraph.open() g = graph.traversal() blr = g.addV('city').property('name', 'Bengaluru').next() hyd = g.addV('city').property('name', 'Hyderabad').next() chn = g.addV('city').property('name', 'Chennai').next() pne = g.addV('city').property('name', 'Pune').next() catElectronics = g.addV('category').property('name', 'Electronics').next() catBooks = g.addV('category').property('name', 'Books').next() catClothing = g.addV('category').property('name', 'Clothing').next() p1 = g.addV('product').property('sku', 'P1001').property('name', 'Laptop').next() p2 = g.addV('product').property('sku', 'P1002').property('name', 'Smartphone').next() p3 = g.addV('product').property('sku', 'P2001').property('name', 'Java Programming Book').next() p4 = g.addV('product').property('sku', 'P3001').property('name', 'T-Shirt').next() g.V(p1).addE('belongsTo').to(catElectronics).next() g.V(p2).addE('belongsTo').to(catElectronics).next() g.V(p3).addE('belongsTo').to(catBooks).next() g.V(p4).addE('belongsTo').to(catClothing).next() c1 = g.addV('customer').property('customerId', 'C101').property('name', 'Amit Sharma').next() c2 = g.addV('customer').property('customerId', 'C102').property('name', 'Priya Iyer').next() c3 = g.addV('customer').property('customerId', 'C103').property('name', 'Rohit Verma').next() o1 = g.addV('order').property('orderId', 'O9001').next() o2 = g.addV('order').property('orderId', 'O9002').next() o3 = g.addV('order').property('orderId', 'O9003').next() o4 = g.addV('order').property('orderId', 'O9004').next() o5 = g.addV('order').property('orderId', 'O9005').next() g.V(c1).addE('placed').to(o1).next() g.V(c1).addE('placed').to(o2).next() g.V(c2).addE('placed').to(o3).next() g.V(c3).addE('placed').to(o4).next() g.V(c3).addE('placed').to(o5).next() g.V(o1).addE('contains').to(p1).next() g.V(o1).addE('contains').to(p3).next() g.V(o2).addE('contains').to(p2).next() g.V(o3).addE('contains').to(p4).next() g.V(o4).addE('contains').to(p1).next() g.V(o4).addE('contains').to(p4).next() g.V(o5).addE('contains').to(p3).next() g.V(o1).addE('deliveredTo').to(blr).next() g.V(o2).addE('deliveredTo').to(blr).next() g.V(o3).addE('deliveredTo').to(hyd).next() g.V(o4).addE('deliveredTo').to(chn).next() g.V(o5).addE('deliveredTo').to(pne).next()
Example 1: Which products are part of order O9001?
g.V(). has('order', 'orderId', 'O9001'). outE('contains'). otherV(). valueMap(true)
gremlin> g.V().has('order', 'orderId', 'O9001').outE('contains').otherV().valueMap(true) ==>[id:14,label:product,name:[Laptop],sku:[P1001]] ==>[id:20,label:product,name:[Java Programming Book],sku:[P2001]]
Example 2: From Customer to Orders
What orders were placed by customer Amit Sharma?
g.V(). has('customer', 'customerId', 'C101'). outE('placed'). otherV(). valueMap(true)
gremlin> g.V().has('customer', 'customerId', 'C101').outE('placed').otherV().valueMap(true) ==>[id:39,label:order,orderId:[O9001]] ==>[id:41,label:order,orderId:[O9002]]
Example 3: From Order to Delivery City
Where was order O9001 delivered?
g.V(). has('order', 'orderId', 'O9001'). outE('deliveredTo'). otherV(). valueMap(true)
gremlin> g.V().has('order', 'orderId', 'O9001').outE('deliveredTo').otherV().valueMap(true) ==>[id:0,label:city,name:[Bengaluru]]
Even though the e-commerce graph uses directed edges, otherV() behaves symmetrically:
· It works whether the edge is incoming or outgoing
· It always excludes the vertex you came from
This makes it ideal for:
· Neighbor discovery
· Business queries phrased as connections
· Cleaner, intention-revealing traversals
How otherV() Differs from Similar Steps?
|
Step |
Returns |
|
outV() |
Source vertex of the edge |
|
inV() |
Target vertex of the edge |
|
bothV() |
Both endpoints (including the current vertex) |
|
otherV() |
Only the opposite endpoint |
In summary,
· otherV() returns the vertex on the opposite side of an edge
· It depends on traversal context, not edge direction
· It avoids repeating the starting vertex
· Particularly well suited for business-domain graphs like e-commerce
· Often clearer and safer than bothV() for real queries
Previous Next Home
No comments:
Post a Comment