There are two problems with your request. For starters, you have nested the three aggregations inside each other. And as the error already suggests: a cardinality aggregation cannot accept sub-aggregations. All three aggregations need to be at the same level.
Furthermore, the bucket_script aggregation can only be nested inside a multi-bucket aggregation. That may change in the future (you can follow the work on this Github issue), but for now you have to use a workaround. You can nest the bucket_script aggregation inside a filters aggregation that only has one filter, with a match_all query. It's not pretty, but it gets the work done:
(By the way, please format any code snippets on this forum using the </> button. It makes your post much easier to read and it's more likely that someone will help you that way )
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.