Skip to content

Instantly share code, notes, and snippets.

@danhyun
Created August 19, 2019 23:00
Show Gist options
  • Save danhyun/7323845b6e8830d9af48d98a7102bcb7 to your computer and use it in GitHub Desktop.
Save danhyun/7323845b6e8830d9af48d98a7102bcb7 to your computer and use it in GitHub Desktop.
recursively unpack elastcisearch nested aggs
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment