Monday, 11 May 2026

Testing Property Existence in Gremlin using has() and hasNot()

  

In real-world graph models, not every vertex or edge is guaranteed to have the same set of properties. Some properties may be optional, populated only under certain conditions, or introduced later as the schema evolves. As a result, a common requirement when writing Gremlin traversals is to determine whether a specific property exists or not.

 

Apache TinkerPop addresses this need through the has() and hasNot() steps. These steps allow a traversal to include or exclude elements based purely on the presence or absence of a property, making them essential tools for writing robust and defensive graph queries.

 

Importantly, these steps apply uniformly to both vertex properties and edge properties, reinforcing Gremlin’s consistency across graph elements.

 

Creating Sample Student Vertices for Demonstration

To illustrate has() and hasNot() more concretely, consider a simple academic domain. The following example creates a small graph of student vertices using TinkerGraph, which is well suited for experimentation and learning.

graph = TinkerGraph.open()
g = graph.traversal()

g.addV('student').
  property('name', 'Rahul').
  property('city', 'Hyderabad').
  property('course', 'Computer Science').
  property('year', 3).
  property('gpa', 8.6).
  property('age', 21)

g.addV('student').
  property('name', 'Sneha').
  property('city', 'Pune').
  property('course', 'Mechanical Engineering').
  property('year', 2).
  property('gpa', 7.9).
  property('hobbies', 'Cricket')

g.addV('student').
  property('name', 'Amit').
  property('city', 'Bengaluru').
  property('course', 'Data Science').
  property('year', 4).
  property('gpa', 9.1)

   

Verify the student vertices

 

g.V().
  hasLabel('student').
  valueMap(true)

gremlin> g.V().
......1>   hasLabel('student').
......2>   valueMap(true)
==>[id:0,label:student,city:[Hyderabad],year:[3],name:[Rahul],course:[Computer Science],gpa:[8.6],age:[21]]
==>[id:7,label:student,city:[Pune],year:[2],hobbies:[Cricket],name:[Sneha],course:[Mechanical Engineering],gpa:[7.9]]
==>[id:14,label:student,city:[Bengaluru],year:[4],name:[Amit],course:[Data Science],gpa:[9.1]]

   

Demonstrating has() with Student Data

Get all the student with the property 'name'

g.V().
  has('name').
  values('name')

gremlin> g.V().
......1>   has('name').
......2>   values('name')
==>Rahul
==>Sneha
==>Amit

Get all the students names, who has the property 'age'

g.V().
  has('age').
  values('name')

gremlin> g.V().
......1>   has('age').
......2>   values('name')
==>Rahul

   

Demonstrating hasNot() with Student Data

Now consider a scenario where some students do not have a 'age' property. To find students who do not have an age property:

 

g.V().
  hasLabel('student').
  hasNot('age').
  values('name')

gremlin> g.V().
......1>   hasLabel('student').
......2>   hasNot('age').
......3>   values('name')
==>Sneha
==>Amit

hasNot() is a readable shorthand for not(has(propertyKey)).

g.V().
  hasLabel('student').
  not(has('age')).
  values('name')

gremlin> g.V().
......1>   hasLabel('student').
......2>   not(has('age')).
......3>   values('name')
==>Sneha
==>Amit

Combining Property Existence with Value Constraints

Property existence checks are often combined with value based filters to express richer intent.

 

For example, to find students whose name is Sneha.

g.V().
  hasLabel('student').
  has('name', 'Sneha').
  valueMap(true)

gremlin> g.V().
......1>   hasLabel('student').
......2>   has('name', 'Sneha').
......3>   valueMap(true)
==>[id:7,label:student,city:[Pune],year:[2],hobbies:[Cricket],name:[Sneha],course:[Mechanical Engineering],gpa:[7.9]]

   

In summary,

·      has(propertyKey) checks for existence, not value.

·      hasNot(propertyKey) excludes elements where the property is defined.

·      hasNot() is a readable shorthand for not(has(propertyKey)).

·      Property existence checks are critical in schema-flexible graph models.

 

By mastering has() and hasNot(), readers gain precise control over how traversals interpret incomplete or evolving graph data, an essential skill for building reliable Gremlin-based applications.

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment