Created
August 19, 2019 23:00
-
-
Save danhyun/7323845b6e8830d9af48d98a7102bcb7 to your computer and use it in GitHub Desktop.
recursively unpack elastcisearch nested aggs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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