<p dir="auto"><strong>Scenario 1</strong><br>
This is a very simplified scenario but captures the problem we are facing.</p>
<p dir="auto">We have a team lead document with a field for “Primary Product” and a field for “Secondary Product”.  We want to show the distribution of all products amongst team leads. To do so we would do a term aggregation on “Primary Product” and a term aggregation on “Secondary Product”.</p>
<p dir="auto">As this is the same products we want to merge the buckets together (done manually). This is simple to do with the term aggregation but has a problem. If both fields have the product, the document count will be one more than it should be once you merge the buckets. We need a way to realise if a document is already included in the count of the first aggregation, so we don’t add the same document from the second aggregation.</p>
<p dir="auto"><strong>Scenario 2</strong><br>
This next scenario is similar to our actual use case where we make use of nested documents but it is the same problem as above.</p>
<p dir="auto">Each team lead is a top level document nested with team members. We then want to show the distribution of teams that contain males or females, assuming a single team leader exists per team.</p>
<p dir="auto">Again we would run two aggregations. For team leaders this would be a term aggregation on gender. For team members this would be a nested term aggregation on gender followed by a reverse nested aggregation to get team leader counts for each gender. The two aggregation result buckets would then have to be manually combined. But it’s the same problem of working out when a team lead was already included in the first aggregation so any duplicate team leads that match in the second aggregation can be ignored.  (The aggregation we would use can be seen here <a href="https://gist.github.com/SMUnlimited/86e03e8f59ac6a09935c">https://gist.github.com/SMUnlimited/86e03e8f59ac6a09935c</a>)</p>
<p dir="auto">Another solution could be to include the team lead himself as a nested team member so we can just do a single nested term aggregation followed by a reverse nested aggregation to get the team member counts.  But this means you are duplicating all the team lead data which is not ideal.</p>
<p dir="auto"><g-emoji class="g-emoji" alias="boom" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/1f4a5.png">💥</g-emoji> What do people think would be the best way to solve this problem?</p>
<p dir="auto"><em>Note we haven’t thought about other types of aggregations yet (some may not be as straightforward to merge buckets) but in theory could also be applied.</em></p>
<p dir="auto">I can't seem to update the license. Not sure if I am doing something wrong, but at this point it is beginning to seem like a bug:<br>
<code class="notranslate">root@esdata0:/usr/share/elasticsearch/bin/x-pack# curl -u infegy esdata0:9200Enter host password for user 'infegy': { "name" : "data0", "cluster_name" : "atlantis", "version" : { "number" : "5.0.0-alpha5", "build_hash" : "d327dd4", "build_date" : "2016-08-04T08:59:39.568Z", "build_snapshot" : false, "lucene_version" : "6.1.0" }, "tagline" : "You Know, for Search" } </code></p>
<p dir="auto"><code class="notranslate">root@esdata0:/usr/share/elasticsearch/bin/x-pack# ./users list admin          : superuser infegy         : superuser</code></p>
<p dir="auto"><code class="notranslate">root@esdata0:/usr/share/elasticsearch/bin/x-pack# curl -XPUT -u infegy esdata0:9200/_xpack/license -d @/root/basic_license.json Enter host password for user 'infegy': curl: (52) Empty reply from server</code><br>
which coincides with:<br>
<code class="notranslate">[2016-09-23 20:17:14,401][WARN ][xpack.security.transport.netty4] [data0] caught exception while handling client http traffic, closing connection [id: 0xcc27ef9c, L:/10.17.218.166:9200 - R:/10.17.218.166:37552] java.lang.UnsupportedOperationException: unsupported message type: DefaultFullHttpResponse (expected: ByteBuf, FileRegion) at io.netty.channel.nio.AbstractNioByteChannel.filterOutboundMessage(AbstractNioByteChannel.java:260) at io.netty.channel.AbstractChannel$AbstractUnsafe.write(AbstractChannel.java:799) at io.netty.channel.DefaultChannelPipeline$HeadContext.write(DefaultChannelPipeline.java:1291) at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:748) at io.netty.channel.AbstractChannelHandlerContext.invokeWriteAndFlush(AbstractChannelHandlerContext.java:811) at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:824) at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:804) at io.netty.channel.AbstractChannelHandlerContext.writeAndFlush(AbstractChannelHandlerContext.java:841) at io.netty.handler.codec.MessageAggregator.decode(MessageAggregator.java:222) at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:88) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:372) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:358) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:350) at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:102) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:372) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:358) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:350) at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:293) at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:267) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:372) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:358) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:350) at io.netty.channel.ChannelInboundHandlerAdapter.channelRead(ChannelInboundHandlerAdapter.java:86) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:372) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:358) at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:350) at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1334) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:372) at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:358) at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:926) at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:129) at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:571) at io.netty.channel.nio.NioEventLoop.processSelectedKeysPlain(NioEventLoop.java:474) at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:428) at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:398) at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:877) at java.lang.Thread.run(Thread.java:745)</code></p>


<p dir="auto"><strong>Elasticsearch version</strong>:</p>
<p dir="auto"><strong>Plugins installed</strong>: []</p>
<p dir="auto"><strong>JVM version</strong>:</p>
<p dir="auto"><strong>OS version</strong>:</p>
<p dir="auto"><strong>Description of the problem including expected versus actual behavior</strong>:</p>
<p dir="auto"><strong>Steps to reproduce</strong>:<br>
1.<br>
2.<br>
3.</p>
<p dir="auto"><strong>Provide logs (if relevant)</strong>:</p>

<p dir="auto"><strong>Describe the feature</strong>:</p>