0

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'
Taylor Riggan
  • 1,963
  • 6
  • 12
Deepak KS
  • 3
  • 2
  • Quick question on this. Will your edges have direction where they will always be going from a parent to a child? And will the parent vertices be labeled as 'parent' and similarly have child vertices labeled as 'child'? – Taylor Riggan Dec 01 '20 at 14:01
  • Yes. I do have 'HAS' edge from parent to children. Parent Vertex has Label of 'Parent' and all Childen vertices has 'Child' label along with properties. – Deepak KS Dec 01 '20 at 15:05

1 Answers1

2

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.

Taylor Riggan
  • 1,963
  • 6
  • 12
  • Thank you Taylor for shift response. Yes, I am currently doing exactly the same way. But just thinking, if this is costly traversal to move to and forth from every child vertex to parent and back. – Deepak KS Dec 02 '20 at 05:46
  • The imperative way is actually very efficient as you are touching the least number of components in the graph as possible. Would be good though to run this against Neptune's /gremlin/profile API endpoint once you get data loaded in to see exactly what the performance characteristics look like. – Taylor Riggan Dec 02 '20 at 19:32