Search code examples
gremlinamazon-neptune

How to find parent vertex of two child vertices with condition in GREMLIN?


I have the following graph:

          V1
       /  |  \
      p1  p2  p3

I want to find parent vertex (V1), if I have p1 and p3 vertices matching the condition in GREMLIN. I have around 25 child nodes for a single parent. And I need to find the parent with any of the child vertices matching the condition.

Ex: if P1, P2, P3 vertices has the following properties - name, value. I need to find parent vertex something like:

SELECT 
   V1
WHERE 
       P1.name = 'a' and P1.value = 'b'
   AND P3.name = 'x' and P3.value = 'y'

(or)

SELECT 
   V1
WHERE 
       P2.name = 'p' and P2.value = 'q'
   AND P3.name = 'x' and P3.value = 'y'

Solution

  • The best way to approach this is imperatively (arbitrarily choosing one of the child vertices to start with).

    Here's the simplest form of such a query:

    g.V().has('Child','name','p1').in('HAS').where(out('HAS').has('name','p2'))
    

    The result of that query would be V1.