Recursive queries + group by & aggregation

Hi.

I believe there could be many hierarchy driven use cases for Dgraph. Directory → subdirectory → files or assembly → subassembly → machine part etc.

How could I traverse ('downwards '/ ‘upwards’) e.g a file system data and e.g. summarise total file size by file type extension to answer questions like: what are the 5 most space consuming file types in a given directory + subdirectories? How many files there are by file type?

The same analogy for machine parts could be e.g. a summary of weight or price or count by part type. In hierarchy each object would have exactly a one parent (expect a root), circular references would not be a problem.

Kind regards,

Juki

If you have an edge from each file, <fileid> Instance "File" ., and another edge <fileid> Type "PDF" ., then you could do something like this.

{
var(func: eq(Instance, "File")) @groupby(Type) {
  sz as sum(Size)
}

mostSpaceConsuming(func: uid(sz), orderdesc: val(sz), first: 5) {
  uid(sz)
  val(sz)
}
}

This is approximately how it might work. @ashwin95r, @pawan: Could you confirm this?

The groupby cannot be done recursively as of now.

But assuming you have different file types in a folder, the query above is almost right.
But the modelling has to be this way

<fileid> <Type> <1> . # this has to be a uid node for aggegation to work.
<1> <name> "PDF" .
<fileid> <Size> "1000" . 
{
var(func: eq(Instance, "File")) @groupby(Type) {
  sz as sum(Size)
}

mostSpaceConsuming(func: uid(sz), orderdesc: val(sz), first: 5) {
  name
  val(sz)
}
}

There’s no recursion in the query I mentioned. When asking for Instance "File", it’s just picking up all files. We can then filter them by a particular directory etc.

The Type edge is "PDF", and the Instance edge is for all things which are files, as opposed to directories.

So, the biggest change here is to have each file type be a node in the graph, as opposed to a string directly.

Oh yeah, not the query you specified, I was just stating that as the topic said Recursive queries + group by & aggregation.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.