Suggestions to Improve GroupBy

Another Groupby usage.

One thing I find strange about GB’s behavior is that it returns the grouping only that it is not possible to open the values bound to that grouping. Unless of course, they are children of the group or have a reverse edge.

e.g (Dataset in the end):

This query

{
  variableblock(func: type(Person)) @groupby(studied_at) {
    a as count(uid)
  }
  
  data(func: uid(a)){
    uid
    name
    total: val(a)
  }
}

Returns

{
  "data": {
    "variableblock": [
      {
        "@groupby": [
          {
            "studied_at": "0x4e22",
            "count": 2
          },
          {
            "studied_at": "0x4e21",
            "count": 3
          }
        ]
      }
    ],
    "data": [
      {
        "uid": "0x4e21",
        "name": "Main City School",
        "total": 3
      },
      {
        "uid": "0x4e22",
        "name": "Other City School",
        "total": 2
      }
    ]
  }
}

Okay, but I can’t expand the nodes I have in mind that belong to these groups.
The only way to do so is turning studied_at as reverse edge ~studied_at { name } Which is not good, because potentially I would have to put everything as reverse. And that consumes disk space.

So the idea here would be to use expand (val (a)) as the custom block idea intended.

e.g:

{
  var(func: type(Person)) @groupby(studied_at) {
    a as count(uid)
  }
  
  q(func: uid(a)){
    uid
    name
    jobTitle
    total: val(a)
    in_this_group: expand(val(a))
  }
}

And the response would be like

{
	"data": {
		"q": [{
				"uid": "0x4e21",
				"name": "Main City School",
				"total": 3,
				"in_this_group": [{
						"uid": "0x4e23",
						"name": "Jane Doe"
					},
					{
						"uid": "0x4e24",
						"name": "Lucas Doe",
						"jobTitle": "Student"
					},
					{
						"uid": "0x4e25",
						"name": "Olivia Doe",
						"jobTitle": "Student"
					}
				]
			},
			{
				"uid": "0x4e22",
				"name": "Other City School",
				"total": 2,
				"in_this_group": [{
						"uid": "0x4e26",
						"name": "Noah Doe",
						"jobTitle": "Student"
					},
					{
						"uid": "0x4e27",
						"name": "Sophia Doe",
						"jobTitle": "Student"
					}
				]
			}
		]
	}
}

Dataset

{
  "set":[	{
		"uid": "_:School1",
		"dgraph.type": "School",
		"name": "Main City School",
		"telephone": "(425) 123-4567",
		"url": "http://www.maincityschool.com"
	},
 {
		"uid": "_:School2",
		"dgraph.type": "School",
		"name": "Other City School",
		"telephone": "(425) 333-4567",
		"url": "http://www.othercityschool.com"
	},
	{
		"dgraph.type": "Person",
		"name": "Jane Doe",
		"jobTitle": "Student",
		"studied_at": {
			"uid": "_:School1"
		}
	},
	{
		"dgraph.type": "Person",
		"name": "Lucas Doe",
		"jobTitle": "Student",
		"studied_at": {
			"uid": "_:School1"
		}
	},
	{
		"dgraph.type": "Person",
		"name": "Olivia Doe",
		"jobTitle": "Student",
		"studied_at": {
			"uid": "_:School1"
		}
	},
	{
		"dgraph.type": "Person",
		"name": "Noah Doe",
		"jobTitle": "Student",
		"studied_at": {
			"uid": "_:School2"
		}
	},
	{
		"dgraph.type": "Person",
		"name": "Sophia Doe",
		"jobTitle": "Student",
		"studied_at": {
			"uid": "_:School2"
		}
	}
]
}
1 Like