2

Im trying to create a gremlin query in cosmos db where the properties of all vertices are flattened.

The best i have achieved is using "valueMap"

Query

g.V('12345').valueMap(true))

Result

{
   "id": "12345",
   "label": "product",
   "name": [
     "product name"
   ],
   "description": [
     "productdescription"
   ],
}

What i am trying to achieve

{
   "id": "12345",
   "label": "product",
   "name": "product name",
   "description": "productdescription"
}

It looks like elementMap is the right way to go, but it doesnt seem to be supported in Cosmos Db.

Is there a reason why this is not supported or does a similar solution exist?

1 Answers1

7

CosmosDB tends to be a bit behind in supporting all aspects of the Gremlin language. There are workarounds. Prior to elementMap() the typical pattern was to use a by() modulator to valueMap() to unfold() the lists:

gremlin> g.V().valueMap(true).by(unfold())
==>[id:1,label:person,name:marko,age:29]
==>[id:2,label:person,name:vadas,age:27]
==>[id:3,label:software,name:lop,lang:java]
==>[id:4,label:person,name:josh,age:32]
==>[id:5,label:software,name:ripple,lang:java]
==>[id:6,label:person,name:peter,age:35]

I don't know if CosmosDB supports that particular by() modulator though. If it does not then it gets a bit ugly:

gremlin> g.V().map(valueMap(true).unfold().group().by(keys).by(select(values).unfold()))
==>[id:1,label:person,name:marko,age:29]
==>[id:2,label:person,name:vadas,age:27]
==>[id:3,label:software,name:lop,lang:java]
==>[id:4,label:person,name:josh,age:32]
==>[id:5,label:software,name:ripple,lang:java]
==>[id:6,label:person,name:peter,age:35]

or perhaps:

gremlin> g.V().map(valueMap(true).unfold().group().by(keys).by(select(values).limit(local,1)))
==>[id:1,label:person,name:marko,age:29]
==>[id:2,label:person,name:vadas,age:27]
==>[id:3,label:software,name:lop,lang:java]
==>[id:4,label:person,name:josh,age:32]
==>[id:5,label:software,name:ripple,lang:java]
==>[id:6,label:person,name:peter,age:35]
stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • Thank you for your answer Stephen! Yes it seems like CosmosDb is missing some features compared to the current version of Gremlin. I tried both queries and the first one gave "Gremlin op does not support by(traversal)" as expected. The second query gave a "GraphSyntaxException" which seems to be affected by the last "unfold()", as when i remove the unfold i get a similar result as before. – Daniel Gustafsson Jun 26 '20 at 12:10
  • Will it let you use `limit(1)` instead? – stephen mallette Jun 26 '20 at 13:33
  • 1
    The 3rd solution returned the output i was looking for. Thank you very much Stephen, and have a great day! – Daniel Gustafsson Jun 26 '20 at 14:08
  • Thanks for the help on this one, #3 also did the trick working with Cosmos! – Matt Sanders Apr 21 '22 at 23:38