Search code examples
gremlinamazon-neptune

Gremlin: is it possible to filter on optional properties? (graph database is Neptune)


I have a graph with a single vertex:

gremlin> g.V().valueMap(true)

==>{id=a, x=[foo], label=vertex}

The vertex can be found in the following query:

gremlin> g.V().has('x', 'foo')

==>v[a]

However, I would like to modify the above query with the additional match constraint: match the vertex if it does not have property "y", or if the vertex does have property "y" and the value for property "y" equals "bar".

I have constructed the following query.

g.V().has('x', 'foo').or(__.hasNot('y'), __.has('y', 'bar'))

The query returns no matching vertices. So, I think I am looking for something equivalent to "IFNULL()" in mysql.

Any advice is much appreciated!

Joel


Solution

  • There's nothing wrong with your or() filter, it should just work.

    gremlin> g = TinkerGraph.open().traversal()
    ==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
    gremlin> g.addV().property(id, 'a').property('x', 'foo').iterate()
    gremlin> g.V().has('x', 'foo').or(__.hasNot('y'), __.has('y', 'bar'))
    ==>v[a]
    

    Alternatively you can check that there's no y value that is not bar.

    gremlin> g.V().has('x', 'foo').not(__.values('y').is(neq('bar')))
    ==>v[a]
    

    However, double negations tend to be confusing, so I would just go with or().