let unpack = (a) => Object.entries(a)
    .filter(([key, value]) => value.buckets)
    .flatMap(([key, value]) => value.buckets
        .flatMap(b => {
            let current = {
                [key] : b.key, 
                doc_count: b.doc_count
            }
            let unpacked = unpack(b)
            if (unpacked.length == 0) {
                return [current]
            } else {
                return unpacked.flat().map((o) => Object.assign({}, current, o))
            }

        }))
    .filter(o => o.doc_count > 0)

let flat = unpack(aggs)