Search code examples
azureazure-cosmosdbgremlingraph-databasesazure-cosmosdb-gremlinapi

Azure Cosmos db Gremlin elementMap()


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?


Solution

  • 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]