Search code examples
gremlinamazon-neptune

Gremlin - Using an OR step to get different types of connected vertices


So I have a graph schema where vertex type A can connect inwards to vertex type B or type C in a one to many relationship. I'm trying to write a query that outputs any of those relationships if they exist, for instance a sample output would be:

Type A | Type B | Type C
Sample1A,'', Sample1C
Sample2A, Sample2B, ''
Sample3A, Sample3B, Sample3C
Sample4A, 'Sample4Ba, Sample4Bb', Sample4C

The fourth example is if A is connected to multiple B types. If B and C don't exist, then nothing is output.

So far I have the query: g.V().hasLabel('A').as('A').in('connect').hasLabel('B').as('B').or().in('connect').hasLabel('C').as('C').select('A','B','C')

But this query only returns the A vertices without any B's or C's.

Using AWS Neptune if that matters.


Solution

  • As kevin mentioned in the comment, you can use .project() method for this scenario.

    g.V().hasLabel("A").as("A").project("a","b", "c")
        .by(select("A"))
        .by(choose(in("connect").hasLabel("B").count().is(0), constant("NO_B").value(), in("connect").hasLabel("B")))
        .by(choose(in("connect").hasLabel("C").count().is(0), constant("NO_C").value() , in("connect").hasLabel("C")));