Search code examples
gremlin

Combined Gremlin query that looks up vertices and edges by some attributes or internal IDs


I'd like to run a Gremlin query from Java that looks for nodes and edges by their internal IDs or some other attributes, which can be specified dynamically. It could also be several queries within a transaction - whichever approach is faster. I want it to work for any popular Gremlin database (TinkerPop, JanusGraph, Orient, Neptune).

I have a query that almost does what I need:

g.E().or(
  hasLabel('e1').has('value','abc'),
  hasLabel('e2').has('type','router')).
union(
  identity(),
  V().or(
    hasLabel('v1').has('name','john'),
    hasLabel('v2').id().is(within(123,456))))

The only issue with this query is that querying by edge internal ID does not work: neither id().is('zzz') nor hasId('zzz') works inside or() (but it works without the or()).


Solution

  • Thanks very much for the suggestions. I was able to get it working using "union", which works like "or":

    g.E().union(
      hasId(within('abc','def')),
      has('e1','value','abc')).
    union(
      identity(),
      V().union(
        hasId(within(123,456)),
        has('v1','name',within('john','kevin'))))
    

    It looks like JanusGraph in particular had a problem with the query that I posted in the question.