0

Suppose I have a vertex Employee and a vertex Department. Employee has a property departmentId but there is no edge between these 2 vertices, can I project departmentName along with employeeName??

g.addV('employee').
  property('id', 1).
  property('name', 'A').
  property('departmentId', 1)

g.addV('department').
  property('id', 1).
  property('name', 'HR')
  • 3
    You can do this by searching all the vertices every time or in advance group them together .. but why? the all point in the graph is to avoid exactly that. it seems like a poor design not to connect them with an edge if you have a query that required to "join" the data of both of them – noam621 Jun 17 '20 at 10:30
  • @noam621 I understand your concern regarding the design, but this is just an example to create the scenario. – Kajal Patel Jun 18 '20 at 05:35

1 Answers1

1

I still think this is a bad design, and the performance here will be bad.

g.V().hasLabel('employee').as('e').
  project('name', 'department name').
    by('name').
    by(V().hasLabel('department').
      has('_id', select('e').
        values('departmentId')).values('name'))

example: https://gremlify.com/kudcz61i5j

Maybe this will have better performance:

g.V().hasLabel('department', 'employee').
  group().by(coalesce(
      hasLabel('department').values('_id'),
      hasLabel('employee').values('departmentId')
    )).
    by(fold().as('group').unfold().
      hasLabel('employee').
      project('name', 'department name').
        by('name').
        by(select('group').unfold().
          hasLabel('department').values('name')).
      fold())

example: https://gremlify.com/nndmumlshmo

noam621
  • 2,766
  • 1
  • 16
  • 26