0

I am using neptune graph database. Need help writing a query with a group by clause.

We have three nodes

CAMPAIGN → AD → LP

Campaign to Ad is 1 to many mapping AD to LP is 1 to 1 mapping

Suppose we have the following vertices and associations

C1 → A1 → LP1
C1 → A2 → LP1
C2 → A3 → LP1
C2 → A4 → LP1
C3 → A5 → LP2

I want my result set to look like this.

[
   {
        "lp": "LP1",
        "campaigns": [
                {aId: "A1", cId: "C1"},
                {aId: "A2", cId: "C1"},
                {aId: "A3", cId: "C2"},
                {aId: "A4", cId: "C2"}
         ]
   },
   {
        "lp": "LP2",
        "campaigns": [
            {aId: "A5", cId: "C3"}
        ]
   }
]

Any help would be greatly appreciated.

1 Answers1

1

Something like this should work, group by the LPs and then by the paths from them.

g.V().hasLabel('LP').
      group().
        by(id).
        by(__.in().in().path().by('aId').by('cId').fold())

Note that if possible, use the edge labels for the in() steps. This will help the query planner only pick relevant edges to traverse over.

Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38