How to get aggregated results as tables using raw document

I have elasticsearch db where my test results are stored. I want to defect tickets and how many cases failed due to that defect as a table. I am able to display the defect, failed testcase id and log link as a table. But I want to summarize it and display the total testcase failed for each defect using aggregation.