Is there a way to efficiently get three layer aggrigations result in elasticsearch, where each layer is conditioned by above one

I am trying to get composite aggregation for certain feilds which are present in each document. These documents are stored in elastic. Format of each document is a json with below mentioned keys. {"id","name","topic","bucket","key"}

Now I want to get result in below format: From documents, how much each topic count is, how much each bucket count is for each topic and how much key count is for each bucket. Now I had thought of using three layer aggrigation query, but is real slow for api's. Could anyone suggest some better options.