Search code examples
gremlintinkerpop

Gremlin : Checking existence in traversal step or set


Background:

I am trying to run a search through a graph and change the search behavior based on if the discovered node belongs to a pre-defined set available before the query is executed.

I can't find any documentation on how to cross-check if a vertex or attribute is present in a set in gremlin.

Examples:

Given: g.V('id1', 'id2', 'id3').as('a')

Find: if a includes 'id4'

OR

Given: g.V('id1', 'id2', 'id3').fold().store('a') => ['id1', 'id2', 'id3']

Find: if a contains id4


Solution

  • One way to approach this is to use the coalesce step. Here is a simple example from the Gremlin Console.

    gremlin> g.V(1,2,3).fold().as('found').V(3).coalesce(where(within('found')).constant('found'),constant('not found'))
    ==>found
    
    gremlin> g.V(1,2,3).fold().as('found').V(4).coalesce(where(within('found')).constant('found'),constant('not found'))
    ==>not found