How to merge results of two aggregations

Hi, I have two terms aggregations. Both return an id and a doc_count for each of the ids. How can I merge the result of these two. I would like to do an union as well as intersection of both the results.

Take a look at pipeline aggs https://www.elastic.co/guide/en/elasticsearch/reference/2.2/search-aggregations-pipeline.html

If that doesn't do it then you need to do it in code.