Search code examples
gremlintinkerpoptinkerpop3gremlin-server

How to get properties from multiple vertexes in gremlin


How to get properties from multiple vertexes in a gremlin.enter image description here

From the above graph, I need to collect 'name' from user and email_id from email vertex and phone number from phone_number vertex. Is it possible to collect all properties in one query?

I tried the below query but it returns email and phone_number as a separate item

g.V().hasLabel('user').as('u').outE('owns')
.inV().hasLabel('email', 'phone_number').as('e', 'p').select('u', 'e', 'p').by(elementMap('name')).by(elementMap()).by(elementMap())

output

{'u': {<T.id: 1>: '124848d5-f387-48eb-bbb7-ad193ef6070a', <T.label: 4>: 'user', 'name': 'Thirumal'}, 'e': {<T.id: 1>: '922f4ac1-9c6d-40fa-bac2-682e0f01673a', <T.label: 4>: 'phone_number', 'phone_number': '+918973697871'}, 'p': {<T.id: 1>: '922f4ac1-9c6d-40fa-bac2-682e0f01673a', <T.label: 4>: 'phone_number', 'phone_number': '+918973697871'}}

{'u': {<T.id: 1>: '124848d5-f387-48eb-bbb7-ad193ef6070a', <T.label: 4>: 'user', 'name': 'Thirumal'}, 'e': {<T.id: 1>: '2c3f90a4-d8bd-4cf5-9ff0-d4e3a6cf1190', <T.label: 4>: 'email', 'email': 'thirumal@enkindletech.com'}, 'p': {<T.id: 1>: '2c3f90a4-d8bd-4cf5-9ff0-d4e3a6cf1190', <T.label: 4>: 'email', 'email': 'thirumal@enkindletech.com'}}

Solution

  • This sort of issues is commonly handled by project():

    g.V().hasLabel('user').
      project('name','email','phone').
        by('name').
        by(out('owns').hasLabel('email').values("email")).
        by(out('owns').hasLabel('phone_number').values('phone_number'))