@Override
 public void writeTo(StreamOutput out) throws IOException {
   out.writeString(name);
   InternalOrder.Streams.writeOrder(order, out);
   ValueFormatterStreams.writeOptional(valueFormatter, out);
   out.writeVInt(requiredSize);
   out.writeVInt(buckets.size());
   for (InternalTerms.Bucket bucket : buckets) {
     out.writeLong(((Bucket) bucket).term);
     out.writeVLong(bucket.getDocCount());
     ((InternalAggregations) bucket.getAggregations()).writeTo(out);
   }
 }
 @Override
 public void readFrom(StreamInput in) throws IOException {
   this.name = in.readString();
   this.order = InternalOrder.Streams.readOrder(in);
   this.valueFormatter = ValueFormatterStreams.readOptional(in);
   this.requiredSize = in.readVInt();
   int size = in.readVInt();
   List<InternalTerms.Bucket> buckets = new ArrayList<InternalTerms.Bucket>(size);
   for (int i = 0; i < size; i++) {
     buckets.add(
         new Bucket(in.readLong(), in.readVLong(), InternalAggregations.readAggregations(in)));
   }
   this.buckets = buckets;
   this.bucketMap = null;
 }