Sunday, 17 May 2026

Incoming Vertex Traversal (inV()): Moving from an Edge to Its Target Vertex

  

For a directed relationship:

a  --->  b

 

·      a is the outgoing (source) vertex

·      b is the incoming (target) vertex

 

The edge points into vertex b. The inV() step always returns the vertex where the edge ends.

 

inV() performs the following:

·      Start at the current edge

·      Move to the incoming (target) vertex

·      Emit that vertex as the traversal result

 

Formally, inV() returns the target vertex of the current edge. inV() can only be used when the traversal is on edges. Each edge produces exactly one vertex

 

 

How inV() Fits with Other Traversal Steps?

Step

Direction

outE()

Vertex outgoing edges    

inE()

Vertex incoming edges    

bothE()

Vertex all incident edges

outV()

Edge source vertex       

inV()

Edge target vertex       

bothV()

Edge both endpoints      

 

Together, these steps allow you to move precisely and intentionally between vertices and edges.

 

Example: An E-Commerce Graph

To make the discussion concrete, consider an e-commerce domain modeled as a property graph. The graph contains:

 

Vertex Types

·      city: Bengaluru, Hyderabad, Chennai, Pune

·      category: Electronics, Books, Clothing

·      product: Laptop, Smartphone, Java Programming Book, T-Shirt

·      customer: Amit Sharma, Priya Iyer, Rohit Verma

·      order: O9001 to O9005

 

Edge Types

·      belongsTo: product category

·      placed: customer order

·      contains: order product

·      deliveredTo: order city

 

Each edge has a clear semantic direction that aligns with real-world meaning.

 

Gremlin Statements to build the Graph

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()

   

From the established domain model:

 

·      order ──contains── product

·      customer ──placed── order

·      order ──deliveredTo── city

·      product ──belongsTo── category

 

This implies:

·      Products have incoming contains edges

·      Orders have incoming placed edges

·      Cities have incoming deliveredTo edges

·      Categories have incoming belongsTo edges

 

Example 1: Orders Placed by a Customer

 

customer ──placed── order

 

g.V().
  has("customer", 'name', 'Rohit Verma').
  outE('placed').
  inV().
  valueMap(true)

gremlin> g.V().
......1>   has("customer", 'name', 'Rohit Verma').
......2>   outE('placed').
......3>   inV().
......4>   valueMap(true)
==>[id:45,label:order,orderId:[O9004]]
==>[id:47,label:order,orderId:[O9005]]

Same can be achieved using outV() step as well, outV is equivalant to outE().inV()

g.V().
  has("customer", 'name', 'Rohit Verma').
  out().
  valueMap(true)

gremlin> g.V().
......1>   has("customer", 'name', 'Rohit Verma').
......2>   out().
......3>   valueMap(true)
==>[id:45,label:order,orderId:[O9004]]
==>[id:47,label:order,orderId:[O9005]]

Example 2: Products Contained in an Order

order ──contains── product

g.V().
  has('order', 'orderId', 'O9005').
  outE('contains').
  inV().
  valueMap(true)

gremlin> g.V().
......1>   has('order', 'orderId', 'O9005').
......2>   outE('contains').
......3>   inV().
......4>   valueMap(true)
==>[id:20,label:product,name:[Java Programming Book],sku:[P2001]]

   

inV() vs outV()

Question

Use

Where does this relationship end? 

inV()

Where did this relationship start?

outV()

 

Think of it as:

·      outV() cause / source

·      inV() effect / target

 

In summary,

·      inV() moves from an edge to its incoming (target) vertex

·      It represents where the relationship ends

·      Valid only when traversing edges

·      Often paired with outE() or inE()

·      Essential for following directionality in Gremlin traversals

 

  

Previous                                                    Next                                                    Home

No comments:

Post a Comment