public static FieldMaskingSpanQueryBuilder fromXContent(QueryParseContext parseContext)
      throws IOException {
    XContentParser parser = parseContext.parser();

    float boost = AbstractQueryBuilder.DEFAULT_BOOST;

    SpanQueryBuilder inner = null;
    String field = null;
    String queryName = null;

    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
      if (token == XContentParser.Token.FIELD_NAME) {
        currentFieldName = parser.currentName();
      } else if (token == XContentParser.Token.START_OBJECT) {
        if (parseContext.getParseFieldMatcher().match(currentFieldName, QUERY_FIELD)) {
          QueryBuilder query = parseContext.parseInnerQueryBuilder();
          if (!(query instanceof SpanQueryBuilder)) {
            throw new ParsingException(
                parser.getTokenLocation(), "[field_masking_span] query must be of type span query");
          }
          inner = (SpanQueryBuilder) query;
        } else {
          throw new ParsingException(
              parser.getTokenLocation(),
              "[field_masking_span] query does not support [" + currentFieldName + "]");
        }
      } else {
        if (parseContext
            .getParseFieldMatcher()
            .match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) {
          boost = parser.floatValue();
        } else if (parseContext.getParseFieldMatcher().match(currentFieldName, FIELD_FIELD)) {
          field = parser.text();
        } else if (parseContext
            .getParseFieldMatcher()
            .match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) {
          queryName = parser.text();
        } else {
          throw new ParsingException(
              parser.getTokenLocation(),
              "[field_masking_span] query does not support [" + currentFieldName + "]");
        }
      }
    }
    if (inner == null) {
      throw new ParsingException(
          parser.getTokenLocation(), "field_masking_span must have [query] span query clause");
    }
    if (field == null) {
      throw new ParsingException(
          parser.getTokenLocation(), "field_masking_span must have [field] set for it");
    }

    FieldMaskingSpanQueryBuilder queryBuilder = new FieldMaskingSpanQueryBuilder(inner, field);
    queryBuilder.boost(boost);
    queryBuilder.queryName(queryName);
    return queryBuilder;
  }
  public static Optional<SpanWithinQueryBuilder> fromXContent(QueryParseContext parseContext)
      throws IOException {
    XContentParser parser = parseContext.parser();

    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String queryName = null;
    SpanQueryBuilder big = null;
    SpanQueryBuilder little = null;

    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
      if (token == XContentParser.Token.FIELD_NAME) {
        currentFieldName = parser.currentName();
      } else if (token == XContentParser.Token.START_OBJECT) {
        if (parseContext.getParseFieldMatcher().match(currentFieldName, BIG_FIELD)) {
          Optional<QueryBuilder> query = parseContext.parseInnerQueryBuilder();
          if (query.isPresent() == false || query.get() instanceof SpanQueryBuilder == false) {
            throw new ParsingException(
                parser.getTokenLocation(), "span_within [big] must be of type span query");
          }
          big = (SpanQueryBuilder) query.get();
        } else if (parseContext.getParseFieldMatcher().match(currentFieldName, LITTLE_FIELD)) {
          Optional<QueryBuilder> query = parseContext.parseInnerQueryBuilder();
          if (query.isPresent() == false || query.get() instanceof SpanQueryBuilder == false) {
            throw new ParsingException(
                parser.getTokenLocation(), "span_within [little] must be of type span query");
          }
          little = (SpanQueryBuilder) query.get();
        } else {
          throw new ParsingException(
              parser.getTokenLocation(),
              "[span_within] query does not support [" + currentFieldName + "]");
        }
      } else if (parseContext
          .getParseFieldMatcher()
          .match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) {
        boost = parser.floatValue();
      } else if (parseContext
          .getParseFieldMatcher()
          .match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) {
        queryName = parser.text();
      } else {
        throw new ParsingException(
            parser.getTokenLocation(),
            "[span_within] query does not support [" + currentFieldName + "]");
      }
    }

    if (big == null) {
      throw new ParsingException(parser.getTokenLocation(), "span_within must include [big]");
    }
    if (little == null) {
      throw new ParsingException(parser.getTokenLocation(), "span_within must include [little]");
    }

    SpanWithinQueryBuilder query = new SpanWithinQueryBuilder(big, little);
    query.boost(boost).queryName(queryName);
    return Optional.of(query);
  }
 public static ParentIdQueryBuilder fromXContent(QueryParseContext parseContext)
     throws IOException {
   XContentParser parser = parseContext.parser();
   float boost = AbstractQueryBuilder.DEFAULT_BOOST;
   String type = null;
   String id = null;
   String queryName = null;
   String currentFieldName = null;
   boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
   XContentParser.Token token;
   while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
     if (token == XContentParser.Token.FIELD_NAME) {
       currentFieldName = parser.currentName();
     } else if (token.isValue()) {
       if (parseContext.getParseFieldMatcher().match(currentFieldName, TYPE_FIELD)) {
         type = parser.text();
       } else if (parseContext.getParseFieldMatcher().match(currentFieldName, ID_FIELD)) {
         id = parser.text();
       } else if (parseContext
           .getParseFieldMatcher()
           .match(currentFieldName, IGNORE_UNMAPPED_FIELD)) {
         ignoreUnmapped = parser.booleanValue();
       } else if (parseContext
           .getParseFieldMatcher()
           .match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) {
         boost = parser.floatValue();
       } else if (parseContext
           .getParseFieldMatcher()
           .match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) {
         queryName = parser.text();
       } else {
         throw new ParsingException(
             parser.getTokenLocation(),
             "[parent_id] query does not support [" + currentFieldName + "]");
       }
     } else {
       throw new ParsingException(
           parser.getTokenLocation(),
           "[parent_id] query does not support [" + currentFieldName + "]");
     }
   }
   ParentIdQueryBuilder queryBuilder = new ParentIdQueryBuilder(type, id);
   queryBuilder.queryName(queryName);
   queryBuilder.boost(boost);
   queryBuilder.ignoreUnmapped(ignoreUnmapped);
   return queryBuilder;
 }
  public static SpanNearQueryBuilder fromXContent(QueryParseContext parseContext)
      throws IOException {
    XContentParser parser = parseContext.parser();

    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    Integer slop = null;
    boolean inOrder = SpanNearQueryBuilder.DEFAULT_IN_ORDER;
    String queryName = null;

    List<SpanQueryBuilder> clauses = new ArrayList<>();

    String currentFieldName = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
      if (token == XContentParser.Token.FIELD_NAME) {
        currentFieldName = parser.currentName();
      } else if (token == XContentParser.Token.START_ARRAY) {
        if (parseContext.getParseFieldMatcher().match(currentFieldName, CLAUSES_FIELD)) {
          while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
            QueryBuilder query = parseContext.parseInnerQueryBuilder();
            if (!(query instanceof SpanQueryBuilder)) {
              throw new ParsingException(
                  parser.getTokenLocation(), "spanNear [clauses] must be of type span query");
            }
            clauses.add((SpanQueryBuilder) query);
          }
        } else {
          throw new ParsingException(
              parser.getTokenLocation(),
              "[span_near] query does not support [" + currentFieldName + "]");
        }
      } else if (token.isValue()) {
        if (parseContext.getParseFieldMatcher().match(currentFieldName, IN_ORDER_FIELD)) {
          inOrder = parser.booleanValue();
        } else if (parseContext
            .getParseFieldMatcher()
            .match(currentFieldName, COLLECT_PAYLOADS_FIELD)) {
          // Deprecated in 3.0.0
        } else if (parseContext.getParseFieldMatcher().match(currentFieldName, SLOP_FIELD)) {
          slop = parser.intValue();
        } else if (parseContext
            .getParseFieldMatcher()
            .match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) {
          boost = parser.floatValue();
        } else if (parseContext
            .getParseFieldMatcher()
            .match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) {
          queryName = parser.text();
        } else {
          throw new ParsingException(
              parser.getTokenLocation(),
              "[span_near] query does not support [" + currentFieldName + "]");
        }
      } else {
        throw new ParsingException(
            parser.getTokenLocation(),
            "[span_near] query does not support [" + currentFieldName + "]");
      }
    }

    if (clauses.isEmpty()) {
      throw new ParsingException(parser.getTokenLocation(), "span_near must include [clauses]");
    }

    if (slop == null) {
      throw new ParsingException(parser.getTokenLocation(), "span_near must include [slop]");
    }

    SpanNearQueryBuilder queryBuilder = new SpanNearQueryBuilder(clauses.get(0), slop);
    for (int i = 1; i < clauses.size(); i++) {
      queryBuilder.clause(clauses.get(i));
    }
    queryBuilder.inOrder(inOrder);
    queryBuilder.boost(boost);
    queryBuilder.queryName(queryName);
    return queryBuilder;
  }
  public static Optional<HasParentQueryBuilder> fromXContent(QueryParseContext parseContext)
      throws IOException {
    XContentParser parser = parseContext.parser();
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String parentType = null;
    boolean score = false;
    String queryName = null;
    InnerHitBuilder innerHits = null;
    boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;

    String currentFieldName = null;
    XContentParser.Token token;
    Optional<QueryBuilder> iqb = Optional.empty();
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
      if (token == XContentParser.Token.FIELD_NAME) {
        currentFieldName = parser.currentName();
      } else if (token == XContentParser.Token.START_OBJECT) {
        if (parseContext.getParseFieldMatcher().match(currentFieldName, QUERY_FIELD)) {
          iqb = parseContext.parseInnerQueryBuilder();
        } else if (parseContext.getParseFieldMatcher().match(currentFieldName, INNER_HITS_FIELD)) {
          innerHits = InnerHitBuilder.fromXContent(parseContext);
        } else {
          throw new ParsingException(
              parser.getTokenLocation(),
              "[has_parent] query does not support [" + currentFieldName + "]");
        }
      } else if (token.isValue()) {
        if (parseContext.getParseFieldMatcher().match(currentFieldName, TYPE_FIELD)) {
          parentType = parser.text();
        } else if (parseContext.getParseFieldMatcher().match(currentFieldName, SCORE_MODE_FIELD)) {
          String scoreModeValue = parser.text();
          if ("score".equals(scoreModeValue)) {
            score = true;
          } else if ("none".equals(scoreModeValue)) {
            score = false;
          } else {
            throw new ParsingException(
                parser.getTokenLocation(),
                "[has_parent] query does not support ["
                    + scoreModeValue
                    + "] as an option for score_mode");
          }
        } else if (parseContext.getParseFieldMatcher().match(currentFieldName, SCORE_FIELD)) {
          score = parser.booleanValue();
        } else if (parseContext
            .getParseFieldMatcher()
            .match(currentFieldName, IGNORE_UNMAPPED_FIELD)) {
          ignoreUnmapped = parser.booleanValue();
        } else if (parseContext
            .getParseFieldMatcher()
            .match(currentFieldName, AbstractQueryBuilder.BOOST_FIELD)) {
          boost = parser.floatValue();
        } else if (parseContext
            .getParseFieldMatcher()
            .match(currentFieldName, AbstractQueryBuilder.NAME_FIELD)) {
          queryName = parser.text();
        } else {
          throw new ParsingException(
              parser.getTokenLocation(),
              "[has_parent] query does not support [" + currentFieldName + "]");
        }
      }
    }
    if (iqb.isPresent() == false) {
      // if inner query is empty, bubble this up to caller so they can decide how to deal with it
      return Optional.empty();
    }
    HasParentQueryBuilder queryBuilder =
        new HasParentQueryBuilder(parentType, iqb.get(), score)
            .ignoreUnmapped(ignoreUnmapped)
            .queryName(queryName)
            .boost(boost);
    if (innerHits != null) {
      queryBuilder.innerHit(innerHits);
    }
    return Optional.of(queryBuilder);
  }