public SearchSourceBuilder fromXContent(XContentParser parser, QueryParseContext context)
      throws IOException {
    SearchSourceBuilder builder = new SearchSourceBuilder();
    XContentParser.Token token = parser.currentToken();
    String currentFieldName = null;
    if (token != XContentParser.Token.START_OBJECT
        && (token = parser.nextToken()) != XContentParser.Token.START_OBJECT) {
      throw new ParsingException(
          parser.getTokenLocation(),
          "Expected [" + XContentParser.Token.START_OBJECT + "] but found [" + token + "]",
          parser.getTokenLocation());
    }
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
      if (token == XContentParser.Token.FIELD_NAME) {
        currentFieldName = parser.currentName();
      } else if (token.isValue()) {
        if (context.parseFieldMatcher().match(currentFieldName, FROM_FIELD)) {
          builder.from = parser.intValue();
        } else if (context.parseFieldMatcher().match(currentFieldName, SIZE_FIELD)) {
          builder.size = parser.intValue();
        } else if (context.parseFieldMatcher().match(currentFieldName, TIMEOUT_FIELD)) {
          builder.timeoutInMillis = parser.longValue();
        } else if (context.parseFieldMatcher().match(currentFieldName, TERMINATE_AFTER_FIELD)) {
          builder.terminateAfter = parser.intValue();
        } else if (context.parseFieldMatcher().match(currentFieldName, MIN_SCORE_FIELD)) {
          builder.minScore = parser.floatValue();
        } else if (context.parseFieldMatcher().match(currentFieldName, VERSION_FIELD)) {
          builder.version = parser.booleanValue();
        } else if (context.parseFieldMatcher().match(currentFieldName, EXPLAIN_FIELD)) {
          builder.explain = parser.booleanValue();
        } else if (context.parseFieldMatcher().match(currentFieldName, TRACK_SCORES_FIELD)) {
          builder.trackScores = parser.booleanValue();
        } else if (context.parseFieldMatcher().match(currentFieldName, _SOURCE_FIELD)) {
          builder.fetchSourceContext = FetchSourceContext.parse(parser, context);
        } else if (context.parseFieldMatcher().match(currentFieldName, FIELDS_FIELD)) {
          List<String> fieldNames = new ArrayList<>();
          fieldNames.add(parser.text());
          builder.fieldNames = fieldNames;
        } else if (context.parseFieldMatcher().match(currentFieldName, SORT_FIELD)) {
          builder.sort(parser.text());
        } else {
          throw new ParsingException(
              parser.getTokenLocation(),
              "Unknown key for a " + token + " in [" + currentFieldName + "].",
              parser.getTokenLocation());
        }
      } else if (token == XContentParser.Token.START_OBJECT) {
        if (context.parseFieldMatcher().match(currentFieldName, QUERY_FIELD)) {
          builder.queryBuilder = context.parseInnerQueryBuilder();
        } else if (context.parseFieldMatcher().match(currentFieldName, POST_FILTER_FIELD)) {
          builder.postQueryBuilder = context.parseInnerQueryBuilder();
        } else if (context.parseFieldMatcher().match(currentFieldName, _SOURCE_FIELD)) {
          builder.fetchSourceContext = FetchSourceContext.parse(parser, context);
        } else if (context.parseFieldMatcher().match(currentFieldName, SCRIPT_FIELDS_FIELD)) {
          List<ScriptField> scriptFields = new ArrayList<>();
          while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            String scriptFieldName = parser.currentName();
            token = parser.nextToken();
            if (token == XContentParser.Token.START_OBJECT) {
              Script script = null;
              boolean ignoreFailure = false;
              while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                  currentFieldName = parser.currentName();
                } else if (token.isValue()) {
                  if (context.parseFieldMatcher().match(currentFieldName, SCRIPT_FIELD)) {
                    script = Script.parse(parser, context.parseFieldMatcher());
                  } else if (context
                      .parseFieldMatcher()
                      .match(currentFieldName, IGNORE_FAILURE_FIELD)) {
                    ignoreFailure = parser.booleanValue();
                  } else {
                    throw new ParsingException(
                        parser.getTokenLocation(),
                        "Unknown key for a " + token + " in [" + currentFieldName + "].",
                        parser.getTokenLocation());
                  }
                } else if (token == XContentParser.Token.START_OBJECT) {
                  if (context.parseFieldMatcher().match(currentFieldName, SCRIPT_FIELD)) {
                    script = Script.parse(parser, context.parseFieldMatcher());
                  } else {
                    throw new ParsingException(
                        parser.getTokenLocation(),
                        "Unknown key for a " + token + " in [" + currentFieldName + "].",
                        parser.getTokenLocation());
                  }
                } else {
                  throw new ParsingException(
                      parser.getTokenLocation(),
                      "Unknown key for a " + token + " in [" + currentFieldName + "].",
                      parser.getTokenLocation());
                }
              }
              scriptFields.add(new ScriptField(scriptFieldName, script, ignoreFailure));
            } else {
              throw new ParsingException(
                  parser.getTokenLocation(),
                  "Expected ["
                      + XContentParser.Token.START_OBJECT
                      + "] in ["
                      + currentFieldName
                      + "] but found ["
                      + token
                      + "]",
                  parser.getTokenLocation());
            }
          }
          builder.scriptFields = scriptFields;
        } else if (context.parseFieldMatcher().match(currentFieldName, INDICES_BOOST_FIELD)) {
          ObjectFloatHashMap<String> indexBoost = new ObjectFloatHashMap<String>();
          while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            if (token == XContentParser.Token.FIELD_NAME) {
              currentFieldName = parser.currentName();
            } else if (token.isValue()) {
              indexBoost.put(currentFieldName, parser.floatValue());
            } else {
              throw new ParsingException(
                  parser.getTokenLocation(),
                  "Unknown key for a " + token + " in [" + currentFieldName + "].",
                  parser.getTokenLocation());
            }
          }
          builder.indexBoost = indexBoost;
        } else if (context.parseFieldMatcher().match(currentFieldName, AGGREGATIONS_FIELD)) {
          List<BytesReference> aggregations = new ArrayList<>();
          while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
            currentFieldName = parser.currentName();
            token = parser.nextToken();
            if (token == XContentParser.Token.START_OBJECT) {
              XContentBuilder xContentBuilder =
                  XContentFactory.contentBuilder(parser.contentType());
              xContentBuilder.startObject();
              xContentBuilder.field(currentFieldName);
              xContentBuilder.copyCurrentStructure(parser);
              xContentBuilder.endObject();
              aggregations.add(xContentBuilder.bytes());
            } else {
              throw new ParsingException(
                  parser.getTokenLocation(),
                  "Unknown key for a " + token + " in [" + currentFieldName + "].",
                  parser.getTokenLocation());
            }
          }
          builder.aggregations = aggregations;
        } else if (context.parseFieldMatcher().match(currentFieldName, HIGHLIGHT_FIELD)) {
          XContentBuilder xContentBuilder =
              XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser);
          builder.highlightBuilder = xContentBuilder.bytes();
        } else if (context.parseFieldMatcher().match(currentFieldName, INNER_HITS_FIELD)) {
          XContentBuilder xContentBuilder =
              XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser);
          builder.innerHitsBuilder = xContentBuilder.bytes();
        } else if (context.parseFieldMatcher().match(currentFieldName, SUGGEST_FIELD)) {
          XContentBuilder xContentBuilder = XContentFactory.contentBuilder(parser.contentType());
          xContentBuilder.copyCurrentStructure(parser);
          builder.suggestBuilder = xContentBuilder.bytes();
        } else if (context.parseFieldMatcher().match(currentFieldName, SORT_FIELD)) {
          List<BytesReference> sorts = new ArrayList<>();
          XContentBuilder xContentBuilder =
              XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser);
          sorts.add(xContentBuilder.bytes());
          builder.sorts = sorts;
        } else if (context.parseFieldMatcher().match(currentFieldName, EXT_FIELD)) {
          XContentBuilder xContentBuilder =
              XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser);
          builder.ext = xContentBuilder.bytes();
        } else {
          throw new ParsingException(
              parser.getTokenLocation(),
              "Unknown key for a " + token + " in [" + currentFieldName + "].",
              parser.getTokenLocation());
        }
      } else if (token == XContentParser.Token.START_ARRAY) {

        if (context.parseFieldMatcher().match(currentFieldName, FIELDS_FIELD)) {
          List<String> fieldNames = new ArrayList<>();
          while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            if (token == XContentParser.Token.VALUE_STRING) {
              fieldNames.add(parser.text());
            } else {
              throw new ParsingException(
                  parser.getTokenLocation(),
                  "Expected ["
                      + XContentParser.Token.VALUE_STRING
                      + "] in ["
                      + currentFieldName
                      + "] but found ["
                      + token
                      + "]",
                  parser.getTokenLocation());
            }
          }
          builder.fieldNames = fieldNames;
        } else if (context.parseFieldMatcher().match(currentFieldName, FIELDDATA_FIELDS_FIELD)) {
          List<String> fieldDataFields = new ArrayList<>();
          while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            if (token == XContentParser.Token.VALUE_STRING) {
              fieldDataFields.add(parser.text());
            } else {
              throw new ParsingException(
                  parser.getTokenLocation(),
                  "Expected ["
                      + XContentParser.Token.VALUE_STRING
                      + "] in ["
                      + currentFieldName
                      + "] but found ["
                      + token
                      + "]",
                  parser.getTokenLocation());
            }
          }
          builder.fieldDataFields = fieldDataFields;
        } else if (context.parseFieldMatcher().match(currentFieldName, SORT_FIELD)) {
          List<BytesReference> sorts = new ArrayList<>();
          while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            XContentBuilder xContentBuilder =
                XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser);
            sorts.add(xContentBuilder.bytes());
          }
          builder.sorts = sorts;
        } else if (context.parseFieldMatcher().match(currentFieldName, RESCORE_FIELD)) {
          List<BytesReference> rescoreBuilders = new ArrayList<>();
          while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            XContentBuilder xContentBuilder =
                XContentFactory.contentBuilder(parser.contentType()).copyCurrentStructure(parser);
            rescoreBuilders.add(xContentBuilder.bytes());
          }
          builder.rescoreBuilders = rescoreBuilders;
        } else if (context.parseFieldMatcher().match(currentFieldName, STATS_FIELD)) {
          List<String> stats = new ArrayList<>();
          while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            if (token == XContentParser.Token.VALUE_STRING) {
              stats.add(parser.text());
            } else {
              throw new ParsingException(
                  parser.getTokenLocation(),
                  "Expected ["
                      + XContentParser.Token.VALUE_STRING
                      + "] in ["
                      + currentFieldName
                      + "] but found ["
                      + token
                      + "]",
                  parser.getTokenLocation());
            }
          }
          builder.stats = stats;
        } else if (context.parseFieldMatcher().match(currentFieldName, _SOURCE_FIELD)) {
          builder.fetchSourceContext = FetchSourceContext.parse(parser, context);
        } else {
          throw new ParsingException(
              parser.getTokenLocation(),
              "Unknown key for a " + token + " in [" + currentFieldName + "].",
              parser.getTokenLocation());
        }
      } else {
        throw new ParsingException(
            parser.getTokenLocation(),
            "Unknown key for a " + token + " in [" + currentFieldName + "].",
            parser.getTokenLocation());
      }
    }
    return builder;
  }
 @Override
 public SearchSourceBuilder readFrom(StreamInput in) throws IOException {
   SearchSourceBuilder builder = new SearchSourceBuilder();
   if (in.readBoolean()) {
     int size = in.readVInt();
     List<BytesReference> aggregations = new ArrayList<>(size);
     for (int i = 0; i < size; i++) {
       aggregations.add(in.readBytesReference());
     }
     builder.aggregations = aggregations;
   }
   builder.explain = in.readOptionalBoolean();
   builder.fetchSourceContext = FetchSourceContext.optionalReadFromStream(in);
   boolean hasFieldDataFields = in.readBoolean();
   if (hasFieldDataFields) {
     int size = in.readVInt();
     List<String> fieldDataFields = new ArrayList<>(size);
     for (int i = 0; i < size; i++) {
       fieldDataFields.add(in.readString());
     }
     builder.fieldDataFields = fieldDataFields;
   }
   boolean hasFieldNames = in.readBoolean();
   if (hasFieldNames) {
     int size = in.readVInt();
     List<String> fieldNames = new ArrayList<>(size);
     for (int i = 0; i < size; i++) {
       fieldNames.add(in.readString());
     }
     builder.fieldNames = fieldNames;
   }
   builder.from = in.readVInt();
   if (in.readBoolean()) {
     builder.highlightBuilder = in.readBytesReference();
   }
   boolean hasIndexBoost = in.readBoolean();
   if (hasIndexBoost) {
     int size = in.readVInt();
     ObjectFloatHashMap<String> indexBoost = new ObjectFloatHashMap<String>(size);
     for (int i = 0; i < size; i++) {
       indexBoost.put(in.readString(), in.readFloat());
     }
     builder.indexBoost = indexBoost;
   }
   if (in.readBoolean()) {
     builder.innerHitsBuilder = in.readBytesReference();
   }
   if (in.readBoolean()) {
     builder.minScore = in.readFloat();
   }
   if (in.readBoolean()) {
     builder.postQueryBuilder = in.readQuery();
   }
   if (in.readBoolean()) {
     builder.queryBuilder = in.readQuery();
   }
   if (in.readBoolean()) {
     int size = in.readVInt();
     List<BytesReference> rescoreBuilders = new ArrayList<>();
     for (int i = 0; i < size; i++) {
       rescoreBuilders.add(in.readBytesReference());
     }
     builder.rescoreBuilders = rescoreBuilders;
   }
   if (in.readBoolean()) {
     int size = in.readVInt();
     List<ScriptField> scriptFields = new ArrayList<>(size);
     for (int i = 0; i < size; i++) {
       scriptFields.add(ScriptField.PROTOTYPE.readFrom(in));
     }
     builder.scriptFields = scriptFields;
   }
   builder.size = in.readVInt();
   if (in.readBoolean()) {
     int size = in.readVInt();
     List<BytesReference> sorts = new ArrayList<>();
     for (int i = 0; i < size; i++) {
       sorts.add(in.readBytesReference());
     }
     builder.sorts = sorts;
   }
   if (in.readBoolean()) {
     int size = in.readVInt();
     List<String> stats = new ArrayList<>();
     for (int i = 0; i < size; i++) {
       stats.add(in.readString());
     }
     builder.stats = stats;
   }
   if (in.readBoolean()) {
     builder.suggestBuilder = in.readBytesReference();
   }
   builder.terminateAfter = in.readVInt();
   builder.timeoutInMillis = in.readLong();
   builder.trackScores = in.readBoolean();
   builder.version = in.readOptionalBoolean();
   if (in.readBoolean()) {
     builder.ext = in.readBytesReference();
   }
   return builder;
 }