@Override
  public Facet reduce(List<Facet> facets) {
    if (facets.size() == 1) {
      return facets.get(0);
    }
    InternalDoubleTermsFacet first = (InternalDoubleTermsFacet) facets.get(0);
    TDoubleIntHashMap aggregated = CacheRecycler.popDoubleIntMap();
    long missing = 0;
    long total = 0;
    for (Facet facet : facets) {
      InternalDoubleTermsFacet mFacet = (InternalDoubleTermsFacet) facet;
      missing += mFacet.getMissingCount();
      total += mFacet.getTotalCount();
      for (DoubleEntry entry : mFacet.entries) {
        aggregated.adjustOrPutValue(entry.term, entry.getCount(), entry.getCount());
      }
    }

    BoundedTreeSet<DoubleEntry> ordered =
        new BoundedTreeSet<DoubleEntry>(first.comparatorType.comparator(), first.requiredSize);
    for (TDoubleIntIterator it = aggregated.iterator(); it.hasNext(); ) {
      it.advance();
      ordered.add(new DoubleEntry(it.key(), it.value()));
    }
    first.entries = ordered;
    first.missing = missing;
    first.total = total;

    CacheRecycler.pushDoubleIntMap(aggregated);

    return first;
  }
 public static InternalDoubleTermsFacet readTermsFacet(StreamInput in) throws IOException {
   InternalDoubleTermsFacet facet = new InternalDoubleTermsFacet();
   facet.readFrom(in);
   return facet;
 }