Search code examples
gremlingremlinpythontinkerpop

Add count of outgoing edges to a vertice's elementMap


using the airport dataset, I want to return a full elementMap of every airport - plus the count of incoming "route" edges.

What I have right now looks like this (Link to the example: https://gremlify.com/z05d54wyt99)

g.V().hasLabel('airport').
    project('airport', 'count').
        by(elementMap()).
        by(inE('route').count())

This however returns the object and the count separately:

{
  "airport": {
    "id": 5763,
    "label": "airport",
    "code": "AUS"
  },
  "count": 1
}

Instead, what I am looking for looks like this:

{
  "id": 5763,
  "label": "airport",
  "code": "AUS",
  "count": 1
}

I know I could use project to specify every property individually, but is there a better way to do this?

Thanks in advance!


Solution

  • Here is one way it can be done. This uses a common Gremlin pattern for merging two maps into one. There are other ways to do this as well. I used a limit(2) just to have two results for the example. In some ways I prefer your original query as it is very simple and you could just extract the results in an application quite easily.

    gremlin> g.V().hasLabel('airport').
    ......1>       limit(2).
    ......2>       project('results').
    ......3>         by(union(elementMap(),project('count').by(inE('route').count())).fold()).
    ......4>       local(
    ......5>         select('results').unfold().unfold().
    ......6>         group().
    ......7>           by(keys).
    ......8>           by(select(values)))  
     
    ==>[country:US,code:ATL,longest:12390,city:Atlanta,count:242,lon:-84.4281005859375,type:airport,label:airport,elev:1026,id:1,icao:K
    ATL,region:US-GA,runways:5,lat:33.6366996765137,desc:Hartsfield - Jackson Atlanta International Airport]
    ==>[country:US,code:ANC,longest:12400,city:Anchorage,count:40,lon:-149.996002197266,type:airport,label:airport,elev:151,id:2,icao:P
    ANC,region:US-AK,runways:3,lat:61.1744003295898,desc:Anchorage Ted Stevens]