gremlingremlin-serverazure-cosmosdb-gremlinapigremlinpython

How to check for a property in a vertex or its child using gremlin-cosmos


I am traversing a path a,b,c,d in my graph where a,b,c,d are the nodes along the traversed path.

(a)->(b)->(c)->(d)

My traversal will end at node d as node d is a leaf node.

I want my traversal to cover all nodes from a to d and it must not come to a halt before it reaches node d.

The goal of the traversal is to find out if a certain property exists in node c or d. If for example the property does not exit in node c, I'd like to check if it does exist in its child node d first before the path is rendered invalid.

How do I achieve this with gremlin query on cosmos? Is there such a thing as an optional has() in Gremlin?

Nodes c and d are labeled differently however they have the same properties.

Currently, I have this query, but it would fail if node c didn't have the property of interest even though node d might have it.

G.V().hasLabel('a').out().as('b').out().as('c').has('status', 'patched').out().as('d').has('status', 'patched')

If either node c or node d have the property 'status': 'patched' is good enough and the path is valid (aka. I want that path in my result to the query)

I am doing this on CosmosDB, so please only provide answers using supported steps found here: https://learn.microsoft.com/en-us/azure/cosmos-db/gremlin/support


Solution

  • The goal of the traversal is to find out if a certain property exists in node c or d.

    This sounds like an or() step:

    G.V().hasLabel('a').out().out().or(
        __.has('status', 'patched').out(),
        __.out().has('status', 'patched')
      ).path()