Search code examples
gremlinamazon-neptune

Gremlin - extracting T.id and T.label from valueMap


This question is related to this post that Kelvin Lawrence answered very helpfully, wanted to post it as a separate question bec the first question was answered well already.

From this query:

g.V('a2661f57-8aa7-4e5c-9c89-55cf9b7e4cf8').as('self')
.sideEffect(out('rated').store('movies')) 
.out('friended')
.group() 
.by(valueMap(true).by(unfold())) 
.by(out('rated').where(within('movies')).count()) 
.order(local) 
.by(values,desc) 
.unfold()
.select(keys)

i get this result:

1   {<T.id: 1>: 'fdc45bd3-be08-4716-b20f-b4f04987c5e0', <T.label: 4>: 'user', 'username': 'elin102dev', 'name': 'elin obrien', 'avatarUrl': 'public/avatars/fdc45bd3-be08-4716-b20f-b4f04987c5e0.jpg'}
2   {<T.id: 1>: 'bbf1b0db-68cc-41f1-8c7a-5fd77b698e39', <T.label: 4>: 'user', 'username': 'iris', 'name': 'Iris Ebert', 'avatarUrl': 'public/avatars/bbf1b0db-68cc-41f1-8c7a-5fd77b698e39.jpg'}
3   {<T.id: 1>: '34c2ea80-4f84-4652-a7c3-48ce43d9aea7', <T.label: 4>: 'user', 'username': 'iris103dev', 'name': 'iris obrien', 'avatarUrl': 'public/avatars/34c2ea80-4f84-4652-a7c3-48ce43d9aea7.jpg'}

I want to convert the T.id and T.label values in the response to simply "id" and "label". Kelvin, if you're reading this, i i tried appending the following to the query above but it returns 0 results:

.select('id', 'label', 'username', 'name', 'avatarUrl') 
    .by(T.id)
    .by(T.label)
    .by('username')
    .by('name')
    .by('avatarUrl')
    .toList()

I could use a little more help figuring this out, not having much success. Thanks in advance.


Solution

  • It is not possible to do a select(T.id) inside a query. In code if you get a map back you can access the T.id field. For a case such as this, it is better to delay fetching the properties until you finally need them. You might try rewriting the query like this.

    g.V('a2661f57-8aa7-4e5c-9c89-55cf9b7e4cf8').as('self').
      sideEffect(out('rated').store('movies')). 
      out('friended').
      group(). 
        by(). 
        by(out('rated').where(within('movies')).count()). 
      order(local). 
        by(values,desc). 
        unfold().
      select(keys).
      project('id','label','username').
        by(id).
        by(label).
        by('username')