g.addV().property('type','student').property('id','student1').as('s1').
addV().property('type','student').property('id','student2').as('s2').
addV().property('type','student').property('id','student3').as('s3').
addV().property('type','student').property('id','student4').as('s4').
addV().property('type','course').property('id','course1').as('c1').
addV().property('type','course').property('id','course2').as('c2').
addV().property('type','course').property('id','course3').as('c3').
addV().property('type','teacher').property('id','teacher1').as('t1').
addE('takesCourse').from('s1').to('c1').
addE('takesCourse').from('s2').to('c2').
addE('takesCourse').from('s3').to('c3').
addE('teacherOf').from('t1').to('c1').
addE('teacherOf').from('t1').to('c2')
g.V().match(__.as('X').has('type','student'),
__.as('X').out('takesCourse').as('Y'),
__.as('Y').in('teacherOf').as('Z')).select('X','Y','Z')
Output
==>[X:v[0],Y:v[12],Z:v[21]]
==>[X:v[3],Y:v[15],Z:v[21]]
Expected Output:
==>[X:v[0],Y:v[12],Z:v[21]]
==>[X:v[3],Y:v[15],Z:v[21]]
==>[X:v[6],Y:N/A,Z:N/A]
==>[X:v[9],Y:N/A,Z:N/A]
It should print
1. All X that matches pattern 1.
2. Y and Z if both of them matches pattern 2 and 3, otherwise print N/A for both of them.
Query 2:(Your approach)
g.V().match(__.as('X').has('type','student'),
__.as('X').coalesce(out('takesCourse'), constant("N/A")).as('Y'),
__.as('Y').choose(__.is("N/A"),constant("N/A"),coalesce(__.in('teacherOf'), constant("N/A"))).as('Z')).select('X','Y','Z')
Output
==>[X:v[0],Y:v[12],Z:v[21]]
==>[X:v[3],Y:v[15],Z:v[21]]
==>[X:v[6],Y:v[18],Z:N/A]
==>[X:v[9],Y:N/A,Z:N/A]
How to modify the query to obtain the output as expected?