@Override
    public void writeTo(StreamOutput out) throws IOException {
      out.writeSharedString(index);
      out.writeOptionalSharedString(type);
      out.writeString(id);
      out.writeOptionalString(routing);
      if (fields == null) {
        out.writeVInt(0);
      } else {
        out.writeVInt(fields.length);
        for (String field : fields) {
          out.writeString(field);
        }
      }

      Versions.writeVersionWithVLongForBW(version, out);
      out.writeByte(versionType.getValue());

      FetchSourceContext.optionalWriteToStream(fetchSourceContext, out);
    }